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', () => {
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('GetTag', () => {
34 beforeAll(async () => {
35 const tagQuery = new GraphQLInteraction()
36 .given("there's data")
37 .uponReceiving('a request to get a single tag')
42 .withOperation('GetTag')
44 `query GetTag($id: ID!) {
64 'Content-Type': 'application/json; charset=utf-8'
69 id: like('pineapple'),
71 id: like('cd038ae7-e8b4-4e38-9543-3d697e69ac34'),
72 title: like('This topic is about pineapples'),
73 updated_at: like(1619978944077),
80 return await internals.provider.addInteraction(tagQuery);
83 test('it returns the tag', async () => {
84 const tag = getTag('pineapple');
85 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
87 tag.subscribe((tagValue) => {
91 expect(response.data).toBe(null);
92 expect(response.loading).toBe(true);
93 expect(response.error).toBe(undefined);
94 await resolveAfterTwo;
95 expect(response.data).toEqual({
99 id: 'cd038ae7-e8b4-4e38-9543-3d697e69ac34',
100 title: 'This topic is about pineapples',
101 updated_at: 1619978944077,
106 expect(response.loading).toBe(false);
107 expect(response.error).toBe(undefined);
112 describe("When there's no data", () => {
113 describe('GetTag', () => {
114 beforeAll(async () => {
115 const tagQuery = new GraphQLInteraction()
116 .given("there's no data")
117 .uponReceiving('a request to get a single tag')
122 .withOperation('GetTag')
124 `query GetTag($id: ID!) {
144 'Content-Type': 'application/json; charset=utf-8'
152 return await internals.provider.addInteraction(tagQuery);
155 test('it returns the tag', async () => {
156 const tag = getTag('pineapple');
157 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
159 tag.subscribe((tagValue) => {
163 expect(response.data).toBe(null);
164 expect(response.loading).toBe(true);
165 expect(response.error).toBe(undefined);
166 await resolveAfterTwo;
167 expect(response.data).toBe(null);
168 expect(response.loading).toBe(false);
169 expect(response.error).toBe(undefined);
174 describe("When there's a server error", () => {
175 describe('GetTag', () => {
176 beforeAll(async () => {
177 const tagQuery = new GraphQLInteraction()
178 .given("there's a server error")
179 .uponReceiving('a request to get a single tag')
184 .withOperation('GetTag')
186 `query GetTag($id: ID!) {
206 return await internals.provider.addInteraction(tagQuery);
209 test('it returns the error', async () => {
210 const tag = getTag('pineapple');
211 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
213 tag.subscribe((tagValue) => {
217 expect(response.data).toBe(null);
218 expect(response.loading).toBe(true);
219 expect(response.error).toBe(undefined);
220 await resolveAfterTwo;
221 expect(response.data).toBe(null);
222 expect(response.loading).toBe(false);
223 expect(response.error).toBeInstanceOf(Error);
228 describe("When there's an error in the response", () => {
229 describe('GetTag', () => {
230 beforeAll(async () => {
231 const tagQuery = new GraphQLInteraction()
232 .given("there's an error in the response")
233 .uponReceiving('a request to get a single tag')
238 .withOperation('GetTag')
240 `query GetTag($id: ID!) {
260 'Content-Type': 'application/json; charset=utf-8'
264 message: like('An error occurred when fetching the tag')
268 return await internals.provider.addInteraction(tagQuery);
271 test('it returns the error', async () => {
272 const tag = getTag('pineapple');
273 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
275 tag.subscribe((tagValue) => {
279 expect(response.data).toBe(null);
280 expect(response.loading).toBe(true);
281 expect(response.error).toBe(undefined);
282 await resolveAfterTwo;
283 expect(response.data).toBe(null);
284 expect(response.loading).toBe(false);
285 expect(response.error.graphQLErrors).toEqual(
286 expect.arrayContaining([
288 message: 'An error occurred when fetching the tag'