Skip to content

Commit 10aa864

Browse files
authored
IBX-10693: Add tests for AltRadio components (#49)
1 parent ac75805 commit 10aa864

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

packages/components/src/components/AltRadio/AltRadioInput/AltRadioInput.test.stories.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ export const Default: Story = {
2727
const canvas = within(canvasElement);
2828
const input = canvas.getByRole('button');
2929

30-
await step('AltRadio handles focus event', async () => {
30+
await step('Click tile', async () => {
3131
await expect(args.onFocus).not.toHaveBeenCalled();
3232

3333
await userEvent.click(input);
3434

3535
await expect(args.onFocus).toHaveBeenCalledOnce();
3636
await expect(args.onChange).toHaveBeenCalledOnce();
37+
await expect(args.onChange).toHaveBeenCalledWith(true);
3738
await expect(args.onInput).toHaveBeenCalledOnce();
39+
await expect(args.onInput).toHaveBeenCalledWith(true);
3840
});
3941

40-
await step('AltRadio handles blur event', async () => {
42+
await step('Click outside tile', async () => {
4143
await expect(args.onBlur).not.toHaveBeenCalled();
4244

4345
await userEvent.click(canvasElement);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { Meta, StoryObj } from '@storybook/react';
2+
import { expect, fn, userEvent, within } from 'storybook/test';
3+
4+
import { AltRadiosListFieldStateful } from '.';
5+
6+
const meta: Meta<typeof AltRadiosListFieldStateful> = {
7+
component: AltRadiosListFieldStateful,
8+
tags: ['!dev'],
9+
args: {
10+
label: 'Choice Inputs List Label',
11+
helperText: 'This is a helper text',
12+
value: 'item1',
13+
items: [
14+
{ id: 'item1', label: 'Item 1', value: 'item1' },
15+
{ id: 'item2', label: 'Item 2', value: 'item2' },
16+
{ id: 'item3', label: 'Item 3', value: 'item3' },
17+
],
18+
onChange: fn(),
19+
},
20+
};
21+
22+
export default meta;
23+
24+
type Story = StoryObj<typeof AltRadiosListFieldStateful>;
25+
26+
export const Default: Story = {
27+
name: 'Default',
28+
play: async ({ canvasElement, step, args }) => {
29+
const canvas = within(canvasElement);
30+
const input2 = canvas.getByText('Item 2');
31+
const input3 = canvas.getByText('Item 3');
32+
33+
await step('Click last item', async () => {
34+
await userEvent.click(input3);
35+
36+
await expect(args.onChange).toHaveBeenCalledOnce();
37+
await expect(args.onChange).toHaveBeenLastCalledWith('item3');
38+
await expect(input3).toHaveClass('ids-alt-radio__tile--checked');
39+
});
40+
41+
await step('Click second item', async () => {
42+
await userEvent.click(input2);
43+
44+
await expect(args.onChange).toHaveBeenCalledTimes(2); // eslint-disable-line no-magic-numbers
45+
await expect(args.onChange).toHaveBeenLastCalledWith('item2');
46+
await expect(input2).toHaveClass('ids-alt-radio__tile--checked');
47+
await expect(input3).not.toHaveClass('ids-alt-radio__tile--checked');
48+
});
49+
50+
await step('Click second item once more', async () => {
51+
await userEvent.click(input2);
52+
53+
await expect(args.onChange).toHaveBeenCalledTimes(2); // eslint-disable-line no-magic-numbers
54+
});
55+
},
56+
};

0 commit comments

Comments
 (0)