]> git.r.bdr.sh - rbdr/forum/blobdiff - src/stores/posts.test.js
Update documentation
[rbdr/forum] / src / stores / posts.test.js
index 5d1fc05f9d73543b0b5f3e2ad57dea2ab0244ee4..ab17286de6f535bffdeb920bc7c4a484b87dcbf7 100644 (file)
@@ -3,7 +3,7 @@ import { resolve } from 'path';
 
 import { resolveAfter } from '$/utils/resolve_after';
 
 
 import { resolveAfter } from '$/utils/resolve_after';
 
-const { like } = Matchers;
+const { eachLike, like } = Matchers;
 
 jest.mock('$/config/config.js');
 
 
 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()
 
     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',
           .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'
+        }]));
+      });
+    });
+  });
 });
 });