Skip to content

Commit 8da1ff9

Browse files
chore: add test for api selector hooks
1 parent 4156bc7 commit 8da1ff9

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

src/data/services/lms/hooks/api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from '@tanstack/react-query';
22
import { useRouteMatch } from 'react-router-dom';
3-
import { snakeCaseObject, camelCaseObject } from '@edx/frontend-platform';
3+
import { camelCaseObject } from '@edx/frontend-platform';
44
import { when } from 'jest-when';
55

66
import routes from 'routes';
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { when } from 'jest-when';
2+
import { keyStore } from '@edx/react-unit-test-utils';
3+
4+
import * as api from './api';
5+
import * as selectors from './selectors';
6+
7+
const statusData = {
8+
isLoading: 'is-loading',
9+
isFetching: 'is-fetching',
10+
isInitialLoading: 'is-initial-loading',
11+
status: 'status',
12+
error: 'error',
13+
};
14+
15+
const testValue = 'some-test-data';
16+
17+
const apiKeys = keyStore(api);
18+
const selectorKeys = keyStore(selectors);
19+
20+
const mockHook = (module, key, returnValue) => {
21+
const spy = jest.spyOn(module, key);
22+
when(spy).calledWith().mockReturnValueOnce(returnValue);
23+
};
24+
25+
describe('lms api selector hooks', () => {
26+
const mockORAConfig = (returnValue) => {
27+
mockHook(api, apiKeys.useORAConfig, returnValue);
28+
};
29+
const mockORAData = (returnValue) => {
30+
mockHook(selectors, selectorKeys.useORAConfigData, returnValue);
31+
};
32+
describe('ORA Config selectors', () => {
33+
describe('useORAConfigDataStatus', () => {
34+
it('returns status data from useORAConfig call', () => {
35+
mockORAConfig({
36+
...statusData,
37+
other: 'field',
38+
and: 'another one',
39+
});
40+
expect(selectors.useORAConfigDataStatus()).toEqual(statusData);
41+
});
42+
});
43+
describe('useIsORAConfigLoaded', () => {
44+
it('returns true if ORAConfig.status is "success"', () => {
45+
mockORAConfig({ ...statusData, status: 'success' });
46+
expect(selectors.useIsORAConfigLoaded()).toEqual(true);
47+
});
48+
it('returns false if ORAConfig.status is not "success"', () => {
49+
mockORAConfig({ ...statusData, status: 'random' });
50+
expect(selectors.useIsORAConfigLoaded()).toEqual(false);
51+
});
52+
});
53+
describe('useORAConfigData', () => {
54+
it('returns data from ORAConfig', () => {
55+
mockORAConfig({ ...statusData, data: testValue });
56+
expect(selectors.useORAConfigData()).toEqual(testValue);
57+
});
58+
});
59+
describe('useSubmissionConfig', () => {
60+
it('returns submissionConfig from ORAConfigData', () => {
61+
mockORAData({ submissionConfig: testValue });
62+
expect(selectors.useSubmissionConfig()).toEqual(testValue);
63+
});
64+
});
65+
describe('useAssessmentStepConfig', () => {
66+
it('returns assessmentSteps from ORAConfigData', () => {
67+
mockORAData({ assessmentSteps: testValue });
68+
expect(selectors.useAssessmentStepConfig()).toEqual(testValue);
69+
});
70+
});
71+
describe('useRubricConfig', () => {
72+
it('returns rubric from ORAConfigData', () => {
73+
mockORAData({ rubric: testValue });
74+
expect(selectors.useRubricConfig()).toEqual(testValue);
75+
});
76+
});
77+
describe('useLeaderboardConfig', () => {
78+
it('returns rubric from ORAConfigData', () => {
79+
mockORAData({ leaderboardConfig: testValue });
80+
expect(selectors.useLeaderboardConfig()).toEqual(testValue);
81+
});
82+
});
83+
});
84+
describe('Page Data selectors', () => {
85+
const mockPageDataQuery = (returnValue) => {
86+
mockHook(api, apiKeys.usePageData, returnValue);
87+
};
88+
const mockPageData = (returnValue) => {
89+
mockHook(selectors, selectorKeys.usePageData, returnValue);
90+
};
91+
describe('usePageDataStatus', () => {
92+
it('returns status data from useORAConfig call', () => {
93+
mockPageDataQuery({
94+
...statusData,
95+
other: 'field',
96+
and: 'another one',
97+
});
98+
expect(selectors.usePageDataStatus()).toEqual(statusData);
99+
});
100+
});
101+
describe('useIsPageDataLoaded', () => {
102+
it('returns true if PageData.status is "success"', () => {
103+
mockPageDataQuery({ ...statusData, status: 'success' });
104+
expect(selectors.useIsPageDataLoaded()).toEqual(true);
105+
});
106+
it('returns false if PageData.status is not "success"', () => {
107+
mockPageDataQuery({ ...statusData, status: 'random' });
108+
expect(selectors.useIsPageDataLoaded()).toEqual(false);
109+
});
110+
});
111+
describe('usePageData', () => {
112+
it('returns data from PageData query', () => {
113+
mockPageDataQuery({ ...statusData, data: testValue });
114+
expect(selectors.usePageData()).toEqual(testValue);
115+
});
116+
});
117+
describe('useSubmissionTeamInfo', () => {
118+
it('returns submission team info from PageData', () => {
119+
mockPageData({ submission: { teamInfo: testValue } });
120+
expect(selectors.useSubmissionTeamInfo()).toEqual(testValue);
121+
});
122+
});
123+
describe('useSubmissionStatus', () => {
124+
it('returns hasCancelled, hasReceivedGraded, and hasSubmitted', () => {
125+
const submissionStatus = {
126+
hasCancelled: 'has-cancelled',
127+
hasReceivedGrade: 'has-received-grade',
128+
hasSubmitted: 'has-submitted',
129+
};
130+
mockPageData({ submission: { ...submissionStatus, other: 'fields' } });
131+
expect(selectors.useSubmissionStatus()).toEqual(submissionStatus);
132+
});
133+
});
134+
describe('useSubmissionResponse', () => {
135+
it('returns submission response from PageData', () => {
136+
mockPageData({ submission: { response: testValue } });
137+
expect(selectors.useSubmissionResponse()).toEqual(testValue);
138+
})
139+
});
140+
});
141+
});

0 commit comments

Comments
 (0)