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', () => {
17 beforeAll(async () => {
18 internals.provider = new Pact({
20 dir: resolve(process.cwd(), 'pacts'),
21 consumer: 'ForumClient',
22 provider: 'ForumServer',
23 pactfileWriteMode: 'update'
26 await internals.provider.setup();
29 afterEach(() => internals.provider.verify());
30 afterAll(() => internals.provider.finalize());
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')
42 .withOperation('GetForums')
58 'Content-Type': 'application/json; charset=utf-8'
65 label: like('test_forums.butter'),
71 return await internals.provider.addInteraction(forumQuery);
74 test('it returns the forums', async () => {
75 const forums = getForums();
76 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
78 forums.subscribe((forumsValue) => {
79 response = forumsValue;
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([
92 label: 'test_forums.butter',
97 expect(response.loading).toBe(false);
98 expect(response.error).toBe(undefined);
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')
111 .withOperation('GetForum')
113 `query GetForum($id: ID!) {
136 'Content-Type': 'application/json; charset=utf-8'
143 label: like('test_forums.freezer'),
146 id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
147 title: like('Very pacty topic'),
148 updated_at: like(1619954611616),
155 return await internals.provider.addInteraction(forumQuery);
158 test('it returns the forum', async () => {
159 const forum = getForum('freezer');
160 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
162 forum.subscribe((forumsValue) => {
163 response = forumsValue;
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([
177 id: '629de02c-151a-4db7-bb86-43b2add8a15a',
178 title: 'Very pacty topic',
179 updated_at: 1619954611616,
184 expect(response.loading).toBe(false);
185 expect(response.error).toBe(undefined);
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')
200 .withOperation('GetForums')
216 'Content-Type': 'application/json; charset=utf-8'
224 return await internals.provider.addInteraction(forumQuery);
227 test('it returns the forums', async () => {
228 const forums = getForums();
229 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
231 forums.subscribe((forumsValue) => {
232 response = forumsValue;
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);
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')
256 .withOperation('GetForum')
258 `query GetForum($id: ID!) {
281 'Content-Type': 'application/json; charset=utf-8'
289 return await internals.provider.addInteraction(forumQuery);
292 test('it returns the forum', async () => {
293 const forum = getForum('freezer');
294 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
296 forum.subscribe((forumsValue) => {
297 response = forumsValue;
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);
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')
321 .withOperation('GetForums')
337 return await internals.provider.addInteraction(forumQuery);
340 test('it returns the error', async () => {
341 const forums = getForums();
342 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
344 forums.subscribe((forumsValue) => {
345 response = forumsValue;
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);
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')
369 .withOperation('GetForum')
371 `query GetForum($id: ID!) {
394 return await internals.provider.addInteraction(forumQuery);
397 test('it returns the error', async () => {
398 const forum = getForum('freezer');
399 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
401 forum.subscribe((forumsValue) => {
402 response = forumsValue;
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);
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')
426 .withOperation('GetForums')
442 'Content-Type': 'application/json; charset=utf-8'
446 message: like('An error occurred when fetching forums')
450 return await internals.provider.addInteraction(forumQuery);
453 test('it returns the error', async () => {
454 const forums = getForums();
455 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
457 forums.subscribe((forumsValue) => {
458 response = forumsValue;
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([
472 message: 'An error occurred when fetching forums'
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')
488 .withOperation('GetForum')
490 `query GetForum($id: ID!) {
513 'Content-Type': 'application/json; charset=utf-8'
517 message: like('An error occurred when fetching the forum')
521 return await internals.provider.addInteraction(forumQuery);
524 test('it returns the error', async () => {
525 const forum = getForum('freezer');
526 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
528 forum.subscribe((forumsValue) => {
529 response = forumsValue;
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([
541 message: 'An error occurred when fetching the forum'