Repository: pactumjs/pactum
Pull request #64 (2021-04-29 14:33:35)

3.0.12
- added `expectError` method
- support multiple interactions by handlers


Patch:
diff --git a/package-lock.json b/package-lock.json
index 35b32e9..d8bbc50 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.0.11",
+  "version": "3.0.12",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index 5bdc733..c7444b8 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.0.11",
+  "version": "3.0.12",
   "description": "REST API Testing Tool for all levels in a Test Pyramid",
   "main": "./src/index.js",
   "types": "./src/index.d.ts",
diff --git a/src/exports/mock.js b/src/exports/mock.js
index 2cd6d01..515b5c2 100644
--- a/src/exports/mock.js
+++ b/src/exports/mock.js
@@ -47,6 +47,8 @@ const mock = {
       }
       if (!Array.isArray(raw)) {
         raw = [raw];
+      } else {
+        alone = false;
       }
       for (let j = 0; j < raw.length; j++) {
         const interaction = new Interaction(raw[j], true);
diff --git a/src/helpers/handler.runner.js b/src/helpers/handler.runner.js
index acce788..dbab7cf 100644
--- a/src/helpers/handler.runner.js
+++ b/src/helpers/handler.runner.js
@@ -5,8 +5,20 @@ const hr = {
 
   interaction(name, data) {
     const mi = handler.getInteractionHandler(name)({ data });
-    if (mi && mi.name) return this.interaction(mi.name, mi.data);
-    return mi;
+    if (Array.isArray(mi)) {
+      const interactions = [];
+      for (let i = 0; i < mi.length; i++) {
+        if (mi[i] && mi[i].name) {
+          interactions.push(this.interaction(mi[i].name, mi[i].data));
+        } else {
+          interactions.push(mi[i]);
+        }
+      }
+      return interactions;
+    } else {
+      if (mi && mi.name) return this.interaction(mi.name, mi.data);
+      return mi;
+    }
   },
 
   capture(name, ctx) {
diff --git a/src/models/Tosser.js b/src/models/Tosser.js
index c93b7d2..e1953d5 100644
--- a/src/models/Tosser.js
+++ b/src/models/Tosser.js
@@ -45,10 +45,15 @@ class Tosser {
   }
 
   async addInteractionsToServer() {
-    const mockIdPromises = [];
+    let mockIdPromises = [];
     for (let i = 0; i < this.interactions.length; i++) {
-      const raw = this.interactions[i];
-      mockIdPromises.push(mock.addInteraction(raw.interaction, raw.data));
+      const { interaction, data } = this.interactions[i];
+      const ids = mock.addInteraction(interaction, data);
+      if (Array.isArray(ids)) {
+        mockIdPromises = mockIdPromises.concat(ids);
+      } else {
+        mockIdPromises.push(ids);
+      }
     }
     this.mockIds = this.mockIds.concat(await Promise.all(mockIdPromises));
   }


Test patch:
diff --git a/test/component/mock.handler.spec.js b/test/component/mock.handler.spec.js
index 1db424f..70fe92a 100644
--- a/test/component/mock.handler.spec.js
+++ b/test/component/mock.handler.spec.js
@@ -1,3 +1,4 @@
+const { expect } = require('chai');
 const mock = require('../../src/exports/mock');
 const pactum = require('../../src/index');
 const handler = pactum.handler;
@@ -50,32 +51,32 @@ describe('Mock Interactions - Handler', () => {
         data: 'child/sub'
       };
     });
-    handler.addInteractionHandler('get sub child', () => {
-      return {
-        name: 'get child',
-        data: 'child/sub'
-      };
+    handler.addInteractionHandler('get users with ids', (ctx) => {
+      const { data } = ctx;
+      const interactions = [];
+      for (let i = 0; i < data.length; i++) {
+        interactions.push({
+          request: {
+            method: 'GET',
+            path: `/api/users/${data[i]}`
+          },
+          response: {
+            status: 200
+          }
+        });
+      }
+      return interactions;
     });
-    handler.addInteractionHandler('get users with id 1 & 2', (ctx) => {
-      return [
-      {
-        request: {
-          method: 'GET',
-          path: `/api/users/${ctx.data[0]}`
-        },
-        response: {
-          status: 200
-        }
-      },
-      {
-        request: {
-          method: 'GET',
-          path: `/api/users/${ctx.data[1]}`
-        },
-        response: {
-          status: 200
-        }
-      }];
+    handler.addInteractionHandler('get parents', (ctx) => {
+      const { data } = ctx;
+      const interactions = [];
+      for (let i = 0; i < data.length; i++) {
+        interactions.push({
+          name: 'get parent',
+          data: data[i]
+        });
+      }
+      return interactions;
     });
   });
 
@@ -108,23 +109,50 @@ describe('Mock Interactions - Handler', () => {
   });
 
   it('GET - handler name & custom data by default mock', async () => {
-    mock.addInteraction({ name: 'get project', data: 1});
+    mock.addInteraction({ name: 'get project', data: 1 });
     await pactum.spec()
       .get('http://localhost:9393/api/projects/1')
       .expectStatus(200);
     mock.clearInteractions();
   });
 
-  it('GET - multiple interactions', async () => {
+  it('GET - single handler - multiple interactions', async () => {
     const specWait = pactum.spec()
-    .get('http://localhost:9393/api/users/1')
-    .expectStatus(200)
+      .get('http://localhost:9393/api/users/1')
+      .expectStatus(200);
     await pactum.spec()
-      .useInteraction('get users with id 1 & 2', [1, 2])
+      .useInteraction('get users with ids', [1, 2])
       .get('http://localhost:9393/api/users/2')
       .expectStatus(200)
       .wait(specWait);
-    mock.clearInteractions();
+  });
+
+  it('GET - single handler - multiple interactions - one interaction not exercised - throws error', async () => {
+    let err;
+    try {
+      const specWait = pactum.spec()
+        .get('http://localhost:9393/api/users/1')
+        .expectStatus(200);
+      await pactum.spec()
+        .useInteraction('get users with ids', [1, 2])
+        .get('http://localhost:9393/api/users/1')
+        .expectStatus(200)
+        .wait(specWait);
+    } catch (error) {
+      err = error;
+    }
+    expect(err).not.undefined;
+  });
+
+  it('GET - single handler - multiple parent interactions', async () => {
+    const specWait = pactum.spec()
+      .get('http://localhost:9393/api/parent/1')
+      .expectStatus(200);
+    await pactum.spec()
+      .useInteraction('get parents', [1, 2])
+      .get('http://localhost:9393/api/parent/2')
+      .expectStatus(200)
+      .wait(specWait);
   });
 
 });