-
Notifications
You must be signed in to change notification settings - Fork 0
IBX-10695: Add tests for RadioButton components #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,23 +25,33 @@ export const Default: Story = { | |||||
name: 'Default', | ||||||
play: async ({ canvasElement, step, args }) => { | ||||||
const canvas = within(canvasElement); | ||||||
const input = canvas.getByRole('radio'); | ||||||
const input = canvas.getByRole<HTMLInputElement>('radio'); | ||||||
|
||||||
await step('Radio Button handles focus event', async () => { | ||||||
await step('Click radio button', async () => { | ||||||
await expect(args.onFocus).not.toHaveBeenCalled(); | ||||||
|
||||||
await userEvent.click(input); | ||||||
|
||||||
await expect(args.onFocus).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(args.onInput).toHaveBeenCalledOnce(); | ||||||
await expect(args.onInput).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(input.checked).toBe(true); | ||||||
}); | ||||||
|
||||||
await step('Click radio button again', async () => { | ||||||
await userEvent.click(input); | ||||||
|
||||||
await expect(args.onFocus).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(args.onInput).toHaveBeenCalledOnce(); | ||||||
await expect(args.onInput).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(input.checked).toBe(true); | ||||||
}); | ||||||
|
||||||
await step('Radio Button handles blur event', async () => { | ||||||
await step('Click outside checkbox', async () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The step description mentions 'checkbox' but this is testing a radio button component. It should be 'Click outside radio button'.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
await expect(args.onBlur).not.toHaveBeenCalled(); | ||||||
|
||||||
await userEvent.click(canvasElement); | ||||||
|
@@ -50,3 +60,35 @@ export const Default: Story = { | |||||
}); | ||||||
}, | ||||||
}; | ||||||
|
||||||
export const UsingLabel: Story = { | ||||||
name: 'Using Label', | ||||||
play: async ({ canvasElement, step, args }) => { | ||||||
const canvas = within(canvasElement); | ||||||
const input = canvas.getByRole<HTMLInputElement>('radio'); | ||||||
const label = canvas.getByText('Radio Button Label'); | ||||||
|
||||||
await step('Click radio button label', async () => { | ||||||
await expect(args.onFocus).not.toHaveBeenCalled(); | ||||||
|
||||||
await userEvent.click(label); | ||||||
|
||||||
await expect(args.onFocus).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(args.onInput).toHaveBeenCalledOnce(); | ||||||
await expect(args.onInput).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(input.checked).toBe(true); | ||||||
}); | ||||||
|
||||||
await step('Click radio button label again', async () => { | ||||||
await userEvent.click(label); | ||||||
|
||||||
await expect(args.onChange).toHaveBeenCalledOnce(); | ||||||
await expect(args.onChange).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
await expect(args.onInput).toHaveBeenCalledOnce(); | ||||||
await expect(args.onInput).toHaveBeenLastCalledWith(true, expect.anything()); | ||||||
Comment on lines
+87
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous issue, these assertions expect onChange and onInput to be called only once total, but this is the second interaction (clicking the label again). The call counts should reflect cumulative calls or be adjusted based on expected radio button behavior. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
await expect(input.checked).toBe(true); | ||||||
}); | ||||||
}, | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { expect, fn, userEvent, within } from 'storybook/test'; | ||
|
||
import { RadioButtonsListFieldStateful } from '.'; | ||
|
||
const meta: Meta<typeof RadioButtonsListFieldStateful> = { | ||
component: RadioButtonsListFieldStateful, | ||
tags: ['!dev'], | ||
args: { | ||
label: 'Choice Inputs List Label', | ||
helperText: 'This is a helper text', | ||
value: 'item1', | ||
items: [ | ||
{ id: 'item1', label: 'Item 1', value: 'item1' }, | ||
{ id: 'item2', label: 'Item 2', value: 'item2' }, | ||
{ id: 'item3', label: 'Item 3', value: 'item3' }, | ||
], | ||
onChange: fn(), | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof RadioButtonsListFieldStateful>; | ||
|
||
export const Default: Story = { | ||
name: 'Default', | ||
play: async ({ canvasElement, step, args }) => { | ||
const canvas = within(canvasElement); | ||
const input2 = canvas.getByRole<HTMLInputElement>('radio', { name: 'Item 2' }); | ||
const input3 = canvas.getByRole<HTMLInputElement>('radio', { name: 'Item 3' }); | ||
|
||
await step('Click last item', async () => { | ||
await userEvent.click(input3); | ||
|
||
await expect(args.onChange).toHaveBeenCalledOnce(); | ||
await expect(args.onChange).toHaveBeenLastCalledWith('item3'); | ||
await expect(input3.checked).toBe(true); | ||
}); | ||
|
||
await step('Click second item', async () => { | ||
await userEvent.click(input2); | ||
|
||
await expect(args.onChange).toHaveBeenCalledTimes(2); // eslint-disable-line no-magic-numbers | ||
await expect(args.onChange).toHaveBeenLastCalledWith('item2'); | ||
await expect(input2.checked).toBe(true); | ||
await expect(input3.checked).toBe(false); | ||
}); | ||
|
||
await step('Click second item once more', async () => { | ||
await userEvent.click(input2); | ||
|
||
await expect(args.onChange).toHaveBeenCalledTimes(2); // eslint-disable-line no-magic-numbers | ||
}); | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertions expect onFocus, onChange, and onInput to be called only once each, but this is the second click on the radio button. Radio buttons typically don't trigger onChange/onInput events when clicked again if already selected, so these call counts should reflect the cumulative calls from both steps.
Copilot uses AI. Check for mistakes.