Skip to content

Commit 142b09d

Browse files
refactor: use useMatch instead of matchPath
1 parent 01f579c commit 142b09d

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useQuery } from '@tanstack/react-query';
2-
import { matchPath, useLocation } from 'react-router-dom';
2+
import { useMatch } from 'react-router-dom';
33
import { camelCaseObject } from '@edx/frontend-platform';
44
import { when } from 'jest-when';
55

@@ -12,7 +12,7 @@ import { useORAConfig, usePageData } from './api';
1212

1313
jest.mock('@tanstack/react-query', () => ({ useQuery: jest.fn() }));
1414

15-
jest.mock('react-router-dom', () => ({ matchPath: jest.fn(), useLocation: jest.fn() }));
15+
jest.mock('react-router-dom', () => ({ useMatch: jest.fn() }));
1616

1717
interface QueryFn { (): string }
1818
interface QueryArgs { queryKey: string, queryFn: QueryFn }
@@ -86,23 +86,16 @@ describe('lms api hooks', () => {
8686
.mockImplementationOnce(mockUseQuery(data));
8787
};
8888

89-
const mockMatchPath = (path) => {
90-
when(matchPath)
91-
.calledWith({ path }, path)
89+
const mockUseMatch = (path) => {
90+
when(useMatch)
91+
.calledWith(path)
9292
.mockReturnValueOnce({ pattern: { path } });
9393
};
9494

95-
const mockUseLocation = (path) => {
96-
when(useLocation)
97-
.calledWith()
98-
.mockReturnValueOnce({ pathname: path });
99-
};
100-
10195
const testUsePageData = usePageData as unknown as MockPageDataUseConfigHook;
10296
describe('submission', () => {
10397
beforeEach(() => {
104-
mockMatchPath(routes.submission);
105-
mockUseLocation(routes.submission);
98+
mockUseMatch(routes.submission);
10699
mockUseQueryForPageData(fakeData.pageData.shapes.emptySubmission, false);
107100
out = testUsePageData();
108101
});
@@ -119,8 +112,7 @@ describe('lms api hooks', () => {
119112
});
120113
describe('assessment', () => {
121114
beforeEach(() => {
122-
mockMatchPath(routes.peerAssessment);
123-
mockUseLocation(routes.peerAssessment);
115+
mockUseMatch(routes.peerAssessment);
124116
mockUseQueryForPageData(fakeData.pageData.shapes.peerAssessment, true);
125117
out = testUsePageData();
126118
});
@@ -136,8 +128,7 @@ describe('lms api hooks', () => {
136128
});
137129
});
138130
it('returns empty object from data if data has not been returned', () => {
139-
mockMatchPath(routes.submission);
140-
mockUseLocation(routes.submission);
131+
mockUseMatch(routes.submission);
141132
mockUseQueryForPageData(undefined, false);
142133
out = testUsePageData();
143134
expect(out.data).toEqual({});

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from '@tanstack/react-query';
22

3-
import { matchPath, useLocation } from 'react-router-dom';
3+
import { useMatch } from 'react-router-dom';
44
import { camelCaseObject } from '@edx/frontend-platform';
55

66
import routes from 'routes';
@@ -24,8 +24,7 @@ export const useORAConfig = (): types.QueryData<types.ORAConfig> => {
2424
};
2525

2626
export const usePageData = (): types.QueryData<types.PageData> => {
27-
const location = useLocation();
28-
const route = matchPath({ path: routes.peerAssessment }, location.pathname);
27+
const route = useMatch(routes.peerAssessment);
2928
const isAssessment = !!route && route.pattern.path === routes.peerAssessment;
3029
const returnData = isAssessment
3130
? fakeData.pageData.shapes.peerAssessment

0 commit comments

Comments
 (0)