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

[Unit Test] Write Component Unit Tests #1110

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Few more tests
tanner-ricks committed Jan 15, 2025
commit c02676babfc7ec8c9b67781dfaea87a04d55598e
9 changes: 9 additions & 0 deletions src/components/__tests__/ScreenReaderOnly.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { render, screen } from '@testing-library/react';
import ScreenReaderOnly from 'components/ScreenReaderOnly';

describe('<ScreenReaderOnly />', () => {
it('Renders expected content', async () => {
render(<ScreenReaderOnly>test children</ScreenReaderOnly>);
expect(screen.getByText('test children')).toBeInTheDocument();
});
});
69 changes: 69 additions & 0 deletions src/components/__tests__/ScrollToTop.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { vi } from 'vitest';
import { render } from '@testing-library/react';
import ScrollToTop from 'components/ScrollToTop';
import type { Location } from 'react-router';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as ReactRouter from 'react-router';
import { useLocation } from 'react-router-dom';

const location: Location = {
state: null,
key: 'default',
pathname: '/test',
search: '',
hash: '',
};

vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom');
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return {
// @ts-expect-error This is standard testing methodology
...actual,
useLocation: vi.fn().mockImplementation(() => location),
};
});

describe('<ScrollToTop />', () => {
it('Renders expected content', async () => {
const oldLocation: Location = {
state: null,
key: 'default',
pathname: '/test',
search: '',
hash: '',
};
const newLocation: Location = {
state: null,
key: 'default',
pathname: '/test-new',
search: '',
hash: '',
};

const scrollToMock = vi.spyOn(window, 'scrollTo');
const useLocationMock = vi.spyOn(ReactRouter, 'useLocation');
// @ts-expect-error This is standard testing methodology
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
useLocation.mockImplementation(() => oldLocation);
useLocationMock.mockImplementation(() => oldLocation);

expect(window.scrollTo).not.toHaveBeenCalled();
const { rerender } = render(<ScrollToTop />);
expect(window.scrollTo).toHaveBeenCalled();
scrollToMock.mockReset();

expect(window.scrollTo).not.toHaveBeenCalled();
// @ts-expect-error This is standard testing methodology
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
useLocation.mockClear();
// @ts-expect-error This is standard testing methodology
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
useLocation.mockImplementation(() => newLocation);
useLocationMock.mockClear();
useLocationMock.mockImplementation(() => newLocation);

rerender(<ScrollToTop />);
expect(window.scrollTo).toHaveBeenCalled();
});
});