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 { post } from './posts';
16 describe('post', () => {
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('GetPost', () => {
34 beforeAll(async () => {
35 const postQuery = new GraphQLInteraction()
36 .given("there's data")
37 .uponReceiving('a request to get a single post')
42 .withOperation('GetPost')
44 `query GetPost($id: ID!) {
64 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
69 'Content-Type': 'application/json; charset=utf-8'
74 id: like('8f75eba5-6989-4dd3-b466-e464546ce374'),
75 text: like('This is a very pacty post'),
76 created_at: like(1619976194937),
78 id: like('a805b3de-cac4-451c-a1e6-f078869c9db9'),
79 handle: like('pacts_person')
82 id: like('5c283ce1-0470-4b98-86f5-1fec9a22c9ac'),
83 title: like('The parent pacts topic')
89 return await internals.provider.addInteraction(postQuery);
92 test('it returns the post', async () => {
93 const post = post('8f75eba5-6989-4dd3-b466-e464546ce374');
94 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
96 post.subscribe((postValue) => {
100 expect(response.data).toBe(null);
101 expect(response.loading).toBe(true);
102 expect(response.error).toBe(undefined);
103 await resolveAfterTwo;
104 expect(response.data).toEqual({
105 id: '8f75eba5-6989-4dd3-b466-e464546ce374',
106 text: 'This is a very pacty post',
107 created_at: 1619976194937,
109 id: 'a805b3de-cac4-451c-a1e6-f078869c9db9',
110 handle: 'pacts_person'
113 id: '5c283ce1-0470-4b98-86f5-1fec9a22c9ac',
114 title: 'The parent pacts topic'
117 expect(response.loading).toBe(false);
118 expect(response.error).toBe(undefined);
123 describe("When there's no data", () => {
124 describe('GetPost', () => {
125 beforeAll(async () => {
126 const postQuery = new GraphQLInteraction()
127 .given("there's no data")
128 .uponReceiving('a request to get a single post')
133 .withOperation('GetPost')
135 `query GetPost($id: ID!) {
155 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
160 'Content-Type': 'application/json; charset=utf-8'
168 return await internals.provider.addInteraction(postQuery);
171 test('it returns the post', async () => {
172 const post = post('8f75eba5-6989-4dd3-b466-e464546ce374');
173 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
175 post.subscribe((postValue) => {
176 response = postValue;
179 expect(response.data).toBe(null);
180 expect(response.loading).toBe(true);
181 expect(response.error).toBe(undefined);
182 await resolveAfterTwo;
183 expect(response.data).toBe(null);
184 expect(response.loading).toBe(false);
185 expect(response.error).toBe(undefined);
190 describe("When there's a server error", () => {
191 describe('GetPost', () => {
192 beforeAll(async () => {
193 const postQuery = new GraphQLInteraction()
194 .given("there's a server error")
195 .uponReceiving('a request to get a single post')
200 .withOperation('GetPost')
202 `query GetPost($id: ID!) {
222 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
227 return await internals.provider.addInteraction(postQuery);
230 test('it returns the error', async () => {
231 const post = post('8f75eba5-6989-4dd3-b466-e464546ce374');
232 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
234 post.subscribe((postValue) => {
235 response = postValue;
238 expect(response.data).toBe(null);
239 expect(response.loading).toBe(true);
240 expect(response.error).toBe(undefined);
241 await resolveAfterTwo;
242 expect(response.data).toBe(null);
243 expect(response.loading).toBe(false);
244 expect(response.error).toBeInstanceOf(Error);
249 describe("When there's an error in the response", () => {
250 describe('GetPost', () => {
251 beforeAll(async () => {
252 const postQuery = new GraphQLInteraction()
253 .given("there's an error in the response")
254 .uponReceiving('a request to get a single post')
259 .withOperation('GetPost')
261 `query GetPost($id: ID!) {
281 id: '8f75eba5-6989-4dd3-b466-e464546ce374'
286 'Content-Type': 'application/json; charset=utf-8'
290 message: like('An error occurred when fetching the post')
294 return await internals.provider.addInteraction(postQuery);
297 test('it returns the error', async () => {
298 const post = post('8f75eba5-6989-4dd3-b466-e464546ce374');
299 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
301 post.subscribe((postValue) => {
302 response = postValue;
305 expect(response.data).toBe(null);
306 expect(response.loading).toBe(true);
307 expect(response.error).toBe(undefined);
308 await resolveAfterTwo;
309 expect(response.data).toBe(null);
310 expect(response.loading).toBe(false);
311 expect(response.error.graphQLErrors).toEqual(
312 expect.arrayContaining([
314 message: 'An error occurred when fetching the post'