]> git.r.bdr.sh - rbdr/forum/blob - src/stores/forums.test.js
694316baac611c3528c6dfc01ee5b6954fe4b303
[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 const { eachLike, like } = Matchers;
7
8 jest.mock('$/config/config.js');
9
10 import { getForum, getForums } from './forums';
11
12 const internals = {
13 provider: null
14 };
15
16 describe('Forums store pact', () => {
17
18 beforeAll(async () => {
19
20 internals.provider = new Pact({
21 port: 1234,
22 dir: resolve(process.cwd(), 'pacts'),
23 consumer: 'ForumsStore',
24 provider: 'ForumAPIServer'
25 });
26
27 await internals.provider.setup();
28 });
29
30 afterEach(() => internals.provider.verify());
31 afterAll(() => internals.provider.finalize());
32
33 describe('GetForums', () => {
34
35 beforeAll(async () => {
36
37 const forumQuery = new GraphQLInteraction()
38 .uponReceiving('a request to list the forums')
39 .withRequest({
40 path: '/graphql',
41 method: 'POST'
42 })
43 .withOperation('GetForums')
44 .withQuery(
45 `query GetForums {
46 forums {
47 id
48 glyph
49 label
50 position
51 __typename
52 }
53 }`
54 )
55 .withVariables({})
56 .willRespondWith({
57 status: 200,
58 headers: {
59 'Content-Type': 'application/json; charset=utf-8'
60 },
61 body: {
62 data: {
63 forums: eachLike({
64 id: like('butter'),
65 glyph: like('⌘'),
66 label: like('test_forums.butter'),
67 position: like(1)
68 })
69 }
70 }
71 });
72 return await internals.provider.addInteraction(forumQuery);
73 });
74
75 test('it returns the forums', async () => {
76
77 const forums = getForums();
78 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
79 let response = null;
80 forums.subscribe((forumsValue) => {
81
82 response = forumsValue;
83 counter();
84 });
85 expect(response.data).toEqual(expect.arrayContaining([]));
86 expect(response.loading).toBe(true);
87 expect(response.error).toBe(undefined);
88 await resolveAfterTwo;
89 expect(response.data).toEqual(expect.arrayContaining([{
90 id: 'butter',
91 glyph: '⌘',
92 label: 'test_forums.butter',
93 position: 1
94 }]));
95 expect(response.loading).toBe(false);
96 expect(response.error).toBe(undefined);
97 });
98 });
99
100 describe('GetForum', () => {
101
102 beforeAll(async () => {
103
104 const forumQuery = new GraphQLInteraction()
105 .uponReceiving('a request to get a single forum')
106 .withRequest({
107 path: '/graphql',
108 method: 'POST'
109 })
110 .withOperation('GetForum')
111 .withQuery(
112 `query GetForum($id: ID!) {
113 forum(id: $id) {
114 id
115 glyph
116 label
117 position
118 topics {
119 id
120 title
121 updated_at
122 ttl
123 __typename
124 }
125 __typename
126 }
127 }`
128 )
129 .withVariables({
130 id: 'freezer'
131 })
132 .willRespondWith({
133 status: 200,
134 headers: {
135 'Content-Type': 'application/json; charset=utf-8'
136 },
137 body: {
138 data: {
139 forum: like({
140 id: 'freezer',
141 glyph: like('✭'),
142 label: like('test_forums.freezer'),
143 position: like(3),
144 topics: eachLike({
145 id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
146 title: like('Very pacty topic'),
147 updated_at: like(1619954611616),
148 ttl: like(3601)
149 })
150 })
151 }
152 }
153 });
154 return await internals.provider.addInteraction(forumQuery);
155 });
156
157 test('it returns the forum', async () => {
158
159 const forum = getForum('freezer');
160 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
161 let response = null;
162 forum.subscribe((forumsValue) => {
163
164 response = forumsValue;
165 counter();
166 });
167 expect(response.data).toEqual(expect.arrayContaining([]));
168 expect(response.loading).toBe(true);
169 expect(response.error).toBe(undefined);
170 await resolveAfterTwo;
171 expect(response.data.id).toBe('freezer');
172 expect(response.data.glyph).toBe('✭');
173 expect(response.data.label).toBe('test_forums.freezer');
174 expect(response.data.position).toBe(3);
175 expect(response.data.topics).toEqual(expect.arrayContaining([{
176 id: '629de02c-151a-4db7-bb86-43b2add8a15a',
177 title: 'Very pacty topic',
178 updated_at: 1619954611616,
179 ttl: 3601
180 }]));
181 expect(response.loading).toBe(false);
182 expect(response.error).toBe(undefined);
183 });
184 });
185 });