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

Use mock base API URL in unit tests for the back button #168

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 16 additions & 6 deletions app/src/components/generic/backButton/BackButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ import { routes } from '../../../types/generic/routes';
import { endpoints } from '../../../types/generic/endpoints';
import { useBaseAPIUrl } from '../../../providers/configProvider/ConfigProvider';


jest.mock('../../../providers/configProvider/ConfigProvider');
const mockUseBaseAPIUrl = useBaseAPIUrl as jest.Mock;
const testUrl = '/test'

describe('BackButton', () => {

beforeEach(() => {
process.env.REACT_APP_ENVIRONMENT = 'jest';
mockUseBaseAPIUrl.mockReturnValue(testUrl);
});
afterEach(() => {
jest.clearAllMocks();
});

it('navigates to previous page when clicking the back buttonand not on the search pages', async () => {
const history = createMemoryHistory({
initialEntries: ['/', '/example'],
Expand All @@ -28,8 +42,6 @@ describe('BackButton', () => {
});

it('calls the login handler when clicking the back button on the upload search page', async () => {
const test_location_prefix = useBaseAPIUrl();

const history = createMemoryHistory({
initialEntries: ['/', '/example'],
initialIndex: 1,
Expand All @@ -53,13 +65,11 @@ describe('BackButton', () => {
userEvent.click(screen.getByText('Back'));

await waitFor(() => {
expect(window.location.replace).toBeCalledWith(test_location_prefix + endpoints.LOGIN);
expect(window.location.replace).toBeCalledWith(testUrl + endpoints.LOGIN);
});
});

it('calls the login handler when clicking the back button on the download search page', async () => {
const test_location_prefix = useBaseAPIUrl();

const history = createMemoryHistory({
initialEntries: ['/', '/example'],
initialIndex: 1,
Expand All @@ -83,7 +93,7 @@ describe('BackButton', () => {
userEvent.click(screen.getByText('Back'));

await waitFor(() => {
expect(window.location.replace).toBeCalledWith(test_location_prefix + endpoints.LOGIN);
expect(window.location.replace).toBeCalledWith(testUrl + endpoints.LOGIN);
});
});
});
Loading