]> git.r.bdr.sh - rbdr/forum/blob - src/stores/forums.test.js
25293c803d04c80d175ace34d9fe8f5951fa7898
[rbdr/forum] / src / stores / forums.test.js
1 import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
2 import { resolve } from 'path';
3
4 import { resolveAfter } from '$/utils/resolve_after';
5
6 import { act } from '@testing-library/svelte';
7
8 const { eachLike, like } = Matchers;
9
10 jest.mock('$/config/config.js');
11
12 import { getForums } from './forums';
13
14 const internals = {
15 provider: null
16 };
17
18 describe('Forum store pact', () => {
19
20 beforeAll(async () => {
21
22 internals.provider = new Pact({
23 port: 1234,
24 dir: resolve(process.cwd(), 'pacts'),
25 consumer: 'ForumsStore',
26 provider: 'ForumAPIServer'
27 });
28
29 await internals.provider.setup();
30 });
31
32 afterEach(() => internals.provider.verify());
33 afterAll(() => internals.provider.finalize());
34
35 describe('there are forums', () => {
36
37 beforeAll(async () => {
38
39 const forumQuery = new GraphQLInteraction()
40 .uponReceiving('a request to list the forums')
41 .withRequest({
42 path: '/graphql',
43 method: 'POST'
44 })
45 .withOperation('GetForums')
46 .withQuery(
47 `query GetForums {
48 forums {
49 id
50 glyph
51 label
52 position
53 __typename
54 }
55 }`
56 )
57 .withVariables({})
58 .willRespondWith({
59 status: 200,
60 headers: {
61 'Content-Type': 'application/json; charset=utf-8'
62 },
63 body: {
64 data: {
65 forums: eachLike({
66 id: like('butter'),
67 glyph: like('⌘'),
68 label: like('test_forums.butter'),
69 position: like(1)
70 })
71 }
72 }
73 });
74 return await internals.provider.addInteraction(forumQuery);
75 });
76
77 test('it returns the forums', async () => {
78
79 const forums = getForums();
80 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
81 let response = null;
82 forums.subscribe((forumsValue) => {
83
84 response = forumsValue;
85 counter();
86 });
87 expect(response.data).toEqual(expect.arrayContaining([]));
88 expect(response.loading).toBe(true);
89 expect(response.error).toBe(undefined);
90 await resolveAfterTwo;
91 expect(response.data).toEqual(expect.arrayContaining([{
92 id: 'butter',
93 glyph: '⌘',
94 label: 'test_forums.butter',
95 position: 1
96 }]));
97 expect(response.loading).toBe(false);
98 expect(response.error).toBe(undefined);
99 });
100 });
101 });