-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandlers.ts
257 lines (249 loc) · 9.01 KB
/
handlers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import {http, HttpResponse, JsonBodyType} from "msw";
import {
buildMockUserSummary,
mockAssignmentsGroup2,
mockAssignmentsGroup6, mockFragment,
mockGameboards,
mockGroups,
mockMyAssignments,
mockNewsPods, buildMockPage,
mockQuizAssignments, mockSchool,
mockSetAssignments,
mockUser,
mockUserAuthSettings,
mockUserPreferences,
mockRegressionTestQuestions,
mockQuestionFinderResults,
mockConceptPage
} from "./data";
import {API_PATH, PATHS} from "../app/services";
import {produce} from "immer";
import {School} from "../IsaacAppTypes";
export const handlers = [
http.get(API_PATH + `${PATHS.GAMEBOARD}/user_gameboards`, ({request}) => {
const url = new URL(request.url);
const startIndexStr = url.searchParams.get("start_index");
const startIndex = (startIndexStr && parseInt(startIndexStr)) || 0;
const limitStr = url.searchParams.get("limit");
const limit = (limitStr && parseInt(limitStr)) || mockGameboards.totalResults;
const limitedGameboards = produce(mockGameboards, g => {
if (startIndex === 0 && limitStr === "ALL") return g;
g.results = g.results.slice(startIndex, Math.min(startIndex + limit, mockGameboards.totalResults));
g.totalNotStarted = g.results.length;
g.totalResults = g.results.length;
});
return HttpResponse.json(limitedGameboards, {
status: 200,
});
}),
http.get(API_PATH + "/groups", ({request}) => {
const url = new URL(request.url);
const archived = url.searchParams.get("archived_groups_only") === "true";
const groups = mockGroups.filter(g => g.archived === archived);
return HttpResponse.json(groups, {
status: 200,
});
}),
http.get(API_PATH + "/groups/:groupId/membership", () => {
// TODO could get members from mock data if groupId refers to a known mock group
return HttpResponse.json([], {
status: 200,
});
}),
http.get(API_PATH + "/assignments", () => {
return HttpResponse.json(mockMyAssignments, {
status: 200,
});
}),
http.get(API_PATH + "/quiz/assignments", () => {
return HttpResponse.json(mockQuizAssignments, {
status: 200,
});
}),
http.get(API_PATH + "/assignments/assign/:assignmentId", ({params}) => {
const {assignmentId: _assignmentId} = params;
const assignmentId = parseInt(_assignmentId as string);
// FIXME augment the returned assignment like in the API
const assignments = mockSetAssignments.filter(a => a.id === assignmentId);
if (assignments.length === 1) {
return HttpResponse.json(assignments[0]);
}
// FIXME this is probably the wrong format for errors
return HttpResponse.json({error: `Assignment with id ${_assignmentId} not found.`}, {
status: 404,
});
}),
http.get(API_PATH + "/assignments/assign", ({request}) => {
const url = new URL(request.url);
const groupIdStr = url.searchParams.get("group");
const groupId = groupIdStr && parseInt(groupIdStr) || undefined;
let setAssignments;
switch (groupId) {
case 2:
setAssignments = mockAssignmentsGroup2;
break;
case 6:
setAssignments = mockAssignmentsGroup6;
break;
default:
setAssignments = mockSetAssignments;
}
return HttpResponse.json(setAssignments, {
status: 200,
});
}),
http.get(API_PATH + "/users/current_user", () => {
return HttpResponse.json(mockUser, {
status: 200,
});
}),
http.post(API_PATH + "/auth/logout", () => {
return HttpResponse.json(null, {
status: 200,
});
}),
http.get(API_PATH + "/users/user_preferences", () => {
return HttpResponse.json(mockUserPreferences, {
status: 200,
});
}),
http.get(API_PATH + "/users/current_user/snapshot", () => {
return HttpResponse.json({
dailyStreakRecord: {currentStreak: 0, largestStreak: 0, currentActivity: 0},
weeklyStreakRecord: {currentStreak: 0, currentActivity: 0, largestWeeklyStreak: 0}
}, {
status: 200,
});
}),
http.get(API_PATH + "/auth/user_authentication_settings", () => {
return HttpResponse.json(mockUserAuthSettings, {
status: 200,
});
}),
http.get(API_PATH + "/info/segue_environment", () => {
return HttpResponse.json({segueEnvironment: "DEV"}, {
status: 200,
});
}),
http.get(API_PATH + "/notifications", () => {
return HttpResponse.json([], {
status: 200,
});
}),
http.get(API_PATH + "/pages/pods/:tag/0", ({request, params}) => {
const {tag} = params;
const podsFilteredByTag = produce(mockNewsPods, pods => {
pods.results = pods.results.filter(p => p.tags.includes(tag as string));
pods.totalResults = pods.results.length;
});
return HttpResponse.json(podsFilteredByTag, {
status: 200,
});
}),
http.get(API_PATH + "/pages/fragments/:fragmentId", ({params}) => {
const {fragmentId} = params;
return HttpResponse.json(mockFragment(fragmentId as string), {
status: 200,
});
}),
http.get(API_PATH + "/pages/questions/_regression_test_", () => {
return HttpResponse.json(mockRegressionTestQuestions, {
status: 200,
});
}),
http.get(API_PATH + "/pages/questions/", () => {
return HttpResponse.json(mockQuestionFinderResults, {
status: 200,
});
}),
http.get(API_PATH + "/pages/concepts/_mock_concept_page_", () => {
return HttpResponse.json(mockConceptPage, {
status: 200,
});
}),
http.get(API_PATH + "/pages/:pageId", ({params}) => {
const {pageId} = params;
return HttpResponse.json(buildMockPage(pageId as string), {
status: 200,
});
}),
http.get(API_PATH + "/glossary/terms", () => {
return HttpResponse.json({results: [], totalResults: 0}, {
status: 200,
});
}),
http.post(API_PATH + "/log", async ({request}) => {
const json = await request.json();
console.info("Log event: ", json);
return HttpResponse.json(null, {status: 200,}
);
}),
http.get(API_PATH + "/user-alerts", () => {
return HttpResponse.json({}, {
status: 200,
});
}),
http.get(API_PATH + "/events", () => {
return HttpResponse.json({results: [], totalResults: 0}, {
status: 200,
});
}),
http.get(API_PATH + "/users/school_lookup", ({request}) => {
// TODO should we actually be using query params here?
const url = new URL(request.url);
const user_ids = url.searchParams.get("user_ids");
// Turn into map from user id to school
const schools: {[userId: number]: School} = (user_ids as string).split(",").reduce((acc: any, userId: string) => {
acc[userId] = mockSchool;
return acc;
}, {});
return HttpResponse.json(schools, {
status: 200,
});
}),
];
// --- Extra handler builder functions ---
export const handlerThatReturns = (options?: {data?: any, status?: number}) => jest.fn(() => {
if (!options?.data) {
return HttpResponse.json(null, {
status: (options?.status ?? 200)
});
}
return HttpResponse.json(options.data, {
status: (options?.status ?? 200)
});
});
export const buildAuthTokenHandler = (newGroup: any, token: string) => jest.fn(() => {
return HttpResponse.json({
token,
ownerUserId: newGroup.ownerId,
groupId: newGroup.id
}, {
status: 200,
});
});
export const buildNewManagerHandler = (groupToAddManagerTo: any, newManager: any) => jest.fn(() => {
return HttpResponse.json({
...groupToAddManagerTo,
additionalManagers: [buildMockUserSummary(newManager, true)]
}, {
status: 200,
});
});
export const buildGroupHandler = (groups?: any[]) => jest.fn(({request}) => {
const url = new URL(request.url);
const archived = url.searchParams.get("archived_groups_only") === "true";
const filteredGroups = (groups ?? mockGroups).filter(g => g.archived === archived);
return HttpResponse.json(filteredGroups, {
status: 200,
});
});
export const buildFunctionHandler = <T extends string, V extends JsonBodyType>(path: string, keys: T[], fn: (p: Record<T, string>) => V) => {
return http.get(API_PATH + path, async ({ request }) => {
const params = keys.reduce(
(acc, key) => Object.assign(acc, {[key]: new URL(request.url).searchParams.get(key)}),
{} as Record<T, string>
);
return HttpResponse.json(fn(params), { status: 200, });
});
};