]> git.r.bdr.sh - rbdr/forum/blobdiff - src/stores/topics.test.js
Update sveltekit version
[rbdr/forum] / src / stores / topics.test.js
index 280bddb262ee6487aed90b59cbcc464f5fe5dba8..7c3285bd16381815d50299196bc66346de1e8daf 100644 (file)
@@ -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'
+        }]));
+      });
+    });
+  });
 });