Skip to content

Commit 6942f86

Browse files
committed
refactor: replace axios with fetch api Refs: RATY-299
1 parent 02a43be commit 6942f86

20 files changed

Lines changed: 498 additions & 531 deletions

File tree

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
"@tanstack/react-query": "^5.28.0",
2525
"@types/lodash": "^4.17.24",
2626
"@types/react-scroll": "^1.8.10",
27-
"axios": "^1.15.0",
28-
"axios-logger": "^2.8.0",
2927
"classnames": "^2.5.1",
3028
"date-fns-tz": "^2.0.1",
3129
"dotenv": "^16.4.7",

src/__tests__/NextAuth.test.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
import '../tests/mockNextAuth';
44

5-
import mockAxios from 'axios';
65
import { Session, User } from 'next-auth';
76

87
import { SIGNOUT_REDIRECT } from '../constants';
@@ -89,21 +88,30 @@ const testApiTokenUrl =
8988
'https://tunnistus.hel.fi/auth/realms/helsinki-tunnistus/protocol/openid-connect/token';
9089

9190
const mockTokenResonses = () => {
92-
global.fetch = vi.fn(() =>
93-
Promise.resolve({
94-
json: () => Promise.resolve({ token_endpoint: testTokenUrl }),
95-
})
96-
) as any;
97-
mockAxios.post = vi.fn().mockImplementation(async (url) => {
98-
switch (url) {
99-
case testTokenUrl:
100-
return { data: refreshResponse };
101-
case testApiTokenUrl:
102-
return {
103-
data: { ...apiTokenResponse, access_token: 'refreshed-api-token' },
104-
};
91+
global.fetch = vi.fn().mockImplementation(async (url: string) => {
92+
if (url.includes('.well-known')) {
93+
return {
94+
ok: true,
95+
json: () => Promise.resolve({ token_endpoint: testTokenUrl }),
96+
};
10597
}
106-
});
98+
if (url === testTokenUrl) {
99+
return {
100+
ok: true,
101+
json: () => Promise.resolve(refreshResponse),
102+
};
103+
}
104+
if (url === testApiTokenUrl) {
105+
return {
106+
ok: true,
107+
json: () =>
108+
Promise.resolve({
109+
...apiTokenResponse,
110+
access_token: 'refreshed-api-token',
111+
}),
112+
};
113+
}
114+
}) as any;
107115
};
108116

109117
describe('getApiAccessTokens function', () => {
@@ -114,17 +122,21 @@ describe('getApiAccessTokens function', () => {
114122
});
115123

116124
test("should throw an error in api-tokens endpoint doesn't return api tokens", async () => {
117-
vi.spyOn(mockAxios, 'post').mockResolvedValue({ data: {} });
125+
global.fetch = vi.fn().mockResolvedValue({
126+
ok: true,
127+
json: () => Promise.resolve({}),
128+
}) as any;
118129

119130
await expect(
120131
async () => await getApiAccessTokens(accessToken)
121132
).rejects.toThrow('No api-tokens present');
122133
});
123134

124135
test('should return api tokens', async () => {
125-
vi.spyOn(mockAxios, 'post').mockResolvedValue({
126-
data: { ...apiTokenResponse },
127-
});
136+
global.fetch = vi.fn().mockResolvedValue({
137+
ok: true,
138+
json: () => Promise.resolve({ ...apiTokenResponse }),
139+
}) as any;
128140

129141
const apiTokens = await getApiAccessTokens(accessToken);
130142
await expect(apiTokens).toEqual({ linkedevents: 'api-token' });
@@ -140,7 +152,9 @@ describe('refreshAccessToken function', () => {
140152

141153
test('should return error if request to refresh access token fails', async () => {
142154
console.error = vi.fn();
143-
vi.spyOn(mockAxios, 'post').mockResolvedValue({ data: undefined });
155+
global.fetch = vi
156+
.fn()
157+
.mockRejectedValue(new Error('request failed')) as any;
144158

145159
const { error } = await refreshAccessToken({ ...token, refreshToken });
146160
expect(error).toBe('RefreshAccessTokenError');
@@ -168,9 +182,10 @@ describe('jwtCallback function', () => {
168182
test('should return session after initial sign in', async () => {
169183
vi.setSystemTime(new Date('2023-01-01'));
170184

171-
vi.spyOn(mockAxios, 'post').mockResolvedValue({
172-
data: { ...apiTokenResponse },
173-
});
185+
global.fetch = vi.fn().mockResolvedValue({
186+
ok: true,
187+
json: () => Promise.resolve({ ...apiTokenResponse }),
188+
}) as any;
174189

175190
const jwt = await jwtCallback({ token, user, account });
176191

@@ -191,10 +206,6 @@ describe('jwtCallback function', () => {
191206
test('should return original token if token is not expired', async () => {
192207
vi.setSystemTime(new Date('2023-01-01'));
193208

194-
vi.spyOn(mockAxios, 'post').mockResolvedValue({
195-
data: { ...apiTokenResponse },
196-
});
197-
198209
const jwt = await jwtCallback({ token });
199210

200211
expect(jwt).toEqual(token);
@@ -203,7 +214,9 @@ describe('jwtCallback function', () => {
203214
test('should return null if refreshing token fails', async () => {
204215
vi.setSystemTime(new Date('2023-01-01'));
205216

206-
vi.spyOn(mockAxios, 'post').mockRejectedValue(new Error('refresh failed'));
217+
global.fetch = vi
218+
.fn()
219+
.mockRejectedValue(new Error('refresh failed')) as any;
207220

208221
const jwt = await jwtCallback({
209222
token: { ...token, accessTokenExpires: 1662531200000 },

src/domain/app/axios/__tests__/axiosClient.test.ts

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/domain/app/axios/axiosClient.ts

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)