Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
60 changes: 60 additions & 0 deletions __tests__/Unit/Components/Searchbar/Searchbar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Searchbar from '@/components/Dashboard/Searchbar';
import { fireEvent, render, screen } from '@testing-library/react';

let mockFn: jest.Func;

beforeEach(() => {
mockFn = jest.fn();
});

describe('test searchbar component', function () {
it('renders the searchbar with appropriate label', function () {
render(<Searchbar label="test-label" handleSearch={mockFn} />);

const label = screen.getByLabelText('test-label');
expect(label).toBeInTheDocument();
});

it('checks if we can type into the searchbar', function () {
render(<Searchbar label="test-label" handleSearch={mockFn} />);

const input = screen.getByLabelText('test-label') as HTMLInputElement;

fireEvent.click(input);
Comment thread
RitikJaiswal75 marked this conversation as resolved.
Outdated
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" handleSearch={mockFn} />);

const input = screen.getByLabelText('test-label') as HTMLInputElement;

const searchButton = screen.getByRole('button');
Comment thread
RitikJaiswal75 marked this conversation as resolved.

fireEvent.change(input, { tatget: { value: '123' } });
fireEvent.click(searchButton);

expect(mockFn).toBeCalledTimes(1);
});

it('tests if enter key calls search', function () {
render(<Searchbar label="test-label" handleSearch={mockFn} />);

const input = screen.getByLabelText('test-label') as HTMLInputElement;

fireEvent.keyDown(input, { key: 'Enter', code: 'Enter', charCode: 13 });

expect(mockFn).toBeCalledTimes(1);
});

it('tests other key down events do not call search', function () {
render(<Searchbar label="test-label" handleSearch={mockFn} />);

const input = screen.getByLabelText('test-label') as HTMLInputElement;

fireEvent.keyDown(input, { key: 'A', code: 'KeyA' });

expect(mockFn).toBeCalledTimes(0);
});
});
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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);
});
});
2 changes: 1 addition & 1 deletion src/components/Dashboard/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Searchbar({ label, handleSearch }: searchProps) {
const [query, setQuery] = useState('');

const handleKeyPress = (event: KeyboardEvent) => {
if (event.key == 'Enter') {
if (event.key === 'Enter') {
handleSearch(query);
}
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pages/issues.tsx → src/pages/issues/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, useState, useEffect, ChangeEvent } from 'react';
import IssueList from '../components/issues/IssueList';
import IssueList from '../../components/issues/IssueList';
import classNames from '@/styles/issues.module.scss';
import Layout from '@/components/Layout';
import Head from '@/components/head';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.