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

Improve Dashboard Page of Status Site #636

Merged
merged 9 commits into from
Jun 22, 2023
64 changes: 64 additions & 0 deletions __tests__/Unit/Components/Searchbar/searchbar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Searchbar from '@/components/Dashboard/Searchbar';
import { splitNSearch } from '@/utils/splitNSearch';
import { fireEvent, render, screen } from '@testing-library/react';

jest.mock('../../../../src/utils/splitNSearch', () => ({
splitNSearch: jest.fn(),
}));

describe('test searchbar component', function () {
it('renders searchbar input and a search button', function () {
render(<Searchbar label="test-label" />);

const input = screen.getByTestId('searchbar_input') as HTMLInputElement;
const searchBtn = screen.getByTestId('search_btn') as HTMLButtonElement;
RitikJaiswal75 marked this conversation as resolved.
Show resolved Hide resolved

expect(input).toBeInTheDocument();
expect(input).toHaveAttribute('placeholder', 'test-label:');
expect(searchBtn).toBeInTheDocument();
expect(searchBtn).toHaveTextContent('Search');
});

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

const input = screen.getByTestId('searchbar_input') 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.getByTestId('searchbar_input') as HTMLInputElement;

const searchBtn = screen.getByTestId('search_btn') as HTMLButtonElement;

fireEvent.change(input, { target: { value: '123' } });
fireEvent.click(searchBtn);

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

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

const input = screen.getByTestId('searchbar_input') as HTMLInputElement;

fireEvent.change(input, { target: { value: '123' } });
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.getByTestId('searchbar_input') as HTMLInputElement;

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

expect(splitNSearch).toBeCalledTimes(2);
RitikJaiswal75 marked this conversation as resolved.
Show resolved Hide resolved
});
});
56 changes: 56 additions & 0 deletions __tests__/Unit/pages/Dashboard/dashboardTest.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.getByTestId('searchbar_input');
expect(searchBar).toBeInTheDocument();

const searchButton = screen.getByTestId('search_btn');
expect(searchButton.innerHTML).toBe('Search');
});

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');
bharati-21 marked this conversation as resolved.
Show resolved Hide resolved
});

it('console logs the value', function () {
console.log = jest.fn();
RitikJaiswal75 marked this conversation as resolved.
Show resolved Hide resolved

renderWithRouter(
<Provider store={store()}>
<DashboardPage />
</Provider>,
{ query: { dev: 'true' } }
);

const input = screen.getByTestId('searchbar_input') as HTMLInputElement;
fireEvent.change(input, { target: { value: 'jhon, doe' } });

const searchButton = screen.getByTestId(
'search_btn'
) as HTMLButtonElement;
fireEvent.click(searchButton);

const value = input.value.split(',');
expect(console.log).toBeCalledWith('Searching', value);
});
});
64 changes: 37 additions & 27 deletions src/components/Dashboard/Dashboard.module.scss
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
$base-unit: 4px;
$base-font-size: 0.3rem;
$color-blue: #041187;
$color-red: #e30062;
$color-green: #85da6b;
$color-disabled: #e5e7eb;
$color-disabled: #7c7e82;
$color-white: #fff;
$color-black: #000;
$diff-margin: $base-unit * 15;
$similar-margin: $base-unit * 8;
$section-padding-top-bottom: $base-unit * 30;
$section-padding-left-right: $base-unit * 25;

.container {
margin: $diff-margin auto;
width: 85%;
}

.searchLabel {
font-size: $base-font-size * 5;
padding-right: $base-unit * 8;
color: $color-blue;
margin: 40px auto;
width: 95%;
display: flex;
justify-content: center;
line-height: 2rem;
align-items: center;
}

.searchBar {
RitikJaiswal75 marked this conversation as resolved.
Show resolved Hide resolved
background-color: $color-disabled;
border-radius: $base-unit * 2;
border: none;
width: 80%;
height: $base-unit * 15;
font-size: $base-font-size * 5;
width: 600px;
font-family: sans-serif;
margin-right: 8px;
border-radius: 4px;
font-size: 1.2rem;
padding: 0.5rem;
}

.searchBtn {
margin-left: $base-unit * 8;
.btn {
background-color: $color-blue;
border-radius: 4px;
color: white;
background-color: $color-blue;
color: $color-white;
height: $base-unit * 15;
border-radius: $base-unit * 2;
border: none;
width: $base-unit * 40;
padding: 12px 60px;
cursor: pointer;
}

.btn:disabled {
color: white;
background-color: $color-disabled;
cursor: not-allowed;
}

@media only screen and (max-width: 520px) {
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.searchBar {
width: 95%;
bharati-21 marked this conversation as resolved.
Show resolved Hide resolved
margin: 4px;
}
}
11 changes: 6 additions & 5 deletions src/components/Dashboard/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@ function Searchbar({ label }: searchProps) {
const [query, setQuery] = useState('');

const handleKeyPress = (event: KeyboardEvent) => {
if (event.key === 'Enter') {
if (event.key === 'Enter' && query !== '') {
splitNSearch(query);
}
};

return (
<section className={styles.container}>
<label htmlFor="search" className={styles.searchLabel}>
{label}
</label>
<input
data-testid="searchbar_input"
type="text"
id="search"
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={(e) => handleKeyPress(e)}
className={styles.searchBar}
placeholder={label + ':'}
/>
<button
shubham-y marked this conversation as resolved.
Show resolved Hide resolved
data-testid="search_btn"
type="submit"
onClick={() => {
splitNSearch(query);
shubham-y marked this conversation as resolved.
Show resolved Hide resolved
}}
className={styles.searchBtn}
className={styles.btn}
disabled={query === ''}
>
Search
</button>
Expand Down
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 = () => {
const router = useRouter();

Expand Down