Skip to content

Commit

Permalink
chore: Testing and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Mar 5, 2024
1 parent 576bd1d commit 73b84ed
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 126 deletions.
3 changes: 1 addition & 2 deletions src/components/app/Root.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const Root = () => {
useEffect(() => {
const timeoutId = setTimeout(() => {
const fetchersIdle = fetchers.every((f) => f.state === 'idle');
const isAuthenticatedUserHydrated = !!authenticatedUser?.profileImage;
if (navigation.state === 'idle' && fetchersIdle && !isAuthenticatedUserHydrated) {
if (navigation.state === 'idle' && fetchersIdle) {
NProgress.done();
} else {
NProgress.start();
Expand Down
2 changes: 1 addition & 1 deletion src/components/app/routes/data/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getErrorResponseStatusCode } from '../../../../utils/common';
import { SUBSIDY_REQUEST_STATE } from '../../../enterprise-subsidy-requests';
import {
determineEnterpriseCustomerUserForDisplay,
getAssignmentsByState, redirectToExternalNoticesPage,
getAssignmentsByState,
transformEnterpriseCustomer,
transformRedeemablePoliciesData,
} from './utils';
Expand Down
76 changes: 76 additions & 0 deletions src/components/app/routes/data/tests/services.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable react/jsx-filename-extension */
import { render, waitFor, screen } from '@testing-library/react';
import { useState } from 'react';
import userEvent from '@testing-library/user-event';
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { Button } from '@openedx/paragon';
import { fetchNotices } from '../services';

const APP_CONFIG = {
USE_API_CACHE: true,
DISCOVERY_API_BASE_URL: 'http://localhost:18381',
LMS_BASE_URL: 'http://localhost:18000',
};
jest.mock('@edx/frontend-platform/config', () => ({
...jest.requireActual('@edx/frontend-platform/config'),
getConfig: jest.fn(() => APP_CONFIG),
}));

jest.mock('@edx/frontend-platform/auth', () => ({
getAuthenticatedUser: jest.fn(() => ({ id: 12345 })),
getAuthenticatedHttpClient: jest.fn(),
}));

const axiosMock = new MockAdapter(axios);
getAuthenticatedHttpClient.mockReturnValue(axios);

describe('fetchNotices', () => {
const NOTICES_ENDPOINT = `${APP_CONFIG.LMS_BASE_URL }/notices/api/v1/unacknowledged`;
const ComponentWithNotices = () => {
const [output, setOuput] = useState(null);
const onClickHandler = async () => {
const apiOutput = await fetchNotices();
if (apiOutput?.results.length > 0) {
setOuput(apiOutput.results[0]);
return;
}
setOuput('No Results');
};
return (
<Button data-testid="fetchNotices" onClick={onClickHandler}>{output || 'hi'}</Button>
);
};

// Preserves original window location, and swaps it back after test is completed
const currentLocation = window.location;
beforeAll(() => {
delete window.location;
window.location = { ...currentLocation, assign: jest.fn() };
});
afterAll(() => {
window.location = currentLocation;
});
it('returns empty data results and does not assign the window location', async () => {
axiosMock.onGet(NOTICES_ENDPOINT).reply(200, { results: [] });
render(<ComponentWithNotices />);
userEvent.click(screen.getByTestId('fetchNotices'));
await waitFor(() => expect(window.location.assign).not.toHaveBeenCalled());
});
it('returns logInfo on 404', async () => {
axiosMock.onGet(NOTICES_ENDPOINT).reply(404, {});
render(<ComponentWithNotices />);
userEvent.click(screen.getByTestId('fetchNotices'));
await waitFor(() => expect(window.location.assign).not.toHaveBeenCalled());
});
it('assigns the window location on successful API response', async () => {
const currentHref = window.location.href;
axiosMock.onGet(NOTICES_ENDPOINT).reply(200, { results: [APP_CONFIG.LMS_BASE_URL] });
render(<ComponentWithNotices />);
userEvent.click(screen.getByTestId('fetchNotices'));
await waitFor(() => expect(window.location.assign).toHaveBeenCalledWith(
`${APP_CONFIG.LMS_BASE_URL }?next=${currentHref}`,
));
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformEnterpriseCustomer } from './utils';
import { transformEnterpriseCustomer } from '../utils';

const mockEnterpriseFeatures = {
'example-feature': true,
Expand Down
9 changes: 0 additions & 9 deletions src/components/app/routes/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,3 @@ export function getAssignmentsByState(assignments = []) {
hasAssignmentsForDisplay,
};
}

export function redirectToExternalNoticesPage(data) {
if (!data?.results) {
return;
}
if (data.results.length > 0) {
window.location.replace(`${data.results[0]}?next=${window.location.href}`);
}
}
32 changes: 0 additions & 32 deletions src/components/notices-provider/NoticesProvider.jsx

This file was deleted.

53 changes: 0 additions & 53 deletions src/components/notices-provider/NoticesProvider.test.jsx

This file was deleted.

25 changes: 0 additions & 25 deletions src/components/notices-provider/api.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/notices-provider/index.js

This file was deleted.

0 comments on commit 73b84ed

Please sign in to comment.