]> git.r.bdr.sh - rbdr/forum/blame - src/stores/forums.test.js
Add error/empty cases for forums
[rbdr/forum] / src / stores / forums.test.js
CommitLineData
73973eda
RBR
1import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
2import { resolve } from 'path';
3
4import { resolveAfter } from '$/utils/resolve_after';
5
73973eda
RBR
6const { eachLike, like } = Matchers;
7
8jest.mock('$/config/config.js');
9
fcbbc496 10import { getForum, getForums } from './forums';
73973eda
RBR
11
12const internals = {
13 provider: null
14};
15
fcbbc496 16describe('Forums store pact', () => {
73973eda
RBR
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
605230e0
RBR
33 describe('When there\'s forums', () => {
34
35 describe('GetForums', () => {
36
37 beforeAll(async () => {
38
39 const forumQuery = new GraphQLInteraction()
40 .given('there\'s forums')
41 .uponReceiving('a request to list the forums')
42 .withRequest({
43 path: '/graphql',
44 method: 'POST'
45 })
46 .withOperation('GetForums')
47 .withQuery(
48 `query GetForums {
49 forums {
50 id
51 glyph
52 label
53 position
54 __typename
55 }
56 }`
57 )
58 .withVariables({})
59 .willRespondWith({
60 status: 200,
61 headers: {
62 'Content-Type': 'application/json; charset=utf-8'
63 },
64 body: {
65 data: {
66 forums: eachLike({
67 id: like('butter'),
68 glyph: like('⌘'),
69 label: like('test_forums.butter'),
70 position: like(1)
71 })
72 }
73973eda 73 }
605230e0
RBR
74 });
75 return await internals.provider.addInteraction(forumQuery);
76 });
77
78 test('it returns the forums', async () => {
79
80 const forums = getForums();
81 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
82 let response = null;
83 forums.subscribe((forumsValue) => {
84
85 response = forumsValue;
86 counter();
73973eda 87 });
605230e0
RBR
88 expect(response.data).toBeInstanceOf(Array);
89 expect(response.data.length).toBe(0);
90 expect(response.loading).toBe(true);
91 expect(response.error).toBe(undefined);
92 await resolveAfterTwo;
93 expect(response.data).toEqual(expect.arrayContaining([{
94 id: 'butter',
95 glyph: '⌘',
96 label: 'test_forums.butter',
97 position: 1
98 }]));
99 expect(response.loading).toBe(false);
100 expect(response.error).toBe(undefined);
101 });
73973eda
RBR
102 });
103
605230e0
RBR
104 describe('GetForum', () => {
105
106 beforeAll(async () => {
107
108 const forumQuery = new GraphQLInteraction()
109 .given('there\'s forums')
110 .uponReceiving('a request to get a single forum')
111 .withRequest({
112 path: '/graphql',
113 method: 'POST'
114 })
115 .withOperation('GetForum')
116 .withQuery(
117 `query GetForum($id: ID!) {
118 forum(id: $id) {
119 id
120 glyph
121 label
122 position
123 topics {
124 id
125 title
126 updated_at
127 ttl
128 __typename
129 }
130 __typename
131 }
132 }`
133 )
134 .withVariables({
135 id: 'freezer'
136 })
137 .willRespondWith({
138 status: 200,
139 headers: {
140 'Content-Type': 'application/json; charset=utf-8'
141 },
142 body: {
143 data: {
144 forum: like({
145 id: 'freezer',
146 glyph: like('✭'),
147 label: like('test_forums.freezer'),
148 position: like(3),
149 topics: eachLike({
150 id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
151 title: like('Very pacty topic'),
152 updated_at: like(1619954611616),
153 ttl: like(3601)
154 })
155 })
156 }
157 }
158 });
159 return await internals.provider.addInteraction(forumQuery);
160 });
161
162 test('it returns the forum', async () => {
73973eda 163
605230e0
RBR
164 const forum = getForum('freezer');
165 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
166 let response = null;
167 forum.subscribe((forumsValue) => {
73973eda 168
605230e0
RBR
169 response = forumsValue;
170 counter();
171 });
172 expect(response.data).toBe(null);
173 expect(response.loading).toBe(true);
174 expect(response.error).toBe(undefined);
175 await resolveAfterTwo;
176 expect(response.data.id).toBe('freezer');
177 expect(response.data.glyph).toBe('✭');
178 expect(response.data.label).toBe('test_forums.freezer');
179 expect(response.data.position).toBe(3);
180 expect(response.data.topics).toEqual(expect.arrayContaining([{
181 id: '629de02c-151a-4db7-bb86-43b2add8a15a',
182 title: 'Very pacty topic',
183 updated_at: 1619954611616,
184 ttl: 3601
185 }]));
186 expect(response.loading).toBe(false);
187 expect(response.error).toBe(undefined);
73973eda 188 });
73973eda
RBR
189 });
190 });
fcbbc496 191
605230e0
RBR
192 describe('When there\'s no forums', () => {
193
194 describe('GetForums', () => {
195
196 beforeAll(async () => {
197
198 const forumQuery = new GraphQLInteraction()
199 .given('there\'s no forums')
200 .uponReceiving('a request to list the forums')
201 .withRequest({
202 path: '/graphql',
203 method: 'POST'
204 })
205 .withOperation('GetForums')
206 .withQuery(
207 `query GetForums {
208 forums {
fcbbc496 209 id
605230e0
RBR
210 glyph
211 label
212 position
fcbbc496
RBR
213 __typename
214 }
605230e0
RBR
215 }`
216 )
217 .withVariables({})
218 .willRespondWith({
219 status: 200,
220 headers: {
221 'Content-Type': 'application/json; charset=utf-8'
222 },
223 body: {
224 data: {
225 forums: []
226 }
fcbbc496 227 }
605230e0
RBR
228 });
229 return await internals.provider.addInteraction(forumQuery);
230 });
231
232 test('it returns the forums', async () => {
233
234 const forums = getForums();
235 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
236 let response = null;
237 forums.subscribe((forumsValue) => {
238
239 response = forumsValue;
240 counter();
241 });
242 expect(response.data).toBeInstanceOf(Array);
243 expect(response.data.length).toBe(0);
244 expect(response.loading).toBe(true);
245 expect(response.error).toBe(undefined);
246 await resolveAfterTwo;
247 expect(response.data).toBeInstanceOf(Array);
248 expect(response.data.length).toBe(0);
249 expect(response.loading).toBe(false);
250 expect(response.error).toBe(undefined);
251 });
252 });
253
254 describe('GetForum', () => {
255
256 beforeAll(async () => {
257
258 const forumQuery = new GraphQLInteraction()
259 .given('there\'s no forums')
260 .uponReceiving('a request to get a single forum')
261 .withRequest({
262 path: '/graphql',
263 method: 'POST'
264 })
265 .withOperation('GetForum')
266 .withQuery(
267 `query GetForum($id: ID!) {
268 forum(id: $id) {
269 id
270 glyph
271 label
272 position
273 topics {
274 id
275 title
276 updated_at
277 ttl
278 __typename
279 }
280 __typename
281 }
282 }`
283 )
284 .withVariables({
285 id: 'freezer'
286 })
287 .willRespondWith({
288 status: 200,
289 headers: {
290 'Content-Type': 'application/json; charset=utf-8'
291 },
292 body: {
293 data: {
294 forum: null
295 }
fcbbc496 296 }
605230e0
RBR
297 });
298 return await internals.provider.addInteraction(forumQuery);
299 });
300
301 test('it returns the forum', async () => {
302
303 const forum = getForum('freezer');
304 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
305 let response = null;
306 forum.subscribe((forumsValue) => {
307
308 response = forumsValue;
309 counter();
310 });
311 expect(response.data).toBe(null);
312 expect(response.loading).toBe(true);
313 expect(response.error).toBe(undefined);
314 await resolveAfterTwo;
315 expect(response.data).toBe(null);
316 expect(response.loading).toBe(false);
317 expect(response.error).toBe(undefined);
318 });
319 });
320 });
321
322 describe('When there\'s an error', () => {
323
324 describe('GetForums', () => {
325
326 beforeAll(async () => {
327
328 const forumQuery = new GraphQLInteraction()
329 .given('there\'s an error')
330 .uponReceiving('a request to list the forums')
331 .withRequest({
332 path: '/graphql',
333 method: 'POST'
334 })
335 .withOperation('GetForums')
336 .withQuery(
337 `query GetForums {
338 forums {
339 id
340 glyph
341 label
342 position
343 __typename
344 }
345 }`
346 )
347 .withVariables({})
348 .willRespondWith({
349 status: 500
350 });
351 return await internals.provider.addInteraction(forumQuery);
352 });
353
354 test('it returns the error', async () => {
355
356 const forums = getForums();
357 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
358 let response = null;
359 forums.subscribe((forumsValue) => {
360
361 response = forumsValue;
362 counter();
fcbbc496 363 });
605230e0
RBR
364 expect(response.data).toBeInstanceOf(Array);
365 expect(response.data.length).toBe(0);
366 expect(response.loading).toBe(true);
367 expect(response.error).toBe(undefined);
368 await resolveAfterTwo;
369 expect(response.data).toBeInstanceOf(Array);
370 expect(response.data.length).toBe(0);
371 expect(response.loading).toBe(false);
372 expect(response.error).toBeInstanceOf(Error);
373 });
fcbbc496
RBR
374 });
375
605230e0 376 describe('GetForum', () => {
fcbbc496 377
605230e0 378 beforeAll(async () => {
fcbbc496 379
605230e0
RBR
380 const forumQuery = new GraphQLInteraction()
381 .given('there\'s an error')
382 .uponReceiving('a request to get a single forum')
383 .withRequest({
384 path: '/graphql',
385 method: 'POST'
386 })
387 .withOperation('GetForum')
388 .withQuery(
389 `query GetForum($id: ID!) {
390 forum(id: $id) {
391 id
392 glyph
393 label
394 position
395 topics {
396 id
397 title
398 updated_at
399 ttl
400 __typename
401 }
402 __typename
403 }
404 }`
405 )
406 .withVariables({
407 id: 'freezer'
408 })
409 .willRespondWith({
410 status: 500
411 });
412 return await internals.provider.addInteraction(forumQuery);
413 });
414
415 test('it returns the error', async () => {
416
417 const forum = getForum('freezer');
418 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
419 let response = null;
420 forum.subscribe((forumsValue) => {
421
422 response = forumsValue;
423 counter();
424 });
425 expect(response.data).toBe(null);
426 expect(response.loading).toBe(true);
427 expect(response.error).toBe(undefined);
428 await resolveAfterTwo;
429 expect(response.data).toBe(null);
430 expect(response.loading).toBe(false);
431 expect(response.error).toBeInstanceOf(Error);
fcbbc496 432 });
fcbbc496
RBR
433 });
434 });
73973eda 435});