Skip to content
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

Tests/dashboard page #547

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions __tests__/Unit/Components/Searchbar/Searchbar.test.tsx
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');
RitikJaiswal75 marked this conversation as resolved.
Show resolved Hide resolved

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we reset the times called?
If some new tests above are added which do the same or we update some, then the rest of the test will fail as toBeCalledTimes would change

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

});
});
57 changes: 57 additions & 0 deletions __tests__/Unit/pages/Dashboard/dashboardPage.test.tsx
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use test selector to get the components on the page?
If some new button or some new element is added in the file before the search button, won't the test fail?

Copy link
Contributor

Choose a reason for hiding this comment

The 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".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
});
});
5 changes: 0 additions & 5 deletions src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import PageNotFound from '../404';
import NavBar from '@/components/navBar';
import Searchbar from '@/components/Dashboard/Searchbar';

const search = (query: string) => {
const searchValues = query.split(',');
console.log('Searching', searchValues);
};

const DashboardPage = () => {
RitikJaiswal75 marked this conversation as resolved.
Show resolved Hide resolved
const router = useRouter();

Expand Down