X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/b15225c9dff2864ee774a0ab1dcf19d9353ec10b..fb76052a02708a8fafa310d99d7d7b403b4b7ad8:/src/stores/posts.test.js?ds=inline diff --git a/src/stores/posts.test.js b/src/stores/posts.test.js index acd843c..ab17286 100644 --- a/src/stores/posts.test.js +++ b/src/stores/posts.test.js @@ -66,7 +66,7 @@ describe('Posts store pact', () => { }` ) .withVariables({ - id: 'freezer' + id: '8f75eba5-6989-4dd3-b466-e464546ce374' }) .willRespondWith({ status: 200, @@ -96,7 +96,7 @@ describe('Posts store pact', () => { test('it returns the post', async () => { - const post = getPost('freezer'); + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); const { counter, promise: resolveAfterTwo } = resolveAfter(2); let response = null; post.subscribe((postValue) => { @@ -162,7 +162,7 @@ describe('Posts store pact', () => { }` ) .withVariables({ - id: 'freezer' + id: '8f75eba5-6989-4dd3-b466-e464546ce374' }) .willRespondWith({ status: 200, @@ -180,7 +180,7 @@ describe('Posts store pact', () => { test('it returns the post', async () => { - const post = getPost('freezer'); + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); const { counter, promise: resolveAfterTwo } = resolveAfter(2); let response = null; post.subscribe((postValue) => { @@ -199,14 +199,14 @@ describe('Posts store pact', () => { }); }); - describe('When there\'s an error', () => { + describe('When there\'s a server error', () => { describe('GetPost', () => { beforeAll(async () => { const postQuery = new GraphQLInteraction() - .given('there\'s an error') + .given('there\'s a server error') .uponReceiving('a request to get a single post') .withRequest({ path: '/graphql', @@ -234,7 +234,7 @@ describe('Posts store pact', () => { }` ) .withVariables({ - id: 'freezer' + id: '8f75eba5-6989-4dd3-b466-e464546ce374' }) .willRespondWith({ status: 500 @@ -244,7 +244,7 @@ describe('Posts store pact', () => { test('it returns the error', async () => { - const post = getPost('freezer'); + const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374'); const { counter, promise: resolveAfterTwo } = resolveAfter(2); let response = null; post.subscribe((postValue) => { @@ -262,4 +262,78 @@ describe('Posts store pact', () => { }); }); }); + + 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 + created_at + author { + id + handle + __typename + } + topic { + id + title + __typename + } + __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' + }])); + }); + }); + }); });