X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/605230e032808155d62ed773cc551b5ae1847a5f..fb76052a02708a8fafa310d99d7d7b403b4b7ad8:/src/stores/forums.test.js?ds=inline diff --git a/src/stores/forums.test.js b/src/stores/forums.test.js index e117828..732964f 100644 --- a/src/stores/forums.test.js +++ b/src/stores/forums.test.js @@ -20,8 +20,9 @@ describe('Forums store pact', () => { internals.provider = new Pact({ port: 1234, dir: resolve(process.cwd(), 'pacts'), - consumer: 'ForumsStore', - provider: 'ForumAPIServer' + consumer: 'ForumClient', + provider: 'ForumServer', + pactfileWriteMode: 'update' }); await internals.provider.setup(); @@ -30,14 +31,14 @@ describe('Forums store pact', () => { afterEach(() => internals.provider.verify()); afterAll(() => internals.provider.finalize()); - describe('When there\'s forums', () => { + describe('When there\'s data', () => { describe('GetForums', () => { beforeAll(async () => { const forumQuery = new GraphQLInteraction() - .given('there\'s forums') + .given('there\'s data') .uponReceiving('a request to list the forums') .withRequest({ path: '/graphql', @@ -106,7 +107,7 @@ describe('Forums store pact', () => { beforeAll(async () => { const forumQuery = new GraphQLInteraction() - .given('there\'s forums') + .given('there\'s data') .uponReceiving('a request to get a single forum') .withRequest({ path: '/graphql', @@ -189,14 +190,14 @@ describe('Forums store pact', () => { }); }); - describe('When there\'s no forums', () => { + describe('When there\'s no data', () => { describe('GetForums', () => { beforeAll(async () => { const forumQuery = new GraphQLInteraction() - .given('there\'s no forums') + .given('there\'s no data') .uponReceiving('a request to list the forums') .withRequest({ path: '/graphql', @@ -256,7 +257,7 @@ describe('Forums store pact', () => { beforeAll(async () => { const forumQuery = new GraphQLInteraction() - .given('there\'s no forums') + .given('there\'s no data') .uponReceiving('a request to get a single forum') .withRequest({ path: '/graphql', @@ -319,14 +320,14 @@ describe('Forums store pact', () => { }); }); - describe('When there\'s an error', () => { + describe('When there\'s a server error', () => { describe('GetForums', () => { beforeAll(async () => { const forumQuery = new GraphQLInteraction() - .given('there\'s an error') + .given('there\'s a server error') .uponReceiving('a request to list the forums') .withRequest({ path: '/graphql', @@ -378,7 +379,7 @@ describe('Forums store pact', () => { beforeAll(async () => { const forumQuery = new GraphQLInteraction() - .given('there\'s an error') + .given('there\'s a server error') .uponReceiving('a request to get a single forum') .withRequest({ path: '/graphql', @@ -432,4 +433,138 @@ describe('Forums store pact', () => { }); }); }); + + describe('When there\'s an error in the response', () => { + + describe('GetForums', () => { + + beforeAll(async () => { + + const forumQuery = new GraphQLInteraction() + .given('there\'s an error in the response') + .uponReceiving('a request to list the forums') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetForums') + .withQuery( + `query GetForums { + forums { + id + glyph + label + position + __typename + } + }` + ) + .withVariables({}) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + errors: eachLike({ + message: like('An error occurred when fetching forums') + }) + } + }); + return await internals.provider.addInteraction(forumQuery); + }); + + test('it returns the error', async () => { + + const forums = getForums(); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + forums.subscribe((forumsValue) => { + + response = forumsValue; + counter(); + }); + expect(response.data).toBeInstanceOf(Array); + expect(response.data.length).toBe(0); + expect(response.loading).toBe(true); + expect(response.error).toBe(undefined); + await resolveAfterTwo; + expect(response.data).toBeInstanceOf(Array); + expect(response.data.length).toBe(0); + expect(response.loading).toBe(false); + expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{ + message: 'An error occurred when fetching forums' + }])); + }); + }); + + describe('GetForum', () => { + + beforeAll(async () => { + + const forumQuery = new GraphQLInteraction() + .given('there\'s an error in the response') + .uponReceiving('a request to get a single forum') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetForum') + .withQuery( + `query GetForum($id: ID!) { + forum(id: $id) { + id + glyph + label + position + topics { + id + title + updated_at + ttl + __typename + } + __typename + } + }` + ) + .withVariables({ + id: 'freezer' + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + errors: eachLike({ + message: like('An error occurred when fetching the forum') + }) + } + }); + return await internals.provider.addInteraction(forumQuery); + }); + + test('it returns the error', async () => { + + const forum = getForum('freezer'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + forum.subscribe((forumsValue) => { + + response = forumsValue; + 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 forum' + }])); + }); + }); + }); });