]> git.r.bdr.sh - rbdr/forum/blame_incremental - src/lib/stores/forums.test.ts
Start migration to supabase
[rbdr/forum] / src / lib / stores / forums.test.ts
... / ...
CommitLineData
1import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
2import { resolve } from 'path';
3
4import { resolveAfter } from '$lib/utils/resolve_after';
5
6const { eachLike, like } = Matchers;
7
8jest.mock('$lib/config/config.ts');
9
10import { getForum, getForums } from './forums';
11
12const internals = {
13 provider: null
14};
15
16describe('Forums store pact', () => {
17 beforeAll(async () => {
18 internals.provider = new Pact({
19 port: 1234,
20 dir: resolve(process.cwd(), 'pacts'),
21 consumer: 'ForumClient',
22 provider: 'ForumServer',
23 pactfileWriteMode: 'update'
24 });
25
26 await internals.provider.setup();
27 });
28
29 afterEach(() => internals.provider.verify());
30 afterAll(() => internals.provider.finalize());
31
32 describe("When there's data", () => {
33 describe('GetForums', () => {
34 beforeAll(async () => {
35 const forumQuery = new GraphQLInteraction()
36 .given("there's data")
37 .uponReceiving('a request to list the forums')
38 .withRequest({
39 path: '/graphql',
40 method: 'POST'
41 })
42 .withOperation('GetForums')
43 .withQuery(
44 `query GetForums {
45 forums {
46 id
47 glyph
48 label
49 position
50 __typename
51 }
52 }`
53 )
54 .withVariables({})
55 .willRespondWith({
56 status: 200,
57 headers: {
58 'Content-Type': 'application/json; charset=utf-8'
59 },
60 body: {
61 data: {
62 forums: eachLike({
63 id: like('butter'),
64 glyph: like('⌘'),
65 label: like('test_forums.butter'),
66 position: like(1)
67 })
68 }
69 }
70 });
71 return await internals.provider.addInteraction(forumQuery);
72 });
73
74 test('it returns the forums', async () => {
75 const forums = getForums();
76 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
77 let response = null;
78 forums.subscribe((forumsValue) => {
79 response = forumsValue;
80 counter();
81 });
82 expect(response.data).toBeInstanceOf(Array);
83 expect(response.data.length).toBe(0);
84 expect(response.loading).toBe(true);
85 expect(response.error).toBe(undefined);
86 await resolveAfterTwo;
87 expect(response.data).toEqual(
88 expect.arrayContaining([
89 {
90 id: 'butter',
91 glyph: '⌘',
92 label: 'test_forums.butter',
93 position: 1
94 }
95 ])
96 );
97 expect(response.loading).toBe(false);
98 expect(response.error).toBe(undefined);
99 });
100 });
101
102 describe('GetForum', () => {
103 beforeAll(async () => {
104 const forumQuery = new GraphQLInteraction()
105 .given("there's data")
106 .uponReceiving('a request to get a single forum')
107 .withRequest({
108 path: '/graphql',
109 method: 'POST'
110 })
111 .withOperation('GetForum')
112 .withQuery(
113 `query GetForum($id: ID!) {
114 forum(id: $id) {
115 id
116 glyph
117 label
118 position
119 topics {
120 id
121 title
122 updated_at
123 ttl
124 __typename
125 }
126 __typename
127 }
128 }`
129 )
130 .withVariables({
131 id: 'freezer'
132 })
133 .willRespondWith({
134 status: 200,
135 headers: {
136 'Content-Type': 'application/json; charset=utf-8'
137 },
138 body: {
139 data: {
140 forum: like({
141 id: 'freezer',
142 glyph: like('✭'),
143 label: like('test_forums.freezer'),
144 position: like(3),
145 topics: eachLike({
146 id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
147 title: like('Very pacty topic'),
148 updated_at: like(1619954611616),
149 ttl: like(3601)
150 })
151 })
152 }
153 }
154 });
155 return await internals.provider.addInteraction(forumQuery);
156 });
157
158 test('it returns the forum', async () => {
159 const forum = getForum('freezer');
160 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
161 let response = null;
162 forum.subscribe((forumsValue) => {
163 response = forumsValue;
164 counter();
165 });
166 expect(response.data).toBe(null);
167 expect(response.loading).toBe(true);
168 expect(response.error).toBe(undefined);
169 await resolveAfterTwo;
170 expect(response.data.id).toBe('freezer');
171 expect(response.data.glyph).toBe('✭');
172 expect(response.data.label).toBe('test_forums.freezer');
173 expect(response.data.position).toBe(3);
174 expect(response.data.topics).toEqual(
175 expect.arrayContaining([
176 {
177 id: '629de02c-151a-4db7-bb86-43b2add8a15a',
178 title: 'Very pacty topic',
179 updated_at: 1619954611616,
180 ttl: 3601
181 }
182 ])
183 );
184 expect(response.loading).toBe(false);
185 expect(response.error).toBe(undefined);
186 });
187 });
188 });
189
190 describe("When there's no data", () => {
191 describe('GetForums', () => {
192 beforeAll(async () => {
193 const forumQuery = new GraphQLInteraction()
194 .given("there's no data")
195 .uponReceiving('a request to list the forums')
196 .withRequest({
197 path: '/graphql',
198 method: 'POST'
199 })
200 .withOperation('GetForums')
201 .withQuery(
202 `query GetForums {
203 forums {
204 id
205 glyph
206 label
207 position
208 __typename
209 }
210 }`
211 )
212 .withVariables({})
213 .willRespondWith({
214 status: 200,
215 headers: {
216 'Content-Type': 'application/json; charset=utf-8'
217 },
218 body: {
219 data: {
220 forums: []
221 }
222 }
223 });
224 return await internals.provider.addInteraction(forumQuery);
225 });
226
227 test('it returns the forums', async () => {
228 const forums = getForums();
229 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
230 let response = null;
231 forums.subscribe((forumsValue) => {
232 response = forumsValue;
233 counter();
234 });
235 expect(response.data).toBeInstanceOf(Array);
236 expect(response.data.length).toBe(0);
237 expect(response.loading).toBe(true);
238 expect(response.error).toBe(undefined);
239 await resolveAfterTwo;
240 expect(response.data).toBeInstanceOf(Array);
241 expect(response.data.length).toBe(0);
242 expect(response.loading).toBe(false);
243 expect(response.error).toBe(undefined);
244 });
245 });
246
247 describe('GetForum', () => {
248 beforeAll(async () => {
249 const forumQuery = new GraphQLInteraction()
250 .given("there's no data")
251 .uponReceiving('a request to get a single forum')
252 .withRequest({
253 path: '/graphql',
254 method: 'POST'
255 })
256 .withOperation('GetForum')
257 .withQuery(
258 `query GetForum($id: ID!) {
259 forum(id: $id) {
260 id
261 glyph
262 label
263 position
264 topics {
265 id
266 title
267 updated_at
268 ttl
269 __typename
270 }
271 __typename
272 }
273 }`
274 )
275 .withVariables({
276 id: 'freezer'
277 })
278 .willRespondWith({
279 status: 200,
280 headers: {
281 'Content-Type': 'application/json; charset=utf-8'
282 },
283 body: {
284 data: {
285 forum: null
286 }
287 }
288 });
289 return await internals.provider.addInteraction(forumQuery);
290 });
291
292 test('it returns the forum', async () => {
293 const forum = getForum('freezer');
294 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
295 let response = null;
296 forum.subscribe((forumsValue) => {
297 response = forumsValue;
298 counter();
299 });
300 expect(response.data).toBe(null);
301 expect(response.loading).toBe(true);
302 expect(response.error).toBe(undefined);
303 await resolveAfterTwo;
304 expect(response.data).toBe(null);
305 expect(response.loading).toBe(false);
306 expect(response.error).toBe(undefined);
307 });
308 });
309 });
310
311 describe("When there's a server error", () => {
312 describe('GetForums', () => {
313 beforeAll(async () => {
314 const forumQuery = new GraphQLInteraction()
315 .given("there's a server error")
316 .uponReceiving('a request to list the forums')
317 .withRequest({
318 path: '/graphql',
319 method: 'POST'
320 })
321 .withOperation('GetForums')
322 .withQuery(
323 `query GetForums {
324 forums {
325 id
326 glyph
327 label
328 position
329 __typename
330 }
331 }`
332 )
333 .withVariables({})
334 .willRespondWith({
335 status: 500
336 });
337 return await internals.provider.addInteraction(forumQuery);
338 });
339
340 test('it returns the error', async () => {
341 const forums = getForums();
342 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
343 let response = null;
344 forums.subscribe((forumsValue) => {
345 response = forumsValue;
346 counter();
347 });
348 expect(response.data).toBeInstanceOf(Array);
349 expect(response.data.length).toBe(0);
350 expect(response.loading).toBe(true);
351 expect(response.error).toBe(undefined);
352 await resolveAfterTwo;
353 expect(response.data).toBeInstanceOf(Array);
354 expect(response.data.length).toBe(0);
355 expect(response.loading).toBe(false);
356 expect(response.error).toBeInstanceOf(Error);
357 });
358 });
359
360 describe('GetForum', () => {
361 beforeAll(async () => {
362 const forumQuery = new GraphQLInteraction()
363 .given("there's a server error")
364 .uponReceiving('a request to get a single forum')
365 .withRequest({
366 path: '/graphql',
367 method: 'POST'
368 })
369 .withOperation('GetForum')
370 .withQuery(
371 `query GetForum($id: ID!) {
372 forum(id: $id) {
373 id
374 glyph
375 label
376 position
377 topics {
378 id
379 title
380 updated_at
381 ttl
382 __typename
383 }
384 __typename
385 }
386 }`
387 )
388 .withVariables({
389 id: 'freezer'
390 })
391 .willRespondWith({
392 status: 500
393 });
394 return await internals.provider.addInteraction(forumQuery);
395 });
396
397 test('it returns the error', async () => {
398 const forum = getForum('freezer');
399 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
400 let response = null;
401 forum.subscribe((forumsValue) => {
402 response = forumsValue;
403 counter();
404 });
405 expect(response.data).toBe(null);
406 expect(response.loading).toBe(true);
407 expect(response.error).toBe(undefined);
408 await resolveAfterTwo;
409 expect(response.data).toBe(null);
410 expect(response.loading).toBe(false);
411 expect(response.error).toBeInstanceOf(Error);
412 });
413 });
414 });
415
416 describe("When there's an error in the response", () => {
417 describe('GetForums', () => {
418 beforeAll(async () => {
419 const forumQuery = new GraphQLInteraction()
420 .given("there's an error in the response")
421 .uponReceiving('a request to list the forums')
422 .withRequest({
423 path: '/graphql',
424 method: 'POST'
425 })
426 .withOperation('GetForums')
427 .withQuery(
428 `query GetForums {
429 forums {
430 id
431 glyph
432 label
433 position
434 __typename
435 }
436 }`
437 )
438 .withVariables({})
439 .willRespondWith({
440 status: 200,
441 headers: {
442 'Content-Type': 'application/json; charset=utf-8'
443 },
444 body: {
445 errors: eachLike({
446 message: like('An error occurred when fetching forums')
447 })
448 }
449 });
450 return await internals.provider.addInteraction(forumQuery);
451 });
452
453 test('it returns the error', async () => {
454 const forums = getForums();
455 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
456 let response = null;
457 forums.subscribe((forumsValue) => {
458 response = forumsValue;
459 counter();
460 });
461 expect(response.data).toBeInstanceOf(Array);
462 expect(response.data.length).toBe(0);
463 expect(response.loading).toBe(true);
464 expect(response.error).toBe(undefined);
465 await resolveAfterTwo;
466 expect(response.data).toBeInstanceOf(Array);
467 expect(response.data.length).toBe(0);
468 expect(response.loading).toBe(false);
469 expect(response.error.graphQLErrors).toEqual(
470 expect.arrayContaining([
471 {
472 message: 'An error occurred when fetching forums'
473 }
474 ])
475 );
476 });
477 });
478
479 describe('GetForum', () => {
480 beforeAll(async () => {
481 const forumQuery = new GraphQLInteraction()
482 .given("there's an error in the response")
483 .uponReceiving('a request to get a single forum')
484 .withRequest({
485 path: '/graphql',
486 method: 'POST'
487 })
488 .withOperation('GetForum')
489 .withQuery(
490 `query GetForum($id: ID!) {
491 forum(id: $id) {
492 id
493 glyph
494 label
495 position
496 topics {
497 id
498 title
499 updated_at
500 ttl
501 __typename
502 }
503 __typename
504 }
505 }`
506 )
507 .withVariables({
508 id: 'freezer'
509 })
510 .willRespondWith({
511 status: 200,
512 headers: {
513 'Content-Type': 'application/json; charset=utf-8'
514 },
515 body: {
516 errors: eachLike({
517 message: like('An error occurred when fetching the forum')
518 })
519 }
520 });
521 return await internals.provider.addInteraction(forumQuery);
522 });
523
524 test('it returns the error', async () => {
525 const forum = getForum('freezer');
526 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
527 let response = null;
528 forum.subscribe((forumsValue) => {
529 response = forumsValue;
530 counter();
531 });
532 expect(response.data).toBe(null);
533 expect(response.loading).toBe(true);
534 expect(response.error).toBe(undefined);
535 await resolveAfterTwo;
536 expect(response.data).toBe(null);
537 expect(response.loading).toBe(false);
538 expect(response.error.graphQLErrors).toEqual(
539 expect.arrayContaining([
540 {
541 message: 'An error occurred when fetching the forum'
542 }
543 ])
544 );
545 });
546 });
547 });
548});