Skip to content

Commit

Permalink
chore: added test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinan029 committed Sep 25, 2024
1 parent 8d0b9d7 commit 21bf852
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@fortawesome/free-solid-svg-icons": "5.15.1",
"@fortawesome/react-fontawesome": "^0.1.14",
"@openedx/paragon": "21.13.1",
"axios": "^1.7.7",
"babel-polyfill": "6.26.0",
"classnames": "2.2.6",
"dayjs": "1.11.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const CustomerPlanContainer = ({
slug,
activeSubsidies,
activeSubscriptions,
inactivePolicies,
inactiveSubsidies,
inactiveSubscriptions,
isLoading,
}) => {
const [showInactive, setShowInactive] = useState(false);
const countOfActivePlans = activeSubscriptions.length + activePolicies.length;
const countOfInactivePlans = inactiveSubscriptions.length + inactivePolicies.length;
const countOfActivePlans = activeSubscriptions.length + activeSubsidies.length;
const countOfInactivePlans = inactiveSubscriptions.length + activeSubsidies.length;
const countOfAllPlans = countOfActivePlans + countOfInactivePlans;
useEffect(() => {
if (!countOfActivePlans && countOfAllPlans) {
Expand Down Expand Up @@ -82,7 +82,7 @@ CustomerPlanContainer.propTypes = {
expirationDate: PropTypes.string.isRequired,
created: PropTypes.string.isRequired,
})).isRequired,
inactivePolicies: PropTypes.arrayOf(PropTypes.shape({
inactiveSubsidies: PropTypes.arrayOf(PropTypes.shape({
uuid: PropTypes.string.isRequired,
activeDatetime: PropTypes.string.isRequired,
expirationDatetime: PropTypes.string.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const useAllAssociatedPlans = (enterpriseId) => {
const [isLoading, setIsLoading] = useState(true);
const [inactiveSubscriptions, setInactiveSubscriptions] = useState([]);
const [activeSubscriptions, setActiveSubscriptions] = useState([]);
const [activePolicies, setActivePolicies] = useState([]);
const [inactivePolicies, setInactivePolicies] = useState([]);
const [activeSubsidies, setActiveSubsidies] = useState([]);
const [inactiveSubsidies, setInactiveSubsidies] = useState([]);

const fetchData = useCallback(
async () => {
Expand Down Expand Up @@ -42,7 +42,7 @@ const useAllAssociatedPlans = (enterpriseId) => {
return {
activeSubsidies,
activeSubscriptions,
inactivePolicies,
inactiveSubsidies,
inactiveSubscriptions,
isLoading,
};
Expand Down
15 changes: 15 additions & 0 deletions src/Configuration/Customers/data/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { logError } from '@edx/frontend-platform/logging';
import {
getEnterpriseOffers,
getCouponOrders,
Expand All @@ -12,6 +13,11 @@ jest.mock('@edx/frontend-platform/auth', () => ({
getAuthenticatedHttpClient: jest.fn(),
}));

jest.mock('@edx/frontend-platform/logging', () => ({
...jest.requireActual('@edx/frontend-platform/logging'),
logError: jest.fn(),
}));

const TEST_ENTERPRISE_UUID = 'test-uuid';

describe('getEnterpriseOffers', () => {
Expand Down Expand Up @@ -58,6 +64,15 @@ describe('getSubsidies', () => {
const results = await getSubsidies(TEST_ENTERPRISE_UUID);
expect(results).toEqual(subsidiesResults.data.results);
});

it('returns error', async () => {
getAuthenticatedHttpClient.mockImplementation(() => ({
get: jest.fn(() => Promise.reject(new Error('Error fetching data'))),
}));
const results = await getSubsidies(TEST_ENTERPRISE_UUID);
expect(results).toEqual([]);
expect(logError).toHaveBeenCalled();
});
});

describe('getCouponOrders', () => {
Expand Down
85 changes: 85 additions & 0 deletions src/data/services/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

import fetchPaginatedData from '../utils';

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

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

describe('fetchPaginatedData', () => {
const EXAMPLE_ENDPOINT = 'http://example.com/api/v1/data';

beforeEach(() => {
jest.clearAllMocks();
});

it('returns empty data results', async () => {
axiosMock.onGet(EXAMPLE_ENDPOINT).reply(200, {
count: 0,
prev: null,
next: null,
num_pages: 0,
results: [],
});
const result = await fetchPaginatedData(EXAMPLE_ENDPOINT);
expect(result).toEqual({
results: [],
response: {
count: 0,
prev: null,
next: null,
numPages: 0,
results: [],
},
});
});

it('traverses pagination', async () => {
const urlFirstPage = `${EXAMPLE_ENDPOINT}?page=1`;
const urlSecondPage = `${EXAMPLE_ENDPOINT}?page=2`;
const mockResult = {
uuid: uuidv4(),
};
const mockSecondResult = {
uuid: uuidv4(),
};
axiosMock.onGet(urlFirstPage).reply(200, {
count: 2,
prev: null,
next: urlSecondPage,
num_pages: 2,
results: [mockResult],
});
axiosMock.onGet(urlSecondPage).reply(200, {
count: 2,
prev: null,
next: null,
num_pages: 2,
results: [mockSecondResult],
enterprise_features: {
feature_a: true,
},
});
const result = await fetchPaginatedData(urlFirstPage);
expect(result).toEqual({
results: [mockResult, mockSecondResult],
response: {
count: 2,
prev: null,
next: null,
numPages: 2,
results: [mockSecondResult],
enterpriseFeatures: {
featureA: true,
},
},
});
});
});

0 comments on commit 21bf852

Please sign in to comment.