]>
git.r.bdr.sh - rbdr/forum/blob - src/stores/topics.test.js
7c3285bd16381815d50299196bc66346de1e8daf
1 import { GraphQLInteraction
, Pact
, Matchers
} from '@pact-foundation/pact';
2 import { resolve
} from 'path';
4 import { resolveAfter
} from '$/utils/resolve_after';
6 const { eachLike
, like
} = Matchers
;
8 jest
.mock('$/config/config.js');
10 import { getTopic
} from './topics';
16 describe('Topics 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('GetTopic', () => {
38 beforeAll(async () => {
40 const topicQuery
= new GraphQLInteraction()
41 .given('there\'s data')
42 .uponReceiving('a request to get a single topic')
47 .withOperation('GetTopic')
49 `query GetTopic($id: ID!) {
82 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
87 'Content-Type': 'application/json; charset=utf-8'
92 id: like('0b58959d-d448-4a4e-84b6-35e5ac0028d1'),
93 title: like('The pacty topic of the day'),
94 updated_at: like(1619979888906),
99 label: like('test_forums.cucumber')
106 id: like('ed93530e-6f9c-4701-91ef-14f9e0ed3e26'),
107 text: like('The content of this post is very relevant'),
108 created_at: like(1619979889798),
110 id: like('07fb2ba0-0945-464a-b215-873296710c8c'),
111 handle: like('cucumber_fan92')
118 return await internals
.provider
.addInteraction(topicQuery
);
121 test('it returns the topic', async () => {
123 const topic
= getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
124 const { counter
, promise: resolveAfterTwo
} = resolveAfter(2);
126 topic
.subscribe((topicValue
) => {
128 response
= topicValue
;
131 expect(response
.data
).toBe(null);
132 expect(response
.loading
).toBe(true);
133 expect(response
.error
).toBe(undefined);
134 await resolveAfterTwo
;
135 expect(response
.data
).toEqual({
136 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1',
137 title: 'The pacty topic of the day',
138 updated_at: 1619979888906,
143 label: 'test_forums.cucumber'
150 id: 'ed93530e-6f9c-4701-91ef-14f9e0ed3e26',
151 text: 'The content of this post is very relevant',
152 created_at: 1619979889798,
154 id: '07fb2ba0-0945-464a-b215-873296710c8c',
155 handle: 'cucumber_fan92'
159 expect(response
.loading
).toBe(false);
160 expect(response
.error
).toBe(undefined);
165 describe('When there\'s no data', () => {
167 describe('GetTopic', () => {
169 beforeAll(async () => {
171 const topicQuery
= new GraphQLInteraction()
172 .given('there\'s no data')
173 .uponReceiving('a request to get a single topic')
178 .withOperation('GetTopic')
180 `query GetTopic($id: ID!) {
213 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
218 'Content-Type': 'application/json; charset=utf-8'
226 return await internals
.provider
.addInteraction(topicQuery
);
229 test('it returns the topic', async () => {
231 const topic
= getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
232 const { counter
, promise: resolveAfterTwo
} = resolveAfter(2);
234 topic
.subscribe((topicValue
) => {
236 response
= topicValue
;
239 expect(response
.data
).toBe(null);
240 expect(response
.loading
).toBe(true);
241 expect(response
.error
).toBe(undefined);
242 await resolveAfterTwo
;
243 expect(response
.data
).toBe(null);
244 expect(response
.loading
).toBe(false);
245 expect(response
.error
).toBe(undefined);
250 describe('When there\'s a server error', () => {
252 describe('GetTopic', () => {
254 beforeAll(async () => {
256 const topicQuery
= new GraphQLInteraction()
257 .given('there\'s a server error')
258 .uponReceiving('a request to get a single topic')
263 .withOperation('GetTopic')
265 `query GetTopic($id: ID!) {
298 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
303 return await internals
.provider
.addInteraction(topicQuery
);
306 test('it returns the error', async () => {
308 const topic
= getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
309 const { counter
, promise: resolveAfterTwo
} = resolveAfter(2);
311 topic
.subscribe((topicValue
) => {
313 response
= topicValue
;
316 expect(response
.data
).toBe(null);
317 expect(response
.loading
).toBe(true);
318 expect(response
.error
).toBe(undefined);
319 await resolveAfterTwo
;
320 expect(response
.data
).toBe(null);
321 expect(response
.loading
).toBe(false);
322 expect(response
.error
).toBeInstanceOf(Error
);
327 describe('When there\'s an error in the response', () => {
329 describe('GetTopic', () => {
331 beforeAll(async () => {
333 const topicQuery
= new GraphQLInteraction()
334 .given('there\'s an error in the response')
335 .uponReceiving('a request to get a single topic')
340 .withOperation('GetTopic')
342 `query GetTopic($id: ID!) {
375 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
380 'Content-Type': 'application/json; charset=utf-8'
384 message: like('An error occurred when fetching the topic')
388 return await internals
.provider
.addInteraction(topicQuery
);
391 test('it returns the error', async () => {
393 const topic
= getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
394 const { counter
, promise: resolveAfterTwo
} = resolveAfter(2);
396 topic
.subscribe((topicValue
) => {
398 response
= topicValue
;
401 expect(response
.data
).toBe(null);
402 expect(response
.loading
).toBe(true);
403 expect(response
.error
).toBe(undefined);
404 await resolveAfterTwo
;
405 expect(response
.data
).toBe(null);
406 expect(response
.loading
).toBe(false);
407 expect(response
.error
.graphQLErrors
).toEqual(expect
.arrayContaining([{
408 message: 'An error occurred when fetching the topic'