]> git.r.bdr.sh - rbdr/forum/blobdiff - src/stores/forums.test.js
Add error/empty cases for forums
[rbdr/forum] / src / stores / forums.test.js
index 25293c803d04c80d175ace34d9fe8f5951fa7898..e117828dddc1d1dcded15ae77db16ca459776332 100644 (file)
@@ -3,19 +3,17 @@ import { resolve } from 'path';
 
 import { resolveAfter } from '$/utils/resolve_after';
 
-import { act } from '@testing-library/svelte';
-
 const { eachLike, like } = Matchers;
 
 jest.mock('$/config/config.js');
 
-import { getForums } from './forums';
+import { getForum, getForums } from './forums';
 
 const internals = {
   provider: null
 };
 
-describe('Forum store pact', () => {
+describe('Forums store pact', () => {
 
   beforeAll(async () => {
 
@@ -32,70 +30,406 @@ describe('Forum store pact', () => {
   afterEach(() => internals.provider.verify());
   afterAll(() => internals.provider.finalize());
 
-  describe('there are forums', () => {
-
-    beforeAll(async () => {
-
-      const forumQuery = new GraphQLInteraction()
-        .uponReceiving('a request to list the forums')
-        .withRequest({
-          path: '/graphql',
-          method: 'POST'
-        })
-        .withOperation('GetForums')
-        .withQuery(
-          `query GetForums {
-            forums {
-              id
-              glyph
-              label
-              position
-              __typename
+  describe('When there\'s forums', () => {
+
+    describe('GetForums', () => {
+
+      beforeAll(async () => {
+
+        const forumQuery = new GraphQLInteraction()
+          .given('there\'s forums')
+          .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: {
+              data: {
+                forums: eachLike({
+                  id: like('butter'),
+                  glyph: like('⌘'),
+                  label: like('test_forums.butter'),
+                  position: like(1)
+                })
+              }
+            }
+          });
+        return await internals.provider.addInteraction(forumQuery);
+      });
+
+      test('it returns the forums', 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).toEqual(expect.arrayContaining([{
+          id: 'butter',
+          glyph: '⌘',
+          label: 'test_forums.butter',
+          position: 1
+        }]));
+        expect(response.loading).toBe(false);
+        expect(response.error).toBe(undefined);
+      });
+    });
+
+    describe('GetForum', () => {
+
+      beforeAll(async () => {
+
+        const forumQuery = new GraphQLInteraction()
+          .given('there\'s forums')
+          .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: {
+              data: {
+                forum: like({
+                  id: 'freezer',
+                  glyph: like('✭'),
+                  label: like('test_forums.freezer'),
+                  position: like(3),
+                  topics: eachLike({
+                    id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
+                    title: like('Very pacty topic'),
+                    updated_at: like(1619954611616),
+                    ttl: like(3601)
+                  })
+                })
+              }
             }
-          }`
-        )
-        .withVariables({})
-        .willRespondWith({
-          status: 200,
-          headers: {
-            'Content-Type': 'application/json; charset=utf-8'
-          },
-          body: {
-            data: {
-              forums: eachLike({
-                id: like('butter'),
-                glyph: like('⌘'),
-                label: like('test_forums.butter'),
-                position: like(1)
-              })
+          });
+        return await internals.provider.addInteraction(forumQuery);
+      });
+
+      test('it returns the forum', 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.id).toBe('freezer');
+        expect(response.data.glyph).toBe('✭');
+        expect(response.data.label).toBe('test_forums.freezer');
+        expect(response.data.position).toBe(3);
+        expect(response.data.topics).toEqual(expect.arrayContaining([{
+          id: '629de02c-151a-4db7-bb86-43b2add8a15a',
+          title: 'Very pacty topic',
+          updated_at: 1619954611616,
+          ttl: 3601
+        }]));
+        expect(response.loading).toBe(false);
+        expect(response.error).toBe(undefined);
+      });
+    });
+  });
+
+  describe('When there\'s no forums', () => {
+
+    describe('GetForums', () => {
+
+      beforeAll(async () => {
+
+        const forumQuery = new GraphQLInteraction()
+          .given('there\'s no forums')
+          .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: {
+              data: {
+                forums: []
+              }
+            }
+          });
+        return await internals.provider.addInteraction(forumQuery);
+      });
+
+      test('it returns the forums', 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).toBe(undefined);
+      });
+    });
+
+    describe('GetForum', () => {
+
+      beforeAll(async () => {
+
+        const forumQuery = new GraphQLInteraction()
+          .given('there\'s no forums')
+          .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: {
+              data: {
+                forum: null
+              }
             }
-          }
+          });
+        return await internals.provider.addInteraction(forumQuery);
+      });
+
+      test('it returns the forum', 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).toBe(undefined);
+      });
+    });
+  });
+
+  describe('When there\'s an error', () => {
+
+    describe('GetForums', () => {
+
+      beforeAll(async () => {
+
+        const forumQuery = new GraphQLInteraction()
+          .given('there\'s an error')
+          .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: 500
+          });
+        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();
         });
-      return await internals.provider.addInteraction(forumQuery);
+        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).toBeInstanceOf(Error);
+      });
     });
 
-    test('it returns the forums', async () => {
+    describe('GetForum', () => {
 
-      const forums = getForums();
-      const { counter, promise: resolveAfterTwo } = resolveAfter(2);
-      let response = null;
-      forums.subscribe((forumsValue) => {
+      beforeAll(async () => {
+
+        const forumQuery = new GraphQLInteraction()
+          .given('there\'s an error')
+          .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: 500
+          });
+        return await internals.provider.addInteraction(forumQuery);
+      });
 
-        response = forumsValue;
-        counter();
+      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).toBeInstanceOf(Error);
       });
-      expect(response.data).toEqual(expect.arrayContaining([]));
-      expect(response.loading).toBe(true);
-      expect(response.error).toBe(undefined);
-      await resolveAfterTwo;
-      expect(response.data).toEqual(expect.arrayContaining([{
-        id: 'butter',
-        glyph: '⌘',
-        label: 'test_forums.butter',
-        position: 1
-      }]));
-      expect(response.loading).toBe(false);
-      expect(response.error).toBe(undefined);
     });
   });
 });