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 { getTag } from './tags';
16 describe('Tags 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('GetTag', () => {
38 beforeAll(async () => {
40 const tagQuery = new GraphQLInteraction()
41 .given('there\'s data')
42 .uponReceiving('a request to get a single tag')
47 .withOperation('GetTag')
49 `query GetTag($id: ID!) {
69 'Content-Type': 'application/json; charset=utf-8'
74 id: like('pineapple'),
76 id: like('cd038ae7-e8b4-4e38-9543-3d697e69ac34'),
77 title: like('This topic is about pineapples'),
78 updated_at: like(1619978944077),
85 return await internals.provider.addInteraction(tagQuery);
88 test('it returns the tag', async () => {
90 const tag = getTag('pineapple');
91 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
93 tag.subscribe((tagValue) => {
98 expect(response.data).toBe(null);
99 expect(response.loading).toBe(true);
100 expect(response.error).toBe(undefined);
101 await resolveAfterTwo;
102 expect(response.data).toEqual({
105 id: 'cd038ae7-e8b4-4e38-9543-3d697e69ac34',
106 title: 'This topic is about pineapples',
107 updated_at: 1619978944077,
111 expect(response.loading).toBe(false);
112 expect(response.error).toBe(undefined);
117 describe('When there\'s no data', () => {
119 describe('GetTag', () => {
121 beforeAll(async () => {
123 const tagQuery = new GraphQLInteraction()
124 .given('there\'s no data')
125 .uponReceiving('a request to get a single tag')
130 .withOperation('GetTag')
132 `query GetTag($id: ID!) {
152 'Content-Type': 'application/json; charset=utf-8'
160 return await internals.provider.addInteraction(tagQuery);
163 test('it returns the tag', async () => {
165 const tag = getTag('pineapple');
166 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
168 tag.subscribe((tagValue) => {
173 expect(response.data).toBe(null);
174 expect(response.loading).toBe(true);
175 expect(response.error).toBe(undefined);
176 await resolveAfterTwo;
177 expect(response.data).toBe(null);
178 expect(response.loading).toBe(false);
179 expect(response.error).toBe(undefined);
184 describe('When there\'s a server error', () => {
186 describe('GetTag', () => {
188 beforeAll(async () => {
190 const tagQuery = new GraphQLInteraction()
191 .given('there\'s a server error')
192 .uponReceiving('a request to get a single tag')
197 .withOperation('GetTag')
199 `query GetTag($id: ID!) {
219 return await internals.provider.addInteraction(tagQuery);
222 test('it returns the error', async () => {
224 const tag = getTag('pineapple');
225 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
227 tag.subscribe((tagValue) => {
232 expect(response.data).toBe(null);
233 expect(response.loading).toBe(true);
234 expect(response.error).toBe(undefined);
235 await resolveAfterTwo;
236 expect(response.data).toBe(null);
237 expect(response.loading).toBe(false);
238 expect(response.error).toBeInstanceOf(Error);
243 describe('When there\'s an error in the response', () => {
245 describe('GetTag', () => {
247 beforeAll(async () => {
249 const tagQuery = new GraphQLInteraction()
250 .given('there\'s an error in the response')
251 .uponReceiving('a request to get a single tag')
256 .withOperation('GetTag')
258 `query GetTag($id: ID!) {
278 'Content-Type': 'application/json; charset=utf-8'
282 message: like('An error occurred when fetching the tag')
286 return await internals.provider.addInteraction(tagQuery);
289 test('it returns the error', async () => {
291 const tag = getTag('pineapple');
292 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
294 tag.subscribe((tagValue) => {
299 expect(response.data).toBe(null);
300 expect(response.loading).toBe(true);
301 expect(response.error).toBe(undefined);
302 await resolveAfterTwo;
303 expect(response.data).toBe(null);
304 expect(response.loading).toBe(false);
305 expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{
306 message: 'An error occurred when fetching the tag'