|
| 1 | +import { useQuery } from '@tanstack/react-query'; |
| 2 | +import { useRouteMatch } from 'react-router-dom'; |
| 3 | +import { snakeCaseObject, camelCaseObject } from '@edx/frontend-platform'; |
| 4 | +import { when } from 'jest-when'; |
| 5 | + |
| 6 | +import routes from 'routes'; |
| 7 | +import * as types from '../types'; |
| 8 | +import { queryKeys } from '../constants'; |
| 9 | +import fakeData from '../fakeData'; |
| 10 | + |
| 11 | +import { useORAConfig, usePageData } from './api'; |
| 12 | + |
| 13 | +jest.mock('@tanstack/react-query', () => ({ useQuery: jest.fn() })); |
| 14 | + |
| 15 | +jest.mock('react-router-dom', () => ({ useRouteMatch: jest.fn() })); |
| 16 | + |
| 17 | +interface QueryFn { (): string } |
| 18 | +interface QueryArgs { queryKey: string, queryFn: QueryFn } |
| 19 | + |
| 20 | +interface MockORAQuery extends QueryArgs { data: types.ORAConfig } |
| 21 | +interface MockUseORAQuery { (QueryArgs): MockORAQuery } |
| 22 | +interface MockORAUseConfigHook { (): MockORAQuery } |
| 23 | + |
| 24 | +interface MockPageDataQuery extends QueryArgs { data: types.PageData } |
| 25 | +interface MockUsePageDataQuery { (QueryArgs): MockPageDataQuery } |
| 26 | +interface MockPageDataUseConfigHook { (): MockPageDataQuery } |
| 27 | + |
| 28 | +let out; |
| 29 | +describe('lms api hooks', () => { |
| 30 | + describe('useORAConfig', () => { |
| 31 | + const mockUseQuery = (hasData: boolean): MockUseORAQuery => ({ queryKey, queryFn }) => ({ |
| 32 | + data: hasData ? camelCaseObject(fakeData.oraConfig.assessmentText) : undefined, |
| 33 | + queryKey, |
| 34 | + queryFn, |
| 35 | + }); |
| 36 | + |
| 37 | + const mockUseQueryForORA = (hasData) => { |
| 38 | + when(useQuery) |
| 39 | + .calledWith(expect.objectContaining({ queryKey: [queryKeys.oraConfig] })) |
| 40 | + .mockImplementationOnce(mockUseQuery(hasData)); |
| 41 | + }; |
| 42 | + |
| 43 | + const testUseORAConfig = useORAConfig as unknown as MockORAUseConfigHook; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + mockUseQueryForORA(true); |
| 47 | + out = testUseORAConfig(); |
| 48 | + }); |
| 49 | + it('initializes query with oraConfig queryKey', () => { |
| 50 | + expect(out.queryKey).toEqual([queryKeys.oraConfig]); |
| 51 | + }); |
| 52 | + it('initializes query with promise pointing to assessment text', async () => { |
| 53 | + const response = await out.queryFn(); |
| 54 | + expect(response).toEqual(fakeData.oraConfig.assessmentText); |
| 55 | + }); |
| 56 | + it('returns camelCase object from data if data has been returned', () => { |
| 57 | + expect(out.data).toEqual(camelCaseObject(fakeData.oraConfig.assessmentText)); |
| 58 | + }); |
| 59 | + it('returns empty object from data if data has not been returned', () => { |
| 60 | + mockUseQueryForORA(false); |
| 61 | + out = testUseORAConfig(); |
| 62 | + expect(out.data).toEqual({}); |
| 63 | + }); |
| 64 | + }); |
| 65 | + describe('usePageData', () => { |
| 66 | + const mockUseQuery = (data?: types.PageData): MockUsePageDataQuery => ({ queryKey, queryFn }) => ({ |
| 67 | + data: data ? camelCaseObject(data) : undefined, |
| 68 | + queryKey, |
| 69 | + queryFn, |
| 70 | + }); |
| 71 | + |
| 72 | + const mockUseQueryForPageData = (data, isAssessment) => { |
| 73 | + when(useQuery) |
| 74 | + .calledWith(expect.objectContaining({ queryKey: [queryKeys.pageData, isAssessment] })) |
| 75 | + .mockImplementationOnce(mockUseQuery(data)); |
| 76 | + }; |
| 77 | + |
| 78 | + const mockUseRouteMatch = (path) => { |
| 79 | + when(useRouteMatch) |
| 80 | + .calledWith() |
| 81 | + .mockReturnValueOnce({ path }); |
| 82 | + }; |
| 83 | + |
| 84 | + const testUsePageData = usePageData as unknown as MockPageDataUseConfigHook; |
| 85 | + describe('submission', () => { |
| 86 | + beforeEach(() => { |
| 87 | + mockUseRouteMatch(routes.submission); |
| 88 | + mockUseQueryForPageData(fakeData.pageData.shapes.emptySubmission, false); |
| 89 | + out = testUsePageData(); |
| 90 | + }); |
| 91 | + it('initializes query with pageData queryKey and isAssessment: false', () => { |
| 92 | + expect(out.queryKey).toEqual([queryKeys.pageData, false]); |
| 93 | + }); |
| 94 | + it('initializes query with promise pointing to empty submission page data', async () => { |
| 95 | + const response = await out.queryFn(); |
| 96 | + expect(response).toEqual(fakeData.pageData.shapes.emptySubmission); |
| 97 | + }); |
| 98 | + it('returns camelCase object from data if data has been returned', () => { |
| 99 | + expect(out.data).toEqual(camelCaseObject(fakeData.pageData.shapes.emptySubmission)); |
| 100 | + }); |
| 101 | + }); |
| 102 | + describe('assessment', () => { |
| 103 | + beforeEach(() => { |
| 104 | + mockUseRouteMatch(routes.assessment); |
| 105 | + mockUseQueryForPageData(fakeData.pageData.shapes.peerAssessment, true); |
| 106 | + out = testUsePageData(); |
| 107 | + }); |
| 108 | + it('initializes query with pageData queryKey and isAssessment: true', () => { |
| 109 | + expect(out.queryKey).toEqual([queryKeys.pageData, true]); |
| 110 | + }); |
| 111 | + it('initializes query with promise pointing to peer assessment page data', async () => { |
| 112 | + const response = await out.queryFn(); |
| 113 | + expect(response).toEqual(fakeData.pageData.shapes.peerAssessment); |
| 114 | + }); |
| 115 | + it('returns camelCase object from data if data has been returned', () => { |
| 116 | + expect(out.data).toEqual(camelCaseObject(fakeData.pageData.shapes.peerAssessment)); |
| 117 | + }); |
| 118 | + }); |
| 119 | + it('returns empty object from data if data has not been returned', () => { |
| 120 | + mockUseRouteMatch(routes.submission); |
| 121 | + mockUseQueryForPageData(undefined, false); |
| 122 | + out = testUsePageData(); |
| 123 | + expect(out.data).toEqual({}); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments