X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/5fc4715f923e5a0935cff2030325608ea6ddc0d6..fb76052a02708a8fafa310d99d7d7b403b4b7ad8:/src/stores/posts.test.js?ds=sidebyside diff --git a/src/stores/posts.test.js b/src/stores/posts.test.js index 5d1fc05..ab17286 100644 --- a/src/stores/posts.test.js +++ b/src/stores/posts.test.js @@ -3,7 +3,7 @@ import { resolve } from 'path'; import { resolveAfter } from '$/utils/resolve_after'; -const { like } = Matchers; +const { eachLike, like } = Matchers; jest.mock('$/config/config.js'); @@ -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', @@ -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' + }])); + }); + }); + }); });