Skip to content

Commit a3e9e7d

Browse files
committed
♻️(frontend) useAddresses with new api
now that we've generated types and api client, we need to use them into our api hooks
1 parent 617dd4f commit a3e9e7d

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

src/frontend/js/components/TeacherDashboardCourseList/index.spec.tsx

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import { MemoryRouter } from 'react-router-dom';
2-
import { QueryClientProvider } from '@tanstack/react-query';
3-
import { render, screen } from '@testing-library/react';
1+
import { screen } from '@testing-library/react';
42
import fetchMock from 'fetch-mock';
5-
import { IntlProvider } from 'react-intl';
63

74
import { CourseListItem } from 'types/Joanie';
8-
import {
9-
RichieContextFactory as mockRichieContextFactory,
10-
UserFactory,
11-
} from 'utils/test/factories/richie';
12-
import JoanieSessionProvider from 'contexts/SessionContext/JoanieSessionProvider';
5+
import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie';
136
import { CourseListItemFactory } from 'utils/test/factories/joanieLegacy';
14-
import { createTestQueryClient } from 'utils/test/createTestQueryClient';
157
import { mockPaginatedResponse } from 'utils/test/mockPaginatedResponse';
168
import { expectNoSpinner } from 'utils/test/expectSpinner';
179
import { PER_PAGE } from 'settings';
10+
import { setupJoanieSession } from 'utils/test/wrappers/JoanieAppWrapper';
11+
import { render } from 'utils/test/render';
1812
import TeacherDashboardCourseList from '.';
1913

2014
jest.mock('utils/context', () => ({
@@ -40,14 +34,9 @@ jest.mock('hooks/useIntersectionObserver', () => ({
4034
describe('components/TeacherDashboardCourseList', () => {
4135
const perPage = PER_PAGE.useCourseProductUnion;
4236
let nbApiCalls: number;
37+
const joanieSessionData = setupJoanieSession();
4338
beforeEach(() => {
44-
fetchMock.get('https://joanie.endpoint/api/v1.0/orders/', [], { overwriteRoutes: true });
45-
fetchMock.get('https://joanie.endpoint/api/v1.0/credit-cards/', [], { overwriteRoutes: true });
46-
fetchMock.get('https://joanie.endpoint/api/v1.0/addresses/', [], { overwriteRoutes: true });
47-
nbApiCalls = 3;
48-
});
49-
afterEach(() => {
50-
fetchMock.restore();
39+
nbApiCalls = joanieSessionData.nbSessionApiRequest;
5140
});
5241

5342
it('should render', async () => {
@@ -72,18 +61,7 @@ describe('components/TeacherDashboardCourseList', () => {
7261
mockPaginatedResponse([productCooking, productDancing], 15, false),
7362
);
7463

75-
const user = UserFactory().one();
76-
render(
77-
<IntlProvider locale="en">
78-
<QueryClientProvider client={createTestQueryClient({ user })}>
79-
<JoanieSessionProvider>
80-
<MemoryRouter>
81-
<TeacherDashboardCourseList titleTranslated="TeacherDashboardCourseList test title" />
82-
</MemoryRouter>
83-
</JoanieSessionProvider>
84-
</QueryClientProvider>
85-
</IntlProvider>,
86-
);
64+
render(<TeacherDashboardCourseList titleTranslated="TeacherDashboardCourseList test title" />);
8765
nbApiCalls += 1; // courses api call
8866
nbApiCalls += 1; // course-product-relations api call
8967

@@ -131,31 +109,21 @@ describe('components/TeacherDashboardCourseList', () => {
131109
},
132110
);
133111

134-
const user = UserFactory().one();
135-
render(
136-
<IntlProvider locale="en">
137-
<QueryClientProvider client={createTestQueryClient({ user })}>
138-
<JoanieSessionProvider>
139-
<MemoryRouter>
140-
<TeacherDashboardCourseList titleTranslated="TeacherDashboardCourseList test title" />
141-
</MemoryRouter>
142-
</JoanieSessionProvider>
143-
</QueryClientProvider>
144-
</IntlProvider>,
145-
);
112+
render(<TeacherDashboardCourseList titleTranslated="TeacherDashboardCourseList test title" />);
146113
nbApiCalls += 1; // courses api call
147114
nbApiCalls += 1; // course-product-relations api call
115+
await expectNoSpinner('Loading courses...');
148116

149117
expect(await screen.getByRole('heading', { name: /TeacherDashboardCourseList test title/ }));
150118

151119
const calledUrls = fetchMock.calls().map((call) => call[0]);
152-
expect(calledUrls).toHaveLength(nbApiCalls);
153120
expect(calledUrls).toContain(
154121
`https://joanie.endpoint/api/v1.0/courses/?has_listed_course_runs=true&page=1&page_size=${perPage}`,
155122
);
156123
expect(calledUrls).toContain(
157124
`https://joanie.endpoint/api/v1.0/course-product-relations/?product_type=credential&page=1&page_size=${perPage}`,
158125
);
126+
expect(calledUrls).toHaveLength(nbApiCalls);
159127

160128
expect(await screen.findByText('You have no courses yet.')).toBeInTheDocument();
161129
});

src/frontend/js/hooks/useAddresses.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { defineMessages } from 'react-intl';
22
import { MutateOptions } from '@tanstack/react-query';
3-
import { useJoanieApi } from 'contexts/JoanieApiContext';
4-
import { Address, AddressCreationPayload, API } from 'types/Joanie';
3+
import { getApiClientJoanie } from 'api/joanie/client';
4+
import type { Address } from 'api/joanie/gen';
5+
import { PatchedAddressRequest } from 'api/joanie/gen';
6+
import { ApiResourceInterface, PaginatedResourceQuery } from 'types/Joanie';
7+
import { Maybe } from 'types/utils';
58
import { HttpError } from 'utils/errors/HttpError';
6-
import { ResourcesQuery, useResource, useResources, UseResourcesProps } from './useResources';
9+
10+
import { useResource, useResources, UseResourcesProps } from './useResources';
711

812
const messages = defineMessages({
913
errorUpdate: {
@@ -33,15 +37,38 @@ const messages = defineMessages({
3337
},
3438
});
3539

36-
export type AddressesMutateOptions = MutateOptions<Address, HttpError, AddressCreationPayload>;
40+
export type AddressesMutateOptions = MutateOptions<PatchedAddressRequest, HttpError, Address>;
41+
42+
const apiInterface: () => ApiResourceInterface<Address, PaginatedResourceQuery> = () => {
43+
const joanieClient = getApiClientJoanie();
44+
return {
45+
get: (resourceQuery?: Maybe<PaginatedResourceQuery>) => {
46+
const { id, ...filters } = resourceQuery || {};
47+
if (id) {
48+
return joanieClient.addresses.addressesRetrieve(id);
49+
}
50+
return joanieClient.addresses.addressesList(filters.page, filters.page_size);
51+
},
52+
create: (data: Address) => {
53+
return joanieClient.addresses.addressesCreate(data);
54+
},
55+
update: (data: PatchedAddressRequest & { id: string }) => {
56+
const { id, ...updateData } = data;
57+
return joanieClient.addresses.addressesPartialUpdate(id, updateData);
58+
},
59+
delete: (id: string) => {
60+
return joanieClient.addresses.addressesDestroy(id);
61+
},
62+
};
63+
};
3764

3865
/**
3966
* Joanie Api hook to retrieve/create/update/delete addresses
4067
* owned by the authenticated user.
4168
*/
42-
const props: UseResourcesProps<Address, ResourcesQuery, API['user']['addresses']> = {
69+
const props: UseResourcesProps<Address, PaginatedResourceQuery, ReturnType<typeof apiInterface>> = {
4370
queryKey: ['addresses'],
44-
apiInterface: () => useJoanieApi().user.addresses,
71+
apiInterface,
4572
omniscient: true,
4673
session: true,
4774
messages,

0 commit comments

Comments
 (0)