forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit testing guide and example tests
- Loading branch information
1 parent
2166dff
commit 0399881
Showing
4 changed files
with
299 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {fireEvent, render, screen} from '@testing-library/react-native'; | ||
import React from 'react'; | ||
import colors from '@styles/theme/colors'; | ||
import Button from '@src/components/Button'; | ||
import type {ButtonProps} from '@src/components/Button'; | ||
|
||
const buttonText = 'Click me'; | ||
const accessibilityLabel = 'button-label'; | ||
|
||
describe('Button Component', () => { | ||
const renderButton = (props: ButtonProps = {}) => | ||
render( | ||
<Button | ||
text={buttonText} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
/>, | ||
); | ||
const onPress = jest.fn(); | ||
const getButton = () => screen.getByText(buttonText); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly with default text', () => { | ||
// Given the component is rendered | ||
renderButton(); | ||
|
||
// Then the default text is displayed | ||
expect(screen.getByText(buttonText)).toBeOnTheScreen(); | ||
}); | ||
|
||
it('renders without text gracefully', () => { | ||
// Given the component is rendered without text | ||
renderButton({text: undefined}); | ||
|
||
// Then the button is not displayed | ||
expect(screen.queryByText(buttonText)).not.toBeOnTheScreen(); | ||
}); | ||
|
||
it('handles press event correctly', () => { | ||
// Given the component is rendered with an onPress function | ||
renderButton({onPress}); | ||
|
||
// When the button is pressed | ||
fireEvent.press(getButton()); | ||
|
||
// Then the onPress function should be called | ||
expect(onPress).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('renders loading state', () => { | ||
// Given the component is rendered with isLoading | ||
renderButton({isLoading: true}); | ||
|
||
// Then the loading state is displayed | ||
expect(getButton()).toBeDisabled(); | ||
}); | ||
|
||
it('disables button when isDisabled is true', () => { | ||
// Given the component is rendered with isDisabled true | ||
renderButton({isDisabled: true}); | ||
|
||
// Then the button is disabled | ||
expect(getButton()).toBeDisabled(); | ||
}); | ||
|
||
it('sets accessibility label correctly', () => { | ||
// Given the component is rendered with an accessibility label | ||
renderButton({accessibilityLabel}); | ||
|
||
// Then the button should be accessible using the provided label | ||
expect(screen.getByLabelText(accessibilityLabel)).toBeOnTheScreen(); | ||
}); | ||
|
||
it('applies custom styles correctly', () => { | ||
// Given the component is rendered with custom styles | ||
renderButton({accessibilityLabel, innerStyles: {width: '100%'}}); | ||
|
||
// Then the button should have the custom styles | ||
const buttonContainer = screen.getByLabelText(accessibilityLabel); | ||
expect(buttonContainer).toHaveStyle({backgroundColor: colors.productDark400}); | ||
expect(buttonContainer).toHaveStyle({width: '100%'}); | ||
}); | ||
}); |
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,78 @@ | ||
import {fireEvent, render, screen} from '@testing-library/react-native'; | ||
import React from 'react'; | ||
import CheckboxWithLabel from '@src/components/CheckboxWithLabel'; | ||
import type {CheckboxWithLabelProps} from '@src/components/CheckboxWithLabel'; | ||
import Text from '@src/components/Text'; | ||
|
||
const LABEL = 'Agree to Terms'; | ||
describe('CheckboxWithLabel Component', () => { | ||
const mockOnInputChange = jest.fn(); | ||
const renderCheckboxWithLabel = (props: Partial<CheckboxWithLabelProps> = {}) => | ||
render( | ||
<CheckboxWithLabel | ||
label={LABEL} | ||
onInputChange={mockOnInputChange} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
/>, | ||
); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders the checkbox with label', () => { | ||
// Given the component is rendered | ||
renderCheckboxWithLabel(); | ||
// Then the label is displayed | ||
expect(screen.getByText(LABEL)).toBeOnTheScreen(); | ||
}); | ||
|
||
it('calls onInputChange when the checkbox is pressed', () => { | ||
// Given the component is rendered | ||
renderCheckboxWithLabel(); | ||
|
||
// When the checkbox is pressed | ||
const checkbox = screen.getByText(LABEL); | ||
fireEvent.press(checkbox); | ||
|
||
// Then the onInputChange function should be called with 'true' (checked) | ||
expect(mockOnInputChange).toHaveBeenCalledWith(true); | ||
|
||
// And when the checkbox is pressed again | ||
fireEvent.press(checkbox); | ||
|
||
// Then the onInputChange function should be called with 'false' (unchecked) | ||
expect(mockOnInputChange).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('displays error message when errorText is provided', () => { | ||
// Given the component is rendered with an error message | ||
const errorText = 'This field is required'; | ||
renderCheckboxWithLabel({errorText}); | ||
|
||
// Then the error message is displayed | ||
expect(screen.getByText(errorText)).toBeOnTheScreen(); | ||
}); | ||
|
||
it('renders custom LabelComponent if provided', () => { | ||
// Given the component is rendered with a custom LabelComponent | ||
function MockLabelComponent() { | ||
return <Text>Mock Label Component</Text>; | ||
} | ||
renderCheckboxWithLabel({LabelComponent: MockLabelComponent}); | ||
|
||
// Then the custom LabelComponent is displayed | ||
expect(screen.getByText('Mock Label Component')).toBeOnTheScreen(); | ||
}); | ||
|
||
it('is accessible and has the correct accessibility label', () => { | ||
// Given the component is rendered with an accessibility label | ||
const accessibilityLabel = 'checkbox-agree-to-terms'; | ||
renderCheckboxWithLabel({accessibilityLabel}); | ||
|
||
// Then the checkbox should be accessible using the provided label | ||
const checkbox = screen.getByLabelText(accessibilityLabel); | ||
expect(checkbox).toBeOnTheScreen(); | ||
}); | ||
}); |