-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add event hooks to existing components (#1757)
- accordion Add event tests for remaining form field components
- Loading branch information
1 parent
e3f2bc1
commit 9045cf5
Showing
10 changed files
with
107 additions
and
33 deletions.
There are no files selected for viewing
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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { generateSnapshots } from '@chanzuckerberg/story-utils'; | ||
import type { StoryFile } from '@storybook/testing-react'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
|
||
import userEvent from '@testing-library/user-event'; | ||
import React from 'react'; | ||
import { InputField } from './InputField'; | ||
|
||
import * as stories from './InputField.stories'; | ||
|
||
describe('<InputField />', () => { | ||
generateSnapshots(stories as StoryFile); | ||
|
||
it('handles changes to the text within the component', async () => { | ||
const user = userEvent.setup(); | ||
const onChange = jest.fn(); | ||
|
||
render( | ||
<InputField | ||
aria-label="label" | ||
data-testid="test-input" | ||
onChange={onChange} | ||
/>, | ||
); | ||
const input = screen.getByTestId('test-input'); | ||
const testText = 'typing'; | ||
|
||
input.focus(); | ||
|
||
await act(async () => { | ||
await user.keyboard(testText); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(testText.length); | ||
}); | ||
}); |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,43 @@ | ||
import { generateSnapshots } from '@chanzuckerberg/story-utils'; | ||
import type { StoryFile } from '@storybook/testing-react'; | ||
import { composeStories } from '@storybook/testing-react'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import React from 'react'; | ||
import { Radio } from './Radio'; | ||
import * as stories from './Radio.stories'; | ||
|
||
const { Default } = composeStories(stories); | ||
|
||
describe('<Radio />', () => { | ||
generateSnapshots(stories as StoryFile); | ||
|
||
test('should toggle the radio with space', async () => { | ||
const user = userEvent.setup(); | ||
render(<Default />); | ||
const onChange = jest.fn(); | ||
|
||
function ControlledRadio() { | ||
const [checked, setChecked] = React.useState(false); | ||
const handleChange = () => { | ||
setChecked(!checked); | ||
onChange(); | ||
}; | ||
|
||
return ( | ||
<Radio | ||
aria-label="test-radio" | ||
checked={checked} | ||
onChange={handleChange} | ||
/> | ||
); | ||
} | ||
|
||
render(<ControlledRadio />); | ||
const radio = screen.getByRole('radio'); | ||
radio.focus(); | ||
|
||
await act(async () => { | ||
await user.keyboard(' '); | ||
}); | ||
|
||
expect(radio).toBeChecked(); | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |