Skip to content

Commit c51552a

Browse files
authored
test: add missing unit tests (#1157)
* test: add missing unit tests for `<Banner>`, `<Datepicker>`, `<ThemeModeScript>` * chore: enable coverage report in `yarn test:open` You can now view test coverage in the local `vitest` window that is open in your browser, instead of needing to run `yarn test:coverage` in tandem.
1 parent be0df88 commit c51552a

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

src/components/Banner/Banner.spec.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import userEvent from '@testing-library/user-event';
2+
import { Banner } from './Banner';
3+
import { render, screen } from '@testing-library/react';
4+
import { describe, expect, it } from 'vitest';
5+
6+
describe('Components / Banner', () => {
7+
it('should close when collapse button is clicked', async () => {
8+
const user = userEvent.setup();
9+
render(
10+
<div>
11+
<Banner>
12+
<Banner.CollapseButton>Click me</Banner.CollapseButton>
13+
</Banner>
14+
</div>,
15+
);
16+
17+
await user.tab();
18+
await user.keyboard('[Space]');
19+
20+
expect(screen.queryByRole('banner')).not.toBeInTheDocument();
21+
});
22+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { describe, expect, it, vi } from 'vitest';
3+
import { Datepicker } from './Datepicker';
4+
import { getFormattedDate } from './helpers';
5+
import userEvent from '@testing-library/user-event';
6+
7+
describe('Components / Datepicker', () => {
8+
it("should display today's date by default", () => {
9+
const todaysDateInDefaultLanguage = getFormattedDate('en', new Date());
10+
11+
render(<Datepicker />);
12+
13+
expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument();
14+
});
15+
16+
it('should update date when a different day is clicked', async () => {
17+
const todaysDayOfMonth = new Date().getDate();
18+
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
19+
20+
render(<Datepicker />);
21+
22+
await userEvent.click(screen.getByRole('textbox'));
23+
await userEvent.click(screen.getAllByText(anotherDay)[0]);
24+
25+
expect((screen.getByRole('textbox') as HTMLInputElement).value?.includes(`${anotherDay}`)).toBeTruthy();
26+
});
27+
28+
it("should reset to today's date when Clear button is clicked", async () => {
29+
const todaysDateInDefaultLanguage = getFormattedDate('en', new Date());
30+
const todaysDayOfMonth = new Date().getDate();
31+
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
32+
33+
render(<Datepicker />);
34+
35+
await userEvent.click(screen.getByRole('textbox'));
36+
await userEvent.click(screen.getAllByText(anotherDay)[0]);
37+
await userEvent.click(screen.getByRole('textbox'));
38+
await userEvent.click(screen.getByText('Clear'));
39+
40+
expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument();
41+
});
42+
43+
it("should use today's date when Today button is clicked", async () => {
44+
const todaysDateInDefaultLanguage = getFormattedDate('en', new Date());
45+
const todaysDayOfMonth = new Date().getDate();
46+
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
47+
48+
render(<Datepicker />);
49+
50+
await userEvent.click(screen.getByRole('textbox'));
51+
await userEvent.click(screen.getAllByText(anotherDay)[0]);
52+
await userEvent.click(screen.getByRole('textbox'));
53+
await userEvent.click(screen.getByText('Today'));
54+
55+
expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument();
56+
});
57+
58+
it('should call `onSelectedDateChange` when a new date is selected', async () => {
59+
const onSelectedDateChange = vi.fn();
60+
const todaysDayOfMonth = new Date().getDate();
61+
const anotherDay = todaysDayOfMonth === 1 ? 2 : 1;
62+
63+
render(<Datepicker onSelectedDateChanged={onSelectedDateChange} />);
64+
65+
await userEvent.click(screen.getByRole('textbox'));
66+
await userEvent.click(screen.getAllByText(anotherDay)[0]);
67+
68+
expect(onSelectedDateChange).toHaveBeenCalledOnce();
69+
});
70+
71+
it('should close month overlay when user clicks outside of it', async () => {
72+
render(<Datepicker />);
73+
74+
await userEvent.click(screen.getByRole('textbox'));
75+
await userEvent.click(document.body);
76+
});
77+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { render } from '@testing-library/react';
2+
import { describe, expect, it } from 'vitest';
3+
import { ThemeModeScript } from './ThemeModeScript';
4+
5+
describe('Components / ThemeModeScript', () => {
6+
it('should insert `<script>`', () => {
7+
render(
8+
<>
9+
<ThemeModeScript />
10+
</>,
11+
);
12+
13+
expect(document.querySelector('script')).toBeDefined();
14+
});
15+
});

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const config: ViteConfig = {
1313
plugins: [react(), tsconfigPaths()],
1414
test: {
1515
coverage: {
16+
enabled: true,
1617
reporter: ['html', 'json', 'text'],
1718
},
1819
environment: 'jsdom',

0 commit comments

Comments
 (0)