diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-05-03 20:59:55 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-05-03 20:59:55 +0200 |
| commit | 26dfa00e2f4eddbdc71ae4d92ee5676f11413ada (patch) | |
| tree | 9df324ee7c3b9350e521018d35b0b18f5eef4fc9 /src | |
| parent | 3435a35ccb828fe81720d057e38eaa4223e917a7 (diff) | |
Add tests for actions + pacts for graphql errrors
Diffstat (limited to 'src')
| -rw-r--r-- | src/stores/actions.js | 2 | ||||
| -rw-r--r-- | src/stores/actions.test.js | 32 | ||||
| -rw-r--r-- | src/stores/forums.test.js | 140 | ||||
| -rw-r--r-- | src/stores/posts.test.js | 80 | ||||
| -rw-r--r-- | src/stores/tags.test.js | 73 | ||||
| -rw-r--r-- | src/stores/topics.test.js | 91 |
6 files changed, 407 insertions, 11 deletions
diff --git a/src/stores/actions.js b/src/stores/actions.js index ae01906..f7fbc28 100644 --- a/src/stores/actions.js +++ b/src/stores/actions.js @@ -4,7 +4,7 @@ import { derived, writable } from 'svelte/store'; * This is a store to set the actions in the top header. */ -export const actions = writable({}); +const actions = writable({}); export const enableTopicActions = (id) => { diff --git a/src/stores/actions.test.js b/src/stores/actions.test.js new file mode 100644 index 0000000..c650536 --- /dev/null +++ b/src/stores/actions.test.js @@ -0,0 +1,32 @@ +import { enableTopicActions, disableTopicActions, topicActions } from './actions'; + +describe('Topic actions and state', () => { + + test('There should be no topic actions by default', () => { + + topicActions.subscribe((actions) => { + + expect(actions).toBe(undefined); + })(); + }); + + test('enableTopicActions should set the topic id', () => { + + enableTopicActions('free_hat'); + topicActions.subscribe((actions) => { + + expect(actions).toEqual({ + id: 'free_hat' + }); + })(); + }); + + test('disableTopicActions should unset the topic id', () => { + + disableTopicActions(); + topicActions.subscribe((actions) => { + + expect(actions).toEqual(undefined); + })(); + }); +}); diff --git a/src/stores/forums.test.js b/src/stores/forums.test.js index eb4bc07..732964f 100644 --- a/src/stores/forums.test.js +++ b/src/stores/forums.test.js @@ -320,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', @@ -379,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', @@ -433,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' + }])); + }); + }); + }); }); 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' + }])); + }); + }); + }); }); diff --git a/src/stores/tags.test.js b/src/stores/tags.test.js index 177919b..bf4fa56 100644 --- a/src/stores/tags.test.js +++ b/src/stores/tags.test.js @@ -181,14 +181,14 @@ describe('Tags store pact', () => { }); }); - describe('When there\'s an error', () => { + describe('When there\'s a server error', () => { describe('GetTag', () => { beforeAll(async () => { const tagQuery = new GraphQLInteraction() - .given('there\'s an error') + .given('there\'s a server error') .uponReceiving('a request to get a single tag') .withRequest({ path: '/graphql', @@ -239,4 +239,73 @@ describe('Tags store pact', () => { }); }); }); + + describe('When there\'s an error in the response', () => { + + describe('GetTag', () => { + + beforeAll(async () => { + + const tagQuery = new GraphQLInteraction() + .given('there\'s an error in the response') + .uponReceiving('a request to get a single tag') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetTag') + .withQuery( + `query GetTag($id: ID!) { + tag(id: $id) { + id + topics { + id + title + updated_at + ttl + __typename + } + __typename + } + }` + ) + .withVariables({ + id: 'pineapple' + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + errors: eachLike({ + message: like('An error occurred when fetching the tag') + }) + } + }); + return await internals.provider.addInteraction(tagQuery); + }); + + test('it returns the error', async () => { + + const tag = getTag('pineapple'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + tag.subscribe((tagValue) => { + + response = tagValue; + 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 tag' + }])); + }); + }); + }); }); diff --git a/src/stores/topics.test.js b/src/stores/topics.test.js index 280bddb..7c3285b 100644 --- a/src/stores/topics.test.js +++ b/src/stores/topics.test.js @@ -247,14 +247,14 @@ describe('Topics store pact', () => { }); }); - describe('When there\'s an error', () => { + describe('When there\'s a server error', () => { describe('GetTopic', () => { beforeAll(async () => { const topicQuery = new GraphQLInteraction() - .given('there\'s an error') + .given('there\'s a server error') .uponReceiving('a request to get a single topic') .withRequest({ path: '/graphql', @@ -323,4 +323,91 @@ describe('Topics store pact', () => { }); }); }); + + describe('When there\'s an error in the response', () => { + + describe('GetTopic', () => { + + beforeAll(async () => { + + const topicQuery = new GraphQLInteraction() + .given('there\'s an error in the response') + .uponReceiving('a request to get a single topic') + .withRequest({ + path: '/graphql', + method: 'POST' + }) + .withOperation('GetTopic') + .withQuery( + `query GetTopic($id: ID!) { + topic(id: $id) { + id + title + updated_at + ttl + forum { + id + glyph + label + __typename + } + tags { + id + weight + __typename + } + posts { + id + text + created_at + author { + id + handle + __typename + } + __typename + } + __typename + } + }` + ) + .withVariables({ + id: '0b58959d-d448-4a4e-84b6-35e5ac0028d1' + }) + .willRespondWith({ + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + }, + body: { + errors: eachLike({ + message: like('An error occurred when fetching the topic') + }) + } + }); + return await internals.provider.addInteraction(topicQuery); + }); + + test('it returns the error', async () => { + + const topic = getTopic('0b58959d-d448-4a4e-84b6-35e5ac0028d1'); + const { counter, promise: resolveAfterTwo } = resolveAfter(2); + let response = null; + topic.subscribe((topicValue) => { + + response = topicValue; + 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 topic' + }])); + }); + }); + }); }); |