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 { getTopic } from './topics';
16 describe('Topics 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('GetTopic', () => {
34 beforeAll(async () => {
35 const topicQuery = new GraphQLInteraction()
36 .given("there's data")
37 .uponReceiving('a request to get a single topic')
42 .withOperation('GetTopic')
44 `query GetTopic($id: ID!) {
77 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
82 'Content-Type': 'application/json; charset=utf-8'
87 id: like('0b58959d-d448-4a4e-84b6-35e5ac0028d1'),
88 title: like('The pacty topic of the day'),
89 updated_at: like(1619979888906),
94 label: like('test_forums.cucumber')
101 id: like('ed93530e-6f9c-4701-91ef-14f9e0ed3e26'),
102 text: like('The content of this post is very relevant'),
103 created_at: like(1619979889798),
105 id: like('07fb2ba0-0945-464a-b215-873296710c8c'),
106 handle: like('cucumber_fan92')
113 return await internals.provider.addInteraction(topicQuery);
116 test('it returns the topic', async () => {
117 const topic = getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
118 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
120 topic.subscribe((topicValue) => {
121 response = topicValue;
124 expect(response.data).toBe(null);
125 expect(response.loading).toBe(true);
126 expect(response.error).toBe(undefined);
127 await resolveAfterTwo;
128 expect(response.data).toEqual({
129 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1',
130 title: 'The pacty topic of the day',
131 updated_at: 1619979888906,
136 label: 'test_forums.cucumber'
146 id: 'ed93530e-6f9c-4701-91ef-14f9e0ed3e26',
147 text: 'The content of this post is very relevant',
148 created_at: 1619979889798,
150 id: '07fb2ba0-0945-464a-b215-873296710c8c',
151 handle: 'cucumber_fan92'
156 expect(response.loading).toBe(false);
157 expect(response.error).toBe(undefined);
162 describe("When there's no data", () => {
163 describe('GetTopic', () => {
164 beforeAll(async () => {
165 const topicQuery = new GraphQLInteraction()
166 .given("there's no data")
167 .uponReceiving('a request to get a single topic')
172 .withOperation('GetTopic')
174 `query GetTopic($id: ID!) {
207 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
212 'Content-Type': 'application/json; charset=utf-8'
220 return await internals.provider.addInteraction(topicQuery);
223 test('it returns the topic', async () => {
224 const topic = getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
225 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
227 topic.subscribe((topicValue) => {
228 response = topicValue;
231 expect(response.data).toBe(null);
232 expect(response.loading).toBe(true);
233 expect(response.error).toBe(undefined);
234 await resolveAfterTwo;
235 expect(response.data).toBe(null);
236 expect(response.loading).toBe(false);
237 expect(response.error).toBe(undefined);
242 describe("When there's a server error", () => {
243 describe('GetTopic', () => {
244 beforeAll(async () => {
245 const topicQuery = new GraphQLInteraction()
246 .given("there's a server error")
247 .uponReceiving('a request to get a single topic')
252 .withOperation('GetTopic')
254 `query GetTopic($id: ID!) {
287 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
292 return await internals.provider.addInteraction(topicQuery);
295 test('it returns the error', async () => {
296 const topic = getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
297 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
299 topic.subscribe((topicValue) => {
300 response = topicValue;
303 expect(response.data).toBe(null);
304 expect(response.loading).toBe(true);
305 expect(response.error).toBe(undefined);
306 await resolveAfterTwo;
307 expect(response.data).toBe(null);
308 expect(response.loading).toBe(false);
309 expect(response.error).toBeInstanceOf(Error);
314 describe("When there's an error in the response", () => {
315 describe('GetTopic', () => {
316 beforeAll(async () => {
317 const topicQuery = new GraphQLInteraction()
318 .given("there's an error in the response")
319 .uponReceiving('a request to get a single topic')
324 .withOperation('GetTopic')
326 `query GetTopic($id: ID!) {
359 id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1'
364 'Content-Type': 'application/json; charset=utf-8'
368 message: like('An error occurred when fetching the topic')
372 return await internals.provider.addInteraction(topicQuery);
375 test('it returns the error', async () => {
376 const topic = getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1');
377 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
379 topic.subscribe((topicValue) => {
380 response = topicValue;
383 expect(response.data).toBe(null);
384 expect(response.loading).toBe(true);
385 expect(response.error).toBe(undefined);
386 await resolveAfterTwo;
387 expect(response.data).toBe(null);
388 expect(response.loading).toBe(false);
389 expect(response.error.graphQLErrors).toEqual(
390 expect.arrayContaining([
392 message: 'An error occurred when fetching the topic'