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