]> git.r.bdr.sh - rbdr/forum/blobdiff - src/stores/forums.test.js
Add pact test for forums store
[rbdr/forum] / src / stores / forums.test.js
diff --git a/src/stores/forums.test.js b/src/stores/forums.test.js
new file mode 100644 (file)
index 0000000..25293c8
--- /dev/null
@@ -0,0 +1,101 @@
+import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
+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';
+
+const internals = {
+  provider: null
+};
+
+describe('Forum store pact', () => {
+
+  beforeAll(async () => {
+
+    internals.provider = new Pact({
+      port: 1234,
+      dir: resolve(process.cwd(), 'pacts'),
+      consumer: 'ForumsStore',
+      provider: 'ForumAPIServer'
+    });
+
+    await internals.provider.setup();
+  });
+
+  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
+            }
+          }`
+        )
+        .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).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);
+    });
+  });
+});