1 import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
2 import { resolve } from 'path';
4 import { resolveAfter } from '$lib/utils/resolve_after';
6 const { eachLike, like } = Matchers;
8 jest.mock('$lib/config/config.ts');
10 import { getForum, getForums } from './forums';
16 describe('Forums store pact', () => {
18 beforeAll(async () => {
20 internals.provider = new Pact({
22 dir: resolve(process.cwd(), 'pacts'),
23 consumer: 'ForumClient',
24 provider: 'ForumServer',
25 pactfileWriteMode: 'update'
28 await internals.provider.setup();
31 afterEach(() => internals.provider.verify());
32 afterAll(() => internals.provider.finalize());
34 describe('When there\'s data', () => {
36 describe('GetForums', () => {
38 beforeAll(async () => {
40 const forumQuery = new GraphQLInteraction()
41 .given('there\'s data')
42 .uponReceiving('a request to list the forums')
47 .withOperation('GetForums')
63 'Content-Type': 'application/json; charset=utf-8'
70 label: like('test_forums.butter'),
76 return await internals.provider.addInteraction(forumQuery);
79 test('it returns the forums', async () => {
81 const forums = getForums();
82 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
84 forums.subscribe((forumsValue) => {
86 response = forumsValue;
89 expect(response.data).toBeInstanceOf(Array);
90 expect(response.data.length).toBe(0);
91 expect(response.loading).toBe(true);
92 expect(response.error).toBe(undefined);
93 await resolveAfterTwo;
94 expect(response.data).toEqual(expect.arrayContaining([{
97 label: 'test_forums.butter',
100 expect(response.loading).toBe(false);
101 expect(response.error).toBe(undefined);
105 describe('GetForum', () => {
107 beforeAll(async () => {
109 const forumQuery = new GraphQLInteraction()
110 .given('there\'s data')
111 .uponReceiving('a request to get a single forum')
116 .withOperation('GetForum')
118 `query GetForum($id: ID!) {
141 'Content-Type': 'application/json; charset=utf-8'
148 label: like('test_forums.freezer'),
151 id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
152 title: like('Very pacty topic'),
153 updated_at: like(1619954611616),
160 return await internals.provider.addInteraction(forumQuery);
163 test('it returns the forum', async () => {
165 const forum = getForum('freezer');
166 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
168 forum.subscribe((forumsValue) => {
170 response = forumsValue;
173 expect(response.data).toBe(null);
174 expect(response.loading).toBe(true);
175 expect(response.error).toBe(undefined);
176 await resolveAfterTwo;
177 expect(response.data.id).toBe('freezer');
178 expect(response.data.glyph).toBe('✭');
179 expect(response.data.label).toBe('test_forums.freezer');
180 expect(response.data.position).toBe(3);
181 expect(response.data.topics).toEqual(expect.arrayContaining([{
182 id: '629de02c-151a-4db7-bb86-43b2add8a15a',
183 title: 'Very pacty topic',
184 updated_at: 1619954611616,
187 expect(response.loading).toBe(false);
188 expect(response.error).toBe(undefined);
193 describe('When there\'s no data', () => {
195 describe('GetForums', () => {
197 beforeAll(async () => {
199 const forumQuery = new GraphQLInteraction()
200 .given('there\'s no data')
201 .uponReceiving('a request to list the forums')
206 .withOperation('GetForums')
222 'Content-Type': 'application/json; charset=utf-8'
230 return await internals.provider.addInteraction(forumQuery);
233 test('it returns the forums', async () => {
235 const forums = getForums();
236 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
238 forums.subscribe((forumsValue) => {
240 response = forumsValue;
243 expect(response.data).toBeInstanceOf(Array);
244 expect(response.data.length).toBe(0);
245 expect(response.loading).toBe(true);
246 expect(response.error).toBe(undefined);
247 await resolveAfterTwo;
248 expect(response.data).toBeInstanceOf(Array);
249 expect(response.data.length).toBe(0);
250 expect(response.loading).toBe(false);
251 expect(response.error).toBe(undefined);
255 describe('GetForum', () => {
257 beforeAll(async () => {
259 const forumQuery = new GraphQLInteraction()
260 .given('there\'s no data')
261 .uponReceiving('a request to get a single forum')
266 .withOperation('GetForum')
268 `query GetForum($id: ID!) {
291 'Content-Type': 'application/json; charset=utf-8'
299 return await internals.provider.addInteraction(forumQuery);
302 test('it returns the forum', async () => {
304 const forum = getForum('freezer');
305 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
307 forum.subscribe((forumsValue) => {
309 response = forumsValue;
312 expect(response.data).toBe(null);
313 expect(response.loading).toBe(true);
314 expect(response.error).toBe(undefined);
315 await resolveAfterTwo;
316 expect(response.data).toBe(null);
317 expect(response.loading).toBe(false);
318 expect(response.error).toBe(undefined);
323 describe('When there\'s a server error', () => {
325 describe('GetForums', () => {
327 beforeAll(async () => {
329 const forumQuery = new GraphQLInteraction()
330 .given('there\'s a server error')
331 .uponReceiving('a request to list the forums')
336 .withOperation('GetForums')
352 return await internals.provider.addInteraction(forumQuery);
355 test('it returns the error', async () => {
357 const forums = getForums();
358 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
360 forums.subscribe((forumsValue) => {
362 response = forumsValue;
365 expect(response.data).toBeInstanceOf(Array);
366 expect(response.data.length).toBe(0);
367 expect(response.loading).toBe(true);
368 expect(response.error).toBe(undefined);
369 await resolveAfterTwo;
370 expect(response.data).toBeInstanceOf(Array);
371 expect(response.data.length).toBe(0);
372 expect(response.loading).toBe(false);
373 expect(response.error).toBeInstanceOf(Error);
377 describe('GetForum', () => {
379 beforeAll(async () => {
381 const forumQuery = new GraphQLInteraction()
382 .given('there\'s a server error')
383 .uponReceiving('a request to get a single forum')
388 .withOperation('GetForum')
390 `query GetForum($id: ID!) {
413 return await internals.provider.addInteraction(forumQuery);
416 test('it returns the error', async () => {
418 const forum = getForum('freezer');
419 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
421 forum.subscribe((forumsValue) => {
423 response = forumsValue;
426 expect(response.data).toBe(null);
427 expect(response.loading).toBe(true);
428 expect(response.error).toBe(undefined);
429 await resolveAfterTwo;
430 expect(response.data).toBe(null);
431 expect(response.loading).toBe(false);
432 expect(response.error).toBeInstanceOf(Error);
437 describe('When there\'s an error in the response', () => {
439 describe('GetForums', () => {
441 beforeAll(async () => {
443 const forumQuery = new GraphQLInteraction()
444 .given('there\'s an error in the response')
445 .uponReceiving('a request to list the forums')
450 .withOperation('GetForums')
466 'Content-Type': 'application/json; charset=utf-8'
470 message: like('An error occurred when fetching forums')
474 return await internals.provider.addInteraction(forumQuery);
477 test('it returns the error', async () => {
479 const forums = getForums();
480 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
482 forums.subscribe((forumsValue) => {
484 response = forumsValue;
487 expect(response.data).toBeInstanceOf(Array);
488 expect(response.data.length).toBe(0);
489 expect(response.loading).toBe(true);
490 expect(response.error).toBe(undefined);
491 await resolveAfterTwo;
492 expect(response.data).toBeInstanceOf(Array);
493 expect(response.data.length).toBe(0);
494 expect(response.loading).toBe(false);
495 expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{
496 message: 'An error occurred when fetching forums'
501 describe('GetForum', () => {
503 beforeAll(async () => {
505 const forumQuery = new GraphQLInteraction()
506 .given('there\'s an error in the response')
507 .uponReceiving('a request to get a single forum')
512 .withOperation('GetForum')
514 `query GetForum($id: ID!) {
537 'Content-Type': 'application/json; charset=utf-8'
541 message: like('An error occurred when fetching the forum')
545 return await internals.provider.addInteraction(forumQuery);
548 test('it returns the error', async () => {
550 const forum = getForum('freezer');
551 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
553 forum.subscribe((forumsValue) => {
555 response = forumsValue;
558 expect(response.data).toBe(null);
559 expect(response.loading).toBe(true);
560 expect(response.error).toBe(undefined);
561 await resolveAfterTwo;
562 expect(response.data).toBe(null);
563 expect(response.loading).toBe(false);
564 expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{
565 message: 'An error occurred when fetching the forum'