-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(atomic): test radio-button render function (#4896)
* Add specs for radio-button render function * Remove tsconfig dependencies from stencil tsconfig. * Add [`@testing-library/jest-dom`](https://www.npmjs.com/package/@testing-library/jest-dom?activeTab=dependencies) to extend Jest’s matchers with additional utilities for testing DOM elements, such as `toBeInTheDocument`, etc. https://coveord.atlassian.net/browse/KIT-3898
- Loading branch information
Showing
8 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/atomic/src/components/common/radio-button.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import {fireEvent, within} from '@storybook/test'; | ||
import {html, render} from 'lit'; | ||
import {vi} from 'vitest'; | ||
import {createRipple} from '../../utils/ripple'; | ||
import {radioButton, RadioButtonProps} from './radio-button'; | ||
|
||
vi.mock('../../utils/ripple', () => ({ | ||
createRipple: vi.fn(), | ||
})); | ||
|
||
describe('radioButton', () => { | ||
let container: HTMLElement; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
const renderRadioButton = ( | ||
props: Partial<RadioButtonProps> | ||
): HTMLInputElement => { | ||
render( | ||
html`${radioButton({...props, groupName: 'test-group'})}`, | ||
container | ||
); | ||
return within(container).getByRole('radio'); | ||
}; | ||
|
||
it('should render a radio button with the correct attributes', () => { | ||
const props = { | ||
text: 'Test Radio Button', | ||
checked: true, | ||
ariaLabel: 'Test Radio Button', | ||
}; | ||
|
||
const input = renderRadioButton(props); | ||
|
||
expect(input).toBeInTheDocument(); | ||
expect(input.name).toBe('test-group'); | ||
expect(input.checked).toBe(true); | ||
expect(input.getAttribute('aria-label')).toBe('Test Radio Button'); | ||
expect(input.value).toBe('Test Radio Button'); | ||
expect(input.classList.contains('selected')).toBe(true); | ||
}); | ||
|
||
it('should call onChecked when the radio button is checked', async () => { | ||
const onChecked = vi.fn(); | ||
const props = { | ||
onChecked, | ||
}; | ||
|
||
const input = renderRadioButton(props); | ||
await fireEvent.click(input); | ||
|
||
await expect(onChecked).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle keyboard navigation', async () => { | ||
const {focus, keyDown} = fireEvent; | ||
const getRadio = (index: number) => | ||
within(container).getByLabelText(`radio-${index}`); | ||
const props = { | ||
groupName: 'test-group', | ||
selectWhenFocused: false, | ||
}; | ||
|
||
render( | ||
html`${radioButton({...props, text: 'radio-1'})} | ||
${radioButton({...props, text: 'radio-2'})} | ||
${radioButton({...props, text: 'radio-3'})}`, | ||
container | ||
); | ||
|
||
const inputs = within(container).getAllByRole('radio'); | ||
|
||
await focus(inputs[0]); | ||
await keyDown(inputs[0], {key: 'ArrowRight'}); | ||
|
||
await expect(getRadio(1)).toBeInTheDocument(); | ||
|
||
keyDown(inputs[1], {key: 'ArrowRight'}); | ||
await expect(getRadio(2)).toBeInTheDocument(); | ||
|
||
keyDown(inputs[2], {key: 'ArrowRight'}); | ||
await expect(getRadio(3)).toBeInTheDocument(); | ||
}); | ||
|
||
it('should create a ripple effect on mousedown', async () => { | ||
const mockedRipple = vi.mocked(createRipple); | ||
const props: Partial<RadioButtonProps> = { | ||
style: 'primary', | ||
}; | ||
|
||
const input = renderRadioButton(props); | ||
await fireEvent.mouseDown(input); | ||
|
||
await expect(mockedRipple).toHaveBeenCalledWith(expect.anything(), { | ||
color: 'primary', | ||
}); | ||
}); | ||
|
||
it('should render a radio button with the correct class', async () => { | ||
const props = { | ||
class: 'test-class', | ||
}; | ||
|
||
const input = renderRadioButton(props); | ||
expect(input).toBeInTheDocument(); | ||
expect(input.classList.contains('test-class')).toBe(true); | ||
expect(input.classList.contains('btn-radio')).toBe(true); | ||
expect(input.classList.contains('selected')).toBe(false); | ||
}); | ||
|
||
it('should render a radio button with the correct part attribute', () => { | ||
const props = { | ||
part: 'test-part', | ||
}; | ||
|
||
const input = renderRadioButton(props); | ||
expect(input.getAttribute('part')).toBe('test-part'); | ||
}); | ||
|
||
it('should render a radio button with the correct ref', () => { | ||
const ref = vi.fn(); | ||
const props = { | ||
groupName: 'test-group', | ||
ref, | ||
}; | ||
|
||
render(html`${radioButton(props)}`, container); | ||
|
||
expect(ref).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render a radio button with the correct aria-current attribute', () => { | ||
const props: Partial<RadioButtonProps> = { | ||
ariaCurrent: 'page', | ||
}; | ||
|
||
renderRadioButton(props); | ||
expect( | ||
within(container).getByRole('radio', {current: 'page'}) | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters