-
Notifications
You must be signed in to change notification settings - Fork 167
Tests/dashboard page #547
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
base: develop
Are you sure you want to change the base?
Tests/dashboard page #547
Changes from all commits
a54b480
a5ddb86
a6bce0f
c853476
364fa64
d99ee99
40f8e53
c029d7f
05ffb23
9560450
b4734d0
b21691c
759bf75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import Searchbar from '@/components/Dashboard/Searchbar'; | ||
| import { splitNSearch } from '@/utils/splitNSearch'; | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
|
|
||
| let testFunc; | ||
|
|
||
| jest.mock('../../../../src/utils/splitNSearch', () => ({ | ||
| splitNSearch: jest.fn(), | ||
| })); | ||
| describe('test searchbar component', function () { | ||
| it('renders the searchbar with appropriate label', function () { | ||
| render(<Searchbar label="test-label" />); | ||
|
|
||
| const label = screen.getByLabelText('test-label'); | ||
| expect(label).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('checks if we can type into the searchbar', function () { | ||
| render(<Searchbar label="test-label" />); | ||
|
|
||
| const input = screen.getByLabelText('test-label') as HTMLInputElement; | ||
|
|
||
| fireEvent.change(input, { target: { value: '123,456' } }); | ||
| expect(input.value).toBe('123,456'); | ||
| }); | ||
|
|
||
| it('tests if the click handler is called', function () { | ||
| render(<Searchbar label="test-label" />); | ||
|
|
||
| const input = screen.getByLabelText('test-label') as HTMLInputElement; | ||
|
|
||
| const searchButton = screen.getByRole('button'); | ||
|
|
||
| fireEvent.change(input, { tatget: { value: '123' } }); | ||
| fireEvent.click(searchButton); | ||
|
|
||
| expect(splitNSearch).toBeCalledTimes(1); | ||
| }); | ||
|
|
||
| it('tests if enter key calls search', function () { | ||
| render(<Searchbar label="test-label" />); | ||
|
|
||
| const input = screen.getByLabelText('test-label') as HTMLInputElement; | ||
|
|
||
| fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 }); | ||
|
|
||
| expect(splitNSearch).toBeCalledTimes(2); | ||
| }); | ||
|
|
||
| it('tests other key down events do not call search', function () { | ||
| render(<Searchbar label="test-label" />); | ||
|
|
||
| const input = screen.getByLabelText('test-label') as HTMLInputElement; | ||
|
|
||
| fireEvent.keyDown(input, { key: 'A', code: 'KeyA' }); | ||
|
|
||
| expect(splitNSearch).toBeCalledTimes(2); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we reset the times called? And when reading the title of the tests, and seeing the times it called its a little bit confusing as I would have thought it to be 0, but seeing it 2 will have to see actually how many times its being called |
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import DashboardPage from '@/pages/dashboard'; | ||
| import { renderWithRouter } from '@/test_utils/createMockRouter'; | ||
| import { Provider } from 'react-redux'; | ||
| import { store } from '@/app/store'; | ||
| import { fireEvent, screen } from '@testing-library/react'; | ||
|
|
||
| describe('dashboard page test', function () { | ||
| it('checks if the page is rendered with exact components', function () { | ||
| renderWithRouter( | ||
| <Provider store={store()}> | ||
| <DashboardPage /> | ||
| </Provider>, | ||
| { query: { dev: 'true' } } | ||
| ); | ||
|
|
||
| const searchBar = screen.getByRole('textbox'); | ||
| expect(searchBar).toBeInTheDocument(); | ||
|
|
||
| const searchButton = screen.getByRole('button'); | ||
| expect(searchButton.innerHTML).toBe('Search'); | ||
|
|
||
| const label = screen.getByLabelText('Users'); | ||
| expect(label).toBeInTheDocument(); | ||
|
Comment on lines
+16
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use test selector to get the components on the page?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this you can use screen.getByRole('button', { name: 'Search'}); This would get the specific element with role button and with text "Search".
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }); | ||
|
|
||
| it('renders 404 without passing the feature flag', function () { | ||
| renderWithRouter( | ||
| <Provider store={store()}> | ||
| <DashboardPage /> | ||
| </Provider> | ||
| ); | ||
|
|
||
| const headings = screen.getAllByRole('heading'); | ||
| expect(headings).toHaveLength(1); | ||
| expect(headings[0].innerHTML).toBe('404 - Page Not Found'); | ||
| }); | ||
|
|
||
| it('console logs the value', function () { | ||
| console.log = jest.fn(); | ||
|
|
||
| renderWithRouter( | ||
| <Provider store={store()}> | ||
| <DashboardPage /> | ||
| </Provider>, | ||
| { query: { dev: 'true' } } | ||
| ); | ||
|
|
||
| const input = screen.getByLabelText('Users') as HTMLInputElement; | ||
| fireEvent.change(input, { target: { value: 'jhon, doe' } }); | ||
|
|
||
| const searchButton = screen.getByRole('button'); | ||
| fireEvent.click(searchButton); | ||
|
|
||
| const value = input.value.split(','); | ||
| expect(console.log).toBeCalledWith('Searching', value); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.