X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/a7cf03c192470cbab13edeb1aec99e0c66dede10..b396db1678e7eef0a03d929fca05e9892590436b:/src/lib/stores/posts.test.ts?ds=inline diff --git a/src/lib/stores/posts.test.ts b/src/lib/stores/posts.test.ts index afc22f3..ef1835d 100644 --- a/src/lib/stores/posts.test.ts +++ b/src/lib/stores/posts.test.ts @@ -10,43 +10,38 @@ jest.mock('$lib/config/config.ts'); import { getPost } from './posts'; const internals = { - provider: null + provider: null }; describe('Posts store pact', () => { - - beforeAll(async () => { - - internals.provider = new Pact({ - port: 1234, - dir: resolve(process.cwd(), 'pacts'), - consumer: 'ForumClient', - provider: 'ForumServer', - pactfileWriteMode: 'update' - }); - - await internals.provider.setup(); - }); - - afterEach(() => internals.provider.verify()); - afterAll(() => internals.provider.finalize()); - - describe('When there\'s data', () => { - - describe('GetPost', () => { - - beforeAll(async () => { - - const postQuery = new GraphQLInteraction() - .given('there\'s data') - .uponReceiving('a request to get a single post') - .withRequest({ - path: '/graphql', - method: 'POST' - }) - .withOperation('GetPost') - .withQuery( - `query GetPost($id: ID!) { + beforeAll(async () => { + internals.provider = new Pact({ + port: 1234, + dir: resolve(process.cwd(), 'pacts'), + consumer: 'ForumClient', + provider: 'ForumServer', + pactfileWriteMode: 'update' + }); + + await internals.provider.setup(); + }); + + afterEach(() => internals.provider.verify()); + afterAll(() => internals.provider.finalize()); + + describe("When there's data", () => { + describe('GetPost', () => { + beforeAll(async () => { + const postQuery = new GraphQLInteraction() + .given("there's data") + .uponReceiving('a request to get a single post') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetPost') + .withQuery( + `query GetPost($id: ID!) { post(id: $id) { id text @@ -64,85 +59,80 @@ describe('Posts store pact', () => { __typename } }` - ) - .withVariables({ - id: '8f75eba5-6989-4dd3-b466-e464546ce374' - }) - .willRespondWith({ - status: 200, - headers: { - 'Content-Type': 'application/json; charset=utf-8' - }, - body: { - data: { - post: like({ - id: like('8f75eba5-6989-4dd3-b466-e464546ce374'), - text: like('This is a very pacty post'), - created_at: like(1619976194937), - author: like({ - id: like('a805b3de-cac4-451c-a1e6-f078869c9db9'), - handle: like('pacts_person') - }), - topic: like({ - id: like('5c283ce1-0470-4b98-86f5-1fec9a22c9ac'), - title: like('The parent pacts topic') - }) - }) - } - } - }); - return await internals.provider.addInteraction(postQuery); - }); - - test('it returns the post', async () => { - - const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); - const { counter, promise: resolveAfterTwo } = resolveAfter(2); - let response = null; - post.subscribe((postValue) => { - - response = postValue; - counter(); - }); - expect(response.data).toBe(null); - expect(response.loading).toBe(true); - expect(response.error).toBe(undefined); - await resolveAfterTwo; - expect(response.data).toEqual({ - id: '8f75eba5-6989-4dd3-b466-e464546ce374', - text: 'This is a very pacty post', - created_at: 1619976194937, - author: { - id: 'a805b3de-cac4-451c-a1e6-f078869c9db9', - handle: 'pacts_person' - }, - topic: { - id: '5c283ce1-0470-4b98-86f5-1fec9a22c9ac', - title: 'The parent pacts topic' - } - }); - expect(response.loading).toBe(false); - expect(response.error).toBe(undefined); - }); - }); - }); - - describe('When there\'s no data', () => { - - describe('GetPost', () => { - - beforeAll(async () => { - - const postQuery = new GraphQLInteraction() - .given('there\'s no data') - .uponReceiving('a request to get a single post') - .withRequest({ - path: '/graphql', - method: 'POST' - }) - .withOperation('GetPost') - .withQuery( - `query GetPost($id: ID!) { + ) + .withVariables({ + id: '8f75eba5-6989-4dd3-b466-e464546ce374' + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + data: { + post: like({ + id: like('8f75eba5-6989-4dd3-b466-e464546ce374'), + text: like('This is a very pacty post'), + created_at: like(1619976194937), + author: like({ + id: like('a805b3de-cac4-451c-a1e6-f078869c9db9'), + handle: like('pacts_person') + }), + topic: like({ + id: like('5c283ce1-0470-4b98-86f5-1fec9a22c9ac'), + title: like('The parent pacts topic') + }) + }) + } + } + }); + return await internals.provider.addInteraction(postQuery); + }); + + test('it returns the post', async () => { + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + post.subscribe((postValue) => { + response = postValue; + counter(); + }); + expect(response.data).toBe(null); + expect(response.loading).toBe(true); + expect(response.error).toBe(undefined); + await resolveAfterTwo; + expect(response.data).toEqual({ + id: '8f75eba5-6989-4dd3-b466-e464546ce374', + text: 'This is a very pacty post', + created_at: 1619976194937, + author: { + id: 'a805b3de-cac4-451c-a1e6-f078869c9db9', + handle: 'pacts_person' + }, + topic: { + id: '5c283ce1-0470-4b98-86f5-1fec9a22c9ac', + title: 'The parent pacts topic' + } + }); + expect(response.loading).toBe(false); + expect(response.error).toBe(undefined); + }); + }); + }); + + describe("When there's no data", () => { + describe('GetPost', () => { + beforeAll(async () => { + const postQuery = new GraphQLInteraction() + .given("there's no data") + .uponReceiving('a request to get a single post') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetPost') + .withQuery( + `query GetPost($id: ID!) { post(id: $id) { id text @@ -160,61 +150,56 @@ describe('Posts store pact', () => { __typename } }` - ) - .withVariables({ - id: '8f75eba5-6989-4dd3-b466-e464546ce374' - }) - .willRespondWith({ - status: 200, - headers: { - 'Content-Type': 'application/json; charset=utf-8' - }, - body: { - data: { - post: null - } - } - }); - return await internals.provider.addInteraction(postQuery); - }); - - test('it returns the post', async () => { - - const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); - const { counter, promise: resolveAfterTwo } = resolveAfter(2); - let response = null; - post.subscribe((postValue) => { - - response = postValue; - counter(); - }); - expect(response.data).toBe(null); - expect(response.loading).toBe(true); - expect(response.error).toBe(undefined); - await resolveAfterTwo; - expect(response.data).toBe(null); - expect(response.loading).toBe(false); - expect(response.error).toBe(undefined); - }); - }); - }); - - describe('When there\'s a server error', () => { - - describe('GetPost', () => { - - beforeAll(async () => { - - const postQuery = new GraphQLInteraction() - .given('there\'s a server error') - .uponReceiving('a request to get a single post') - .withRequest({ - path: '/graphql', - method: 'POST' - }) - .withOperation('GetPost') - .withQuery( - `query GetPost($id: ID!) { + ) + .withVariables({ + id: '8f75eba5-6989-4dd3-b466-e464546ce374' + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + data: { + post: null + } + } + }); + return await internals.provider.addInteraction(postQuery); + }); + + test('it returns the post', async () => { + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + post.subscribe((postValue) => { + response = postValue; + counter(); + }); + expect(response.data).toBe(null); + expect(response.loading).toBe(true); + expect(response.error).toBe(undefined); + await resolveAfterTwo; + expect(response.data).toBe(null); + expect(response.loading).toBe(false); + expect(response.error).toBe(undefined); + }); + }); + }); + + describe("When there's a server error", () => { + describe('GetPost', () => { + beforeAll(async () => { + const postQuery = new GraphQLInteraction() + .given("there's a server error") + .uponReceiving('a request to get a single post') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetPost') + .withQuery( + `query GetPost($id: ID!) { post(id: $id) { id text @@ -232,53 +217,48 @@ describe('Posts store pact', () => { __typename } }` - ) - .withVariables({ - id: '8f75eba5-6989-4dd3-b466-e464546ce374' - }) - .willRespondWith({ - status: 500 - }); - return await internals.provider.addInteraction(postQuery); - }); - - test('it returns the error', async () => { - - const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); - const { counter, promise: resolveAfterTwo } = resolveAfter(2); - let response = null; - post.subscribe((postValue) => { - - response = postValue; - counter(); - }); - expect(response.data).toBe(null); - expect(response.loading).toBe(true); - expect(response.error).toBe(undefined); - await resolveAfterTwo; - expect(response.data).toBe(null); - expect(response.loading).toBe(false); - expect(response.error).toBeInstanceOf(Error); - }); - }); - }); - - describe('When there\'s an error in the response', () => { - - describe('GetPost', () => { - - beforeAll(async () => { - - const postQuery = new GraphQLInteraction() - .given('there\'s an error in the response') - .uponReceiving('a request to get a single post') - .withRequest({ - path: '/graphql', - method: 'POST' - }) - .withOperation('GetPost') - .withQuery( - `query GetPost($id: ID!) { + ) + .withVariables({ + id: '8f75eba5-6989-4dd3-b466-e464546ce374' + }) + .willRespondWith({ + status: 500 + }); + return await internals.provider.addInteraction(postQuery); + }); + + test('it returns the error', async () => { + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + post.subscribe((postValue) => { + response = postValue; + counter(); + }); + expect(response.data).toBe(null); + expect(response.loading).toBe(true); + expect(response.error).toBe(undefined); + await resolveAfterTwo; + expect(response.data).toBe(null); + expect(response.loading).toBe(false); + expect(response.error).toBeInstanceOf(Error); + }); + }); + }); + + describe("When there's an error in the response", () => { + describe('GetPost', () => { + beforeAll(async () => { + const postQuery = new GraphQLInteraction() + .given("there's an error in the response") + .uponReceiving('a request to get a single post') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetPost') + .withQuery( + `query GetPost($id: ID!) { post(id: $id) { id text @@ -296,44 +276,46 @@ describe('Posts store pact', () => { __typename } }` - ) - .withVariables({ - id: '8f75eba5-6989-4dd3-b466-e464546ce374' - }) - .willRespondWith({ - status: 200, - headers: { - 'Content-Type': 'application/json; charset=utf-8' - }, - body: { - errors: eachLike({ - message: like('An error occurred when fetching the post') - }) - } - }); - return await internals.provider.addInteraction(postQuery); - }); - - test('it returns the error', async () => { - - const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); - const { counter, promise: resolveAfterTwo } = resolveAfter(2); - let response = null; - post.subscribe((postValue) => { - - response = postValue; - counter(); - }); - expect(response.data).toBe(null); - expect(response.loading).toBe(true); - expect(response.error).toBe(undefined); - await resolveAfterTwo; - expect(response.data).toBe(null); - expect(response.loading).toBe(false); - expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{ - message: 'An error occurred when fetching the post' - }])); - }); - }); - }); + ) + .withVariables({ + id: '8f75eba5-6989-4dd3-b466-e464546ce374' + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + errors: eachLike({ + message: like('An error occurred when fetching the post') + }) + } + }); + return await internals.provider.addInteraction(postQuery); + }); + + test('it returns the error', async () => { + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + post.subscribe((postValue) => { + response = postValue; + counter(); + }); + expect(response.data).toBe(null); + expect(response.loading).toBe(true); + expect(response.error).toBe(undefined); + await resolveAfterTwo; + expect(response.data).toBe(null); + expect(response.loading).toBe(false); + expect(response.error.graphQLErrors).toEqual( + expect.arrayContaining([ + { + message: 'An error occurred when fetching the post' + } + ]) + ); + }); + }); + }); });