-
-
Notifications
You must be signed in to change notification settings - Fork 181
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
[TASK-1046] Introduce unit tests for UI components #5120
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e93d423
initial jest configuration
pauloamorimbr 7a86de0
files location change and ts setup for jest
pauloamorimbr 1ee442d
update configuration and file names
pauloamorimbr 672049b
cleanup
pauloamorimbr 95ada22
Add koboSelect3 test
pauloamorimbr 478731d
comment
pauloamorimbr 08c1f83
broken up tests
pauloamorimbr d216eb3
reducing mock scope
pauloamorimbr c50aced
remove unused packages
pauloamorimbr aed5811
add jest to CI
pauloamorimbr bb8b161
add swc=true to tsconfig
pauloamorimbr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type {Config} from 'jest'; | ||
|
||
const config: Config = { | ||
verbose: true, | ||
testEnvironment: 'jsdom', | ||
roots: ['<rootDir>/../js', '<rootDir>'], | ||
moduleNameMapper: { | ||
'^js/(.*)$': '<rootDir>/../js/$1', | ||
'\\.(css|scss)$': 'identity-obj-proxy', | ||
}, | ||
setupFilesAfterEnv: ['<rootDir>/setupJestTest.ts'], | ||
transform: {'^.+\\.(t|j)sx?$': '@swc/jest'}, | ||
}; | ||
|
||
export default config; |
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,4 @@ | ||
import '@testing-library/jest-dom/jest-globals'; | ||
import '@testing-library/jest-dom'; | ||
|
||
global.t = (str: string) => str; |
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,66 @@ | ||
import Button from './button'; | ||
|
||
import {render, screen} from '@testing-library/react'; | ||
import {describe, it, expect, jest} from '@jest/globals'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const user = userEvent.setup(); | ||
|
||
describe('Enabled button', () => { | ||
// Mock | ||
const handleClickFunction = jest.fn(); | ||
|
||
beforeEach(() => { | ||
render( | ||
<Button | ||
type='primary' | ||
size='l' | ||
label='Button Label' | ||
onClick={handleClickFunction} | ||
/> | ||
); | ||
}); | ||
|
||
it('should render', async () => { | ||
// Assert | ||
expect(screen.getByLabelText('Button Label')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should be clickable', async () => { | ||
// Act | ||
const button = screen.getByLabelText('Button Label'); | ||
await user.click(button); | ||
|
||
expect(handleClickFunction).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('Disabled button', () => { | ||
// Mock | ||
const handleClickFunction = jest.fn(); | ||
|
||
beforeEach(() => { | ||
render( | ||
<Button | ||
type='primary' | ||
size='l' | ||
label='Button Label' | ||
onClick={handleClickFunction} | ||
isDisabled | ||
/> | ||
); | ||
}); | ||
|
||
it('should render', async () => { | ||
// Assert | ||
expect(screen.getByLabelText('Button Label')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not be clickable', async () => { | ||
// Act | ||
const button = screen.getByLabelText('Button Label'); | ||
await user.click(button); | ||
|
||
expect(handleClickFunction).not.toHaveBeenCalled(); | ||
}); | ||
}); |
136 changes: 136 additions & 0 deletions
136
jsapp/js/components/special/koboAccessibleSelect.spec.tsx
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,136 @@ | ||
import {fireEvent, render, screen} from '@testing-library/react'; | ||
import {describe, it, expect, jest} from '@jest/globals'; | ||
import userEvent from '@testing-library/user-event'; | ||
import type {KoboSelectOption} from './koboAccessibleSelect'; | ||
import KoboSelect3 from './koboAccessibleSelect'; | ||
import {useState} from 'react'; | ||
|
||
const options: KoboSelectOption[] = [ | ||
{value: '1', label: 'Apple'}, | ||
{value: '2', label: 'Banana'}, | ||
{value: '3', label: 'Avocado'}, | ||
]; | ||
|
||
// A wrapper is needed for the component to retain value changes | ||
const Wrapper = ({onChange}: {onChange: (newValue: string) => void}) => { | ||
const [value, setValue] = useState<string>(''); | ||
|
||
const handleChange = (newValue: string | null = '') => { | ||
setValue(newValue || ''); | ||
onChange(newValue || ''); | ||
}; | ||
|
||
return ( | ||
<KoboSelect3 | ||
name='testSelect' | ||
options={options} | ||
value={value} | ||
onChange={handleChange} | ||
/> | ||
); | ||
}; | ||
|
||
|
||
describe('KoboSelect3', () => { | ||
const user = userEvent.setup(); | ||
|
||
// Mock | ||
const scrollIntoViewMock = jest.fn(); | ||
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock; | ||
const onChangeMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
render(<Wrapper onChange={onChangeMock} />); | ||
}); | ||
|
||
it('should render with proper placeholder', async () => { | ||
const trigger = screen.getByRole('combobox'); | ||
const triggerLabel = trigger.querySelector('label'); | ||
expect(trigger).toBeInTheDocument(); | ||
expect(triggerLabel).toHaveTextContent('Select…'); | ||
}); | ||
|
||
it('should have the list closed on start', async () => { | ||
const list = screen.getByRole('listbox'); | ||
expect(list.dataset.expanded).toBe('false'); | ||
}); | ||
|
||
it('should have a list with the correct items count', async () => { | ||
const listOptions = screen.getAllByRole('option'); | ||
expect(listOptions).toHaveLength(3); | ||
}); | ||
|
||
it('should be selectable by mouse click', async () => { | ||
const trigger = screen.getByRole('combobox'); | ||
const list = screen.getByRole('listbox'); | ||
const listOptions = screen.getAllByRole('option'); | ||
|
||
// Clicks the trigger | ||
await user.click(trigger); | ||
expect(list.dataset.expanded).toBe('true'); | ||
|
||
// Select first option | ||
await user.click(listOptions[0]); | ||
|
||
// Onchange should be called with the correct value | ||
expect(onChangeMock).lastCalledWith(options[0].value); | ||
|
||
expect(list.dataset.expanded).toBe('false'); | ||
}); | ||
|
||
it('should be selectable by keyboard arrows', async () => { | ||
const trigger = screen.getByRole('combobox'); | ||
const triggerLabel = trigger.querySelector('label'); | ||
|
||
// No item selected | ||
expect(triggerLabel).toHaveTextContent('Select…'); | ||
|
||
// Increase option on arrow down | ||
fireEvent.keyDown(trigger, {key: 'ArrowDown'}); | ||
expect(onChangeMock).lastCalledWith(options[0].value); | ||
expect(triggerLabel).toHaveTextContent(options[0].label); | ||
|
||
// Increase option on arrow right | ||
fireEvent.keyDown(trigger, {key: 'ArrowRight'}); | ||
expect(onChangeMock).lastCalledWith(options[1].value); | ||
expect(triggerLabel).toHaveTextContent(options[1].label); | ||
fireEvent.keyDown(trigger, {key: 'ArrowRight'}); | ||
expect(onChangeMock).lastCalledWith(options[2].value); | ||
expect(triggerLabel).toHaveTextContent(options[2].label); | ||
|
||
// Don't go past the last one | ||
onChangeMock.mockReset(); | ||
fireEvent.keyDown(trigger, {key: 'ArrowDown'}); | ||
expect(onChangeMock).not.toHaveBeenCalled(); | ||
expect(triggerLabel).toHaveTextContent(options[2].label); | ||
|
||
// Decrease option on arrow up | ||
fireEvent.keyDown(trigger, {key: 'ArrowUp'}); | ||
expect(onChangeMock).lastCalledWith(options[1].value); | ||
expect(triggerLabel).toHaveTextContent(options[1].label); | ||
|
||
// Decrease option on arrow left | ||
fireEvent.keyDown(trigger, {key: 'ArrowLeft'}); | ||
expect(onChangeMock).lastCalledWith(options[0].value); | ||
expect(triggerLabel).toHaveTextContent(options[0].label); | ||
|
||
// Don't go past the first one | ||
onChangeMock.mockReset(); | ||
fireEvent.keyDown(trigger, {key: 'ArrowUp'}); | ||
expect(onChangeMock).not.toHaveBeenCalled(); | ||
expect(triggerLabel).toHaveTextContent(options[0].label); | ||
}); | ||
|
||
it('should be selectable by typing', async () => { | ||
const trigger = screen.getByRole('combobox'); | ||
const triggerLabel = trigger.querySelector('label'); | ||
|
||
// No item selected | ||
expect(triggerLabel).toHaveTextContent('Select…'); | ||
|
||
// Type 'b' to select Banana | ||
fireEvent.keyDown(trigger, {key: 'b'}); | ||
expect(onChangeMock).lastCalledWith(options[1].value); | ||
expect(triggerLabel).toHaveTextContent(options[1].label); | ||
}); | ||
}); | ||
p2edwards marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
📓 Good point — many of our Kobo components are like this — 'controlled'-only. It'd be possible to make them more flexible.
'Course, then there would be more to test 😅
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.
It's a common practice I found for single component testing, since most of the components will depend on external context anyways. So I really don't think it's the case of making them 'uncontrolled' just for the sake of tests. We only need to be careful to not create biased tests that could influence the component behavior. (but yeah, cool article! 😄)