]> git.r.bdr.sh - rbdr/forum/commitdiff
Add tags store pact tests
authorRuben Beltran del Rio <redacted>
Sun, 2 May 2021 18:16:44 +0000 (20:16 +0200)
committerRuben Beltran del Rio <redacted>
Sun, 2 May 2021 18:16:44 +0000 (20:16 +0200)
pacts/forumclient-forumserver.json
src/stores/posts.test.js
src/stores/tags.test.js [new file with mode: 0644]

index b1c7fcbc06839cb8db9dd5471a613de33cab83a4..3933bccffb7a932fd69599e6f8a0ba9edf00854d 100644 (file)
@@ -19,7 +19,7 @@
           "operationName": "GetPost",
           "query": "query GetPost($id: ID!) {\n              post(id: $id) {\n                id\n                text\n                created_at\n                author {\n                  id\n                  handle\n                  __typename\n                }\n                topic {\n                  id\n                  title\n                  __typename\n                }\n                __typename\n              }\n            }",
           "variables": {
-            "id": "freezer"
+            "id": "8f75eba5-6989-4dd3-b466-e464546ce374"
           }
         },
         "matchingRules": {
@@ -98,7 +98,7 @@
           "operationName": "GetPost",
           "query": "query GetPost($id: ID!) {\n              post(id: $id) {\n                id\n                text\n                created_at\n                author {\n                  id\n                  handle\n                  __typename\n                }\n                topic {\n                  id\n                  title\n                  __typename\n                }\n                __typename\n              }\n            }",
           "variables": {
-            "id": "freezer"
+            "id": "8f75eba5-6989-4dd3-b466-e464546ce374"
           }
         },
         "matchingRules": {
           "operationName": "GetPost",
           "query": "query GetPost($id: ID!) {\n              post(id: $id) {\n                id\n                text\n                created_at\n                author {\n                  id\n                  handle\n                  __typename\n                }\n                topic {\n                  id\n                  title\n                  __typename\n                }\n                __typename\n              }\n            }",
           "variables": {
-            "id": "freezer"
+            "id": "8f75eba5-6989-4dd3-b466-e464546ce374"
           }
         },
         "matchingRules": {
         "headers": {
         }
       }
+    },
+    {
+      "description": "a request to get a single tag",
+      "providerState": "there's data",
+      "request": {
+        "method": "POST",
+        "path": "/graphql",
+        "headers": {
+          "content-type": "application/json"
+        },
+        "body": {
+          "operationName": "GetTag",
+          "query": "query GetTag($id: ID!) {\n              tag(id: $id) {\n                id\n                topics {\n                  id\n                  title\n                  updated_at\n                  ttl\n                  __typename\n                }\n                __typename\n              }\n            }",
+          "variables": {
+            "id": "pineapple"
+          }
+        },
+        "matchingRules": {
+          "$.body.query": {
+            "match": "regex",
+            "regex": "query\\s*GetTag\\(\\$id:\\s*ID!\\)\\s*\\{\\s*tag\\(id:\\s*\\$id\\)\\s*\\{\\s*id\\s*topics\\s*\\{\\s*id\\s*title\\s*updated_at\\s*ttl\\s*__typename\\s*\\}\\s*__typename\\s*\\}\\s*\\}"
+          }
+        }
+      },
+      "response": {
+        "status": 200,
+        "headers": {
+          "Content-Type": "application/json; charset=utf-8"
+        },
+        "body": {
+          "data": {
+            "tag": {
+              "id": "pineapple",
+              "topics": [
+                {
+                  "id": "cd038ae7-e8b4-4e38-9543-3d697e69ac34",
+                  "title": "This topic is about pineapples",
+                  "updated_at": 1619978944077,
+                  "ttl": 3555
+                }
+              ]
+            }
+          }
+        },
+        "matchingRules": {
+          "$.body.data.tag.id": {
+            "match": "type"
+          },
+          "$.body.data.tag.topics": {
+            "min": 1
+          },
+          "$.body.data.tag.topics[*].*": {
+            "match": "type"
+          },
+          "$.body.data.tag.topics[*].id": {
+            "match": "type"
+          },
+          "$.body.data.tag.topics[*].title": {
+            "match": "type"
+          },
+          "$.body.data.tag.topics[*].updated_at": {
+            "match": "type"
+          },
+          "$.body.data.tag.topics[*].ttl": {
+            "match": "type"
+          }
+        }
+      }
+    },
+    {
+      "description": "a request to get a single tag",
+      "providerState": "there's no data",
+      "request": {
+        "method": "POST",
+        "path": "/graphql",
+        "headers": {
+          "content-type": "application/json"
+        },
+        "body": {
+          "operationName": "GetTag",
+          "query": "query GetTag($id: ID!) {\n              tag(id: $id) {\n                id\n                topics {\n                  id\n                  title\n                  updated_at\n                  ttl\n                  __typename\n                }\n                __typename\n              }\n            }",
+          "variables": {
+            "id": "pineapple"
+          }
+        },
+        "matchingRules": {
+          "$.body.query": {
+            "match": "regex",
+            "regex": "query\\s*GetTag\\(\\$id:\\s*ID!\\)\\s*\\{\\s*tag\\(id:\\s*\\$id\\)\\s*\\{\\s*id\\s*topics\\s*\\{\\s*id\\s*title\\s*updated_at\\s*ttl\\s*__typename\\s*\\}\\s*__typename\\s*\\}\\s*\\}"
+          }
+        }
+      },
+      "response": {
+        "status": 200,
+        "headers": {
+          "Content-Type": "application/json; charset=utf-8"
+        },
+        "body": {
+          "data": {
+            "tag": null
+          }
+        }
+      }
+    },
+    {
+      "description": "a request to get a single tag",
+      "providerState": "there's an error",
+      "request": {
+        "method": "POST",
+        "path": "/graphql",
+        "headers": {
+          "content-type": "application/json"
+        },
+        "body": {
+          "operationName": "GetTag",
+          "query": "query GetTag($id: ID!) {\n              tag(id: $id) {\n                id\n                topics {\n                  id\n                  title\n                  updated_at\n                  ttl\n                  __typename\n                }\n                __typename\n              }\n            }",
+          "variables": {
+            "id": "pineapple"
+          }
+        },
+        "matchingRules": {
+          "$.body.query": {
+            "match": "regex",
+            "regex": "query\\s*GetTag\\(\\$id:\\s*ID!\\)\\s*\\{\\s*tag\\(id:\\s*\\$id\\)\\s*\\{\\s*id\\s*topics\\s*\\{\\s*id\\s*title\\s*updated_at\\s*ttl\\s*__typename\\s*\\}\\s*__typename\\s*\\}\\s*\\}"
+          }
+        }
+      },
+      "response": {
+        "status": 500,
+        "headers": {
+        }
+      }
     }
   ],
   "metadata": {
index acd843c8400f620c39927d1aeeedf120a44d1968..5d1fc05f9d73543b0b5f3e2ad57dea2ab0244ee4 100644 (file)
@@ -3,7 +3,7 @@ import { resolve } from 'path';
 
 import { resolveAfter } from '$/utils/resolve_after';
 
-const { eachLike, like } = Matchers;
+const { like } = Matchers;
 
 jest.mock('$/config/config.js');
 
@@ -66,7 +66,7 @@ describe('Posts store pact', () => {
             }`
           )
           .withVariables({
-            id: 'freezer'
+            id: '8f75eba5-6989-4dd3-b466-e464546ce374'
           })
           .willRespondWith({
             status: 200,
@@ -96,7 +96,7 @@ describe('Posts store pact', () => {
 
       test('it returns the post', async () => {
 
-        const post = getPost('freezer');
+        const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
         const { counter, promise: resolveAfterTwo } = resolveAfter(2);
         let response = null;
         post.subscribe((postValue) => {
@@ -162,7 +162,7 @@ describe('Posts store pact', () => {
             }`
           )
           .withVariables({
-            id: 'freezer'
+            id: '8f75eba5-6989-4dd3-b466-e464546ce374'
           })
           .willRespondWith({
             status: 200,
@@ -180,7 +180,7 @@ describe('Posts store pact', () => {
 
       test('it returns the post', async () => {
 
-        const post = getPost('freezer');
+        const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
         const { counter, promise: resolveAfterTwo } = resolveAfter(2);
         let response = null;
         post.subscribe((postValue) => {
@@ -234,7 +234,7 @@ describe('Posts store pact', () => {
             }`
           )
           .withVariables({
-            id: 'freezer'
+            id: '8f75eba5-6989-4dd3-b466-e464546ce374'
           })
           .willRespondWith({
             status: 500
@@ -244,7 +244,7 @@ describe('Posts store pact', () => {
 
       test('it returns the error', async () => {
 
-        const post = getPost('freezer');
+        const post = getPost('8f75eba5-6989-4dd3-b466-e464546ce374');
         const { counter, promise: resolveAfterTwo } = resolveAfter(2);
         let response = null;
         post.subscribe((postValue) => {
diff --git a/src/stores/tags.test.js b/src/stores/tags.test.js
new file mode 100644 (file)
index 0000000..177919b
--- /dev/null
@@ -0,0 +1,242 @@
+import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
+import { resolve } from 'path';
+
+import { resolveAfter } from '$/utils/resolve_after';
+
+const { eachLike, like } = Matchers;
+
+jest.mock('$/config/config.js');
+
+import { getTag } from './tags';
+
+const internals = {
+  provider: null
+};
+
+describe('Tags store pact', () => {
+
+  beforeAll(async () => {
+
+    internals.provider = new Pact({
+      port: 1234,
+      dir: resolve(process.cwd(), 'pacts'),
+      consumer: 'ForumClient',
+      provider: 'ForumServer',
+      pactfileWriteMode: 'update'
+    });
+
+    await internals.provider.setup();
+  });
+
+  afterEach(() => internals.provider.verify());
+  afterAll(() => internals.provider.finalize());
+
+  describe('When there\'s data', () => {
+
+    describe('GetTag', () => {
+
+      beforeAll(async () => {
+
+        const tagQuery = new GraphQLInteraction()
+          .given('there\'s data')
+          .uponReceiving('a request to get a single tag')
+          .withRequest({
+            path: '/graphql',
+            method: 'POST'
+          })
+          .withOperation('GetTag')
+          .withQuery(
+            `query GetTag($id: ID!) {
+              tag(id: $id) {
+                id
+                topics {
+                  id
+                  title
+                  updated_at
+                  ttl
+                  __typename
+                }
+                __typename
+              }
+            }`
+          )
+          .withVariables({
+            id: 'pineapple'
+          })
+          .willRespondWith({
+            status: 200,
+            headers: {
+              'Content-Type': 'application/json; charset=utf-8'
+            },
+            body: {
+              data: {
+                tag: {
+                  id: like('pineapple'),
+                  topics: eachLike({
+                    id: like('cd038ae7-e8b4-4e38-9543-3d697e69ac34'),
+                    title: like('This topic is about pineapples'),
+                    updated_at: like(1619978944077),
+                    ttl: like(3555)
+                  })
+                }
+              }
+            }
+          });
+        return await internals.provider.addInteraction(tagQuery);
+      });
+
+      test('it returns the tag', async () => {
+
+        const tag = getTag('pineapple');
+        const { counter, promise: resolveAfterTwo } = resolveAfter(2);
+        let response = null;
+        tag.subscribe((tagValue) => {
+
+          response = tagValue;
+          counter();
+        });
+        expect(response.data).toBe(null);
+        expect(response.loading).toBe(true);
+        expect(response.error).toBe(undefined);
+        await resolveAfterTwo;
+        expect(response.data).toEqual({
+          id: 'pineapple',
+          topics: [{
+            id: 'cd038ae7-e8b4-4e38-9543-3d697e69ac34',
+            title: 'This topic is about pineapples',
+            updated_at: 1619978944077,
+            ttl: 3555
+          }]
+        });
+        expect(response.loading).toBe(false);
+        expect(response.error).toBe(undefined);
+      });
+    });
+  });
+
+  describe('When there\'s no data', () => {
+
+    describe('GetTag', () => {
+
+      beforeAll(async () => {
+
+        const tagQuery = new GraphQLInteraction()
+          .given('there\'s no data')
+          .uponReceiving('a request to get a single tag')
+          .withRequest({
+            path: '/graphql',
+            method: 'POST'
+          })
+          .withOperation('GetTag')
+          .withQuery(
+            `query GetTag($id: ID!) {
+              tag(id: $id) {
+                id
+                topics {
+                  id
+                  title
+                  updated_at
+                  ttl
+                  __typename
+                }
+                __typename
+              }
+            }`
+          )
+          .withVariables({
+            id: 'pineapple'
+          })
+          .willRespondWith({
+            status: 200,
+            headers: {
+              'Content-Type': 'application/json; charset=utf-8'
+            },
+            body: {
+              data: {
+                tag: null
+              }
+            }
+          });
+        return await internals.provider.addInteraction(tagQuery);
+      });
+
+      test('it returns the tag', async () => {
+
+        const tag = getTag('pineapple');
+        const { counter, promise: resolveAfterTwo } = resolveAfter(2);
+        let response = null;
+        tag.subscribe((tagValue) => {
+
+          response = tagValue;
+          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('GetTag', () => {
+
+      beforeAll(async () => {
+
+        const tagQuery = new GraphQLInteraction()
+          .given('there\'s an error')
+          .uponReceiving('a request to get a single tag')
+          .withRequest({
+            path: '/graphql',
+            method: 'POST'
+          })
+          .withOperation('GetTag')
+          .withQuery(
+            `query GetTag($id: ID!) {
+              tag(id: $id) {
+                id
+                topics {
+                  id
+                  title
+                  updated_at
+                  ttl
+                  __typename
+                }
+                __typename
+              }
+            }`
+          )
+          .withVariables({
+            id: 'pineapple'
+          })
+          .willRespondWith({
+            status: 500
+          });
+        return await internals.provider.addInteraction(tagQuery);
+      });
+
+      test('it returns the error', async () => {
+
+        const tag = getTag('pineapple');
+        const { counter, promise: resolveAfterTwo } = resolveAfter(2);
+        let response = null;
+        tag.subscribe((tagValue) => {
+
+          response = tagValue;
+          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);
+      });
+    });
+  });
+});