generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Unit Test] Write Component Unit Tests (#1110)
Components needed unit tests so some unit tests have been written. ## Changes - Updated vite config to facilitate in IDE running of tests - Wrote tests for the following component files - ScreenReaderOnly - ScrollToTop - SectionIntro.tsx - TextInput.tsx - WarningErrorIcon.tsx ## How to test this PR 1. Pull the branch for this PR 2. Navigate to the sbl-frontend directory 3. run `yarn run vitest` 4. Verify that the coverage is 100% across the board for the above files mentioned
- Loading branch information
1 parent
e253495
commit f0ad871
Showing
6 changed files
with
218 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import ScreenReaderOnly from 'components/ScreenReaderOnly'; | ||
|
||
describe('<ScreenReaderOnly />', () => { | ||
it('Renders expected content', async () => { | ||
render(<ScreenReaderOnly>test children</ScreenReaderOnly>); | ||
expect(screen.getByText('test children')).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { vi } from 'vitest'; | ||
import { render } from '@testing-library/react'; | ||
import ScrollToTop from 'components/ScrollToTop'; | ||
import type { Location } from 'react-router'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import * as ReactRouter from 'react-router'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
const location: Location = { | ||
state: null, | ||
key: 'default', | ||
pathname: '/test', | ||
search: '', | ||
hash: '', | ||
}; | ||
|
||
vi.mock('react-router-dom', async () => { | ||
const actual = await vi.importActual('react-router-dom'); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return { | ||
// @ts-expect-error This is standard testing methodology | ||
...actual, | ||
useLocation: vi.fn().mockImplementation(() => location), | ||
}; | ||
}); | ||
|
||
describe('<ScrollToTop />', () => { | ||
it('Renders expected content', async () => { | ||
const oldLocation: Location = { | ||
state: null, | ||
key: 'default', | ||
pathname: '/test', | ||
search: '', | ||
hash: '', | ||
}; | ||
const newLocation: Location = { | ||
state: null, | ||
key: 'default', | ||
pathname: '/test-new', | ||
search: '', | ||
hash: '', | ||
}; | ||
|
||
const scrollToMock = vi.spyOn(window, 'scrollTo'); | ||
const useLocationMock = vi.spyOn(ReactRouter, 'useLocation'); | ||
// @ts-expect-error This is standard testing methodology | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
useLocation.mockImplementation(() => oldLocation); | ||
useLocationMock.mockImplementation(() => oldLocation); | ||
|
||
expect(window.scrollTo).not.toHaveBeenCalled(); | ||
const { rerender } = render(<ScrollToTop />); | ||
expect(window.scrollTo).toHaveBeenCalled(); | ||
scrollToMock.mockReset(); | ||
|
||
expect(window.scrollTo).not.toHaveBeenCalled(); | ||
// @ts-expect-error This is standard testing methodology | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
useLocation.mockClear(); | ||
// @ts-expect-error This is standard testing methodology | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
useLocation.mockImplementation(() => newLocation); | ||
useLocationMock.mockClear(); | ||
useLocationMock.mockImplementation(() => newLocation); | ||
|
||
rerender(<ScrollToTop />); | ||
expect(window.scrollTo).toHaveBeenCalled(); | ||
}); | ||
}); |
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,31 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import SectionIntro from 'components/SectionIntro'; | ||
|
||
describe('<SectionIntro />', () => { | ||
it('Renders expected content', async () => { | ||
render( | ||
<SectionIntro data-testid='test' id='test'> | ||
{undefined} | ||
</SectionIntro>, | ||
); | ||
expect(screen.getByTestId('test')).toBeInTheDocument(); | ||
}); | ||
|
||
it('Renders expected heading content', async () => { | ||
render( | ||
<SectionIntro data-testid='test' id='test' heading='test heading'> | ||
{undefined} | ||
</SectionIntro>, | ||
); | ||
expect(screen.getByText('test heading')).toBeInTheDocument(); | ||
}); | ||
|
||
it('Renders expected child content', async () => { | ||
render( | ||
<SectionIntro data-testid='test' id='test'> | ||
<span>test content</span> | ||
</SectionIntro>, | ||
); | ||
expect(screen.getByText('test content')).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import TextInput from 'components/TextInput'; | ||
import { | ||
DefaultInputCharLimit, | ||
EmailInputCharLimit, | ||
PhoneInputCharLimit, | ||
UrlInputCharLimit, | ||
} from 'utils/constants'; | ||
|
||
describe('<TextInput />', () => { | ||
it('Renders expected content', async () => { | ||
render(<TextInput data-testid='test' id='test' name='test' />); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
|
||
it('Renders expected content - empty type', async () => { | ||
// @ts-expect-error This is a test that exists to test an otherwise unreachable default case | ||
render(<TextInput data-testid='test' id='test' name='test' type='' />); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - positive manual limit', async () => { | ||
render( | ||
<TextInput data-testid='test' id='test' name='test' maxLength={261} />, | ||
); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', '261'); | ||
}); | ||
|
||
it('Has correct char limit - negative manual limit', async () => { | ||
render( | ||
<TextInput data-testid='test' id='test' name='test' maxLength={-1} />, | ||
); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - url', async () => { | ||
render(<TextInput data-testid='test' id='test' name='test' type='url' />); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(UrlInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - tel', async () => { | ||
render(<TextInput data-testid='test' id='test' name='test' type='tel' />); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(PhoneInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - email', async () => { | ||
render(<TextInput data-testid='test' id='test' name='test' type='email' />); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(EmailInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - number', async () => { | ||
render( | ||
<TextInput data-testid='test' id='test' name='test' type='number' />, | ||
); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - password', async () => { | ||
render( | ||
<TextInput data-testid='test' id='test' name='test' type='password' />, | ||
); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - search', async () => { | ||
render( | ||
<TextInput data-testid='test' id='test' name='test' type='search' />, | ||
); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
|
||
it('Has correct char limit - text', async () => { | ||
render(<TextInput data-testid='test' id='test' name='test' type='text' />); | ||
const input = screen.getByTestId('test'); | ||
expect(input).toBeInTheDocument(); | ||
expect(input).toHaveAttribute('maxLength', String(DefaultInputCharLimit)); | ||
}); | ||
}); |
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,9 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import WarningErrorIcon from 'components/WarningErrorIcon'; | ||
|
||
describe('<WarningErrorIcon />', () => { | ||
it('Renders expected content', async () => { | ||
render(<WarningErrorIcon />); | ||
expect(screen.getByRole('img')).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