From b95d8498d03eb5dd0f12525e97f3ae7d3c231b41 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 28 Nov 2023 09:55:29 +0000 Subject: [PATCH] Use mock base API URL --- .../generic/backButton/BackButton.test.tsx | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/components/generic/backButton/BackButton.test.tsx b/app/src/components/generic/backButton/BackButton.test.tsx index 45afd055b..a1758a574 100644 --- a/app/src/components/generic/backButton/BackButton.test.tsx +++ b/app/src/components/generic/backButton/BackButton.test.tsx @@ -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'], @@ -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, @@ -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, @@ -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); }); }); });