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 { getPost } from './posts';
16 describe('Posts 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('GetPost', () => {
38 beforeAll(async () => {
40 const postQuery = new GraphQLInteraction()
41 .given('there\'s data')
42 .uponReceiving('a request to get a single post')
47 .withOperation('GetPost')
49 `query GetPost($id: ID!) {
69 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
74 'Content-Type': 'application/json; charset=utf-8'
79 id: like('8f75eba5-6989-4dd3-b466-e464546ce374'),
80 text: like('This is a very pacty post'),
81 created_at: like(1619976194937),
83 id: like('a805b3de-cac4-451c-a1e6-f078869c9db9'),
84 handle: like('pacts_person')
87 id: like('5c283ce1-0470-4b98-86f5-1fec9a22c9ac'),
88 title: like('The parent pacts topic')
94 return await internals.provider.addInteraction(postQuery);
97 test('it returns the post', async () => {
99 const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
100 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
102 post.subscribe((postValue) => {
104 response = postValue;
107 expect(response.data).toBe(null);
108 expect(response.loading).toBe(true);
109 expect(response.error).toBe(undefined);
110 await resolveAfterTwo;
111 expect(response.data).toEqual({
112 id: '8f75eba5-6989-4dd3-b466-e464546ce374',
113 text: 'This is a very pacty post',
114 created_at: 1619976194937,
116 id: 'a805b3de-cac4-451c-a1e6-f078869c9db9',
117 handle: 'pacts_person'
120 id: '5c283ce1-0470-4b98-86f5-1fec9a22c9ac',
121 title: 'The parent pacts topic'
124 expect(response.loading).toBe(false);
125 expect(response.error).toBe(undefined);
130 describe('When there\'s no data', () => {
132 describe('GetPost', () => {
134 beforeAll(async () => {
136 const postQuery = new GraphQLInteraction()
137 .given('there\'s no data')
138 .uponReceiving('a request to get a single post')
143 .withOperation('GetPost')
145 `query GetPost($id: ID!) {
165 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
170 'Content-Type': 'application/json; charset=utf-8'
178 return await internals.provider.addInteraction(postQuery);
181 test('it returns the post', async () => {
183 const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
184 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
186 post.subscribe((postValue) => {
188 response = postValue;
191 expect(response.data).toBe(null);
192 expect(response.loading).toBe(true);
193 expect(response.error).toBe(undefined);
194 await resolveAfterTwo;
195 expect(response.data).toBe(null);
196 expect(response.loading).toBe(false);
197 expect(response.error).toBe(undefined);
202 describe('When there\'s a server error', () => {
204 describe('GetPost', () => {
206 beforeAll(async () => {
208 const postQuery = new GraphQLInteraction()
209 .given('there\'s a server error')
210 .uponReceiving('a request to get a single post')
215 .withOperation('GetPost')
217 `query GetPost($id: ID!) {
237 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
242 return await internals.provider.addInteraction(postQuery);
245 test('it returns the error', async () => {
247 const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
248 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
250 post.subscribe((postValue) => {
252 response = postValue;
255 expect(response.data).toBe(null);
256 expect(response.loading).toBe(true);
257 expect(response.error).toBe(undefined);
258 await resolveAfterTwo;
259 expect(response.data).toBe(null);
260 expect(response.loading).toBe(false);
261 expect(response.error).toBeInstanceOf(Error);
266 describe('When there\'s an error in the response', () => {
268 describe('GetPost', () => {
270 beforeAll(async () => {
272 const postQuery = new GraphQLInteraction()
273 .given('there\'s an error in the response')
274 .uponReceiving('a request to get a single post')
279 .withOperation('GetPost')
281 `query GetPost($id: ID!) {
301 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
306 'Content-Type': 'application/json; charset=utf-8'
310 message: like('An error occurred when fetching the post')
314 return await internals.provider.addInteraction(postQuery);
317 test('it returns the error', async () => {
319 const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
320 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
322 post.subscribe((postValue) => {
324 response = postValue;
327 expect(response.data).toBe(null);
328 expect(response.loading).toBe(true);
329 expect(response.error).toBe(undefined);
330 await resolveAfterTwo;
331 expect(response.data).toBe(null);
332 expect(response.loading).toBe(false);
333 expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{
334 message: 'An error occurred when fetching the post'