Repository: pactumjs/pactum (js)

Issue:
withJson accepts template name
**Is your feature request related to a problem? Please describe.**
Able to use `withTemplate` in spec

**Describe the solution you'd like**
```js
await spec()
 .post('/api/users')
 .withJson('USERS:WORKEMAIL')
 .expectStatus(200)
```

**Describe alternatives you've considered**
NA

**Additional context**
NA


Pull request description:
feat: support data template name in withJson and expectJson
closes #333 

Patch:
diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts
index 431360e..b3d9e27 100644
--- a/src/models/Spec.d.ts
+++ b/src/models/Spec.d.ts
@@ -1,9 +1,9 @@
-import { RequestOptions, IncomingMessage } from 'http';
 import FormData from 'form-data-lite';
+import { IncomingMessage, RequestOptions } from 'http';
+import { Expect } from '../exports/expect';
+import { CaptureHandlerFunction, ExpectHandlerFunction, RetryHandlerFunction } from '../exports/handler';
 import { Interaction } from '../exports/mock';
-import { ExpectHandlerFunction, RetryHandlerFunction, CaptureHandlerFunction } from '../exports/handler';
 import { LogLevel } from '../exports/settings';
-import { Expect } from '../exports/expect';
 
 declare interface RetryOptions {
   /** maximum number of retries - defaults to 3 */
@@ -163,6 +163,7 @@ declare class Spec {
    * @see https://pactumjs.github.io/api/requests/withJson.html
    */
   withJson(json: object): Spec;
+  withJson(templateName: string): Spec;
   withJson(filePath: string): Spec;
 
   /**
@@ -296,14 +297,18 @@ declare class Spec {
    * @see https://pactumjs.github.io/api/assertions/expectJson.html
    */
   expectJson(json: object): Spec;
-  expectJson(path: string, value: any): Spec;
+  expectJson(templateName: string): Spec;
+  expectJson(filePath: string): Spec;
+  expectJson(jsonPath: string, value: any): Spec;
 
   /**
    * expects a partial json object in the response
    * @see https://pactumjs.github.io/api/assertions/expectJsonLike.html
    */
   expectJsonLike(json: object): Spec;
-  expectJsonLike(path: string, value: any): Spec;
+  expectJsonLike(templateName: string): Spec;
+  expectJsonLike(filePath: string): Spec;
+  expectJsonLike(jsonPath: string, value: any): Spec;
 
   /**
    * expects the response to match with json schema
@@ -311,22 +316,28 @@ declare class Spec {
    */
   expectJsonSchema(schema: object): Spec;
   expectJsonSchema(schema: object, options: object): Spec;
-  expectJsonSchema(path: string, schema: object): Spec;
-  expectJsonSchema(path: string, schema: object, options: object): Spec;
+  expectJsonSchema(templateName: string): Spec;
+  expectJsonSchema(filePath: string): Spec;
+  expectJsonSchema(jsonPath: string, schema: object): Spec;
+  expectJsonSchema(jsonPath: string, schema: object, options: object): Spec;
 
   /**
    * expects the json to match with value
    * @see https://pactumjs.github.io/api/assertions/expectJsonMatch.html
    */
   expectJsonMatch(value: object): Spec;
-  expectJsonMatch(path: string, value: object): Spec;
+  expectJsonMatch(templateName: string): Spec;
+  expectJsonMatch(filePath: string): Spec;
+  expectJsonMatch(jsonPath: string, value: object): Spec;
 
   /**
    * expects the json to strictly match with value
    * @see https://pactumjs.github.io/api/assertions/expectJsonMatchStrict.html
    */
   expectJsonMatchStrict(value: object): Spec;
-  expectJsonMatchStrict(path: string, value: object): Spec;
+  expectJsonMatchStrict(templateName: string): Spec;
+  expectJsonMatchStrict(filePath: string): Spec;
+  expectJsonMatchStrict(jsonPath: string, value: object): Spec;
 
   /**
    * expects the json to an array with length
diff --git a/src/models/Spec.js b/src/models/Spec.js
index e55eba0..6fa794b 100644
--- a/src/models/Spec.js
+++ b/src/models/Spec.js
@@ -15,6 +15,7 @@ const hr = require('../helpers/handler.runner');
 const rlc = require('../helpers/reporter.lifeCycle');
 const config = require('../config');
 const { findFile } = require('../helpers/file.utils');
+const stash = require('../exports/stash');
 
 class Spec {
   constructor(name, data) {
@@ -193,7 +194,7 @@ class Spec {
 
   withJson(json) {
     if (typeof json === 'string') {
-      json = JSON.parse(findFile(json));
+      json = get_json_from_template_or_file(json);
     } else if (typeof json !== 'object') {
       throw new PactumRequestError(`Invalid json in request - ${json}`);
     }
@@ -382,13 +383,13 @@ class Spec {
   }
 
   expectJson(path, value) {
-    typeof value === 'undefined' ? this._expect.json.push(getJson(path)) : this._expect.jsonQuery.push({ path, value });
+    typeof value === 'undefined' ? this._expect.json.push(get_json_from_template_or_file(path)) : this._expect.jsonQuery.push({ path, value });
     return this;
   }
   expectJsonAt(...args) { return this.expectJson(...args); }
 
   expectJsonLike(path, value) {
-    typeof value === 'undefined' ? this._expect.jsonLike.push(getJson(path)) : this._expect.jsonQueryLike.push({ path, value });
+    typeof value === 'undefined' ? this._expect.jsonLike.push(get_json_from_template_or_file(path)) : this._expect.jsonQueryLike.push({ path, value });
     return this;
   }
   expectJsonLikeAt(...args) { return this.expectJsonLike(...args); }
@@ -398,7 +399,7 @@ class Spec {
       this._expect.jsonSchemaQuery.push({ path, value, options });
     } else {
       if (typeof value === 'undefined') {
-        this._expect.jsonSchema.push({ value: getJson(path) });
+        this._expect.jsonSchema.push({ value: get_json_from_template_or_file(path) });
       } else {
         if (typeof path === 'object' && typeof value === 'object') {
           this._expect.jsonSchema.push({ value: path, options: value });
@@ -412,13 +413,13 @@ class Spec {
   expectJsonSchemaAt(...args) { return this.expectJsonSchema(...args); }
 
   expectJsonMatch(path, value) {
-    typeof value === 'undefined' ? this._expect.jsonMatch.push(getJson(path)) : this._expect.jsonMatchQuery.push({ path, value });
+    typeof value === 'undefined' ? this._expect.jsonMatch.push(get_json_from_template_or_file(path)) : this._expect.jsonMatchQuery.push({ path, value });
     return this;
   }
   expectJsonMatchAt(...args) { return this.expectJsonMatch(...args); }
 
   expectJsonMatchStrict(path, value) {
-    typeof value === 'undefined' ? this._expect.jsonMatchStrict.push(getJson(path)) : this._expect.jsonMatchStrictQuery.push({ path, value });
+    typeof value === 'undefined' ? this._expect.jsonMatchStrict.push(get_json_from_template_or_file(path)) : this._expect.jsonMatchStrictQuery.push({ path, value });
     return this;
   }
   expectJsonMatchStrictAt(...args) { return this.expectJsonMatchStrict(...args); }
@@ -548,9 +549,13 @@ function validateRequestUrl(request, url) {
   }
 }
 
-function getJson(path) {
+function get_json_from_template_or_file(path) {
   if (typeof path === 'string') {
-    return JSON.parse(findFile(path));
+    if (stash.getDataTemplate()[path]) {
+      return { '@DATA:TEMPLATE@': path };
+    } else {
+      return JSON.parse(findFile(path));
+    }
   }
   return path;
 }


Test patch:
diff --git a/test/component/templates.spec.js b/test/component/templates.spec.js
index 1786f94..5422c97 100644
--- a/test/component/templates.spec.js
+++ b/test/component/templates.spec.js
@@ -34,6 +34,11 @@ describe('Templates & Maps', () => {
     pactum.handler.addDataFuncHandler('GetSum', (ctx) => parseInt(ctx.args[0]) + parseInt(ctx.args[1]));
   });
 
+  after(() => {
+    stash.clearDataTemplates();
+    stash.clearDataMaps();
+  });
+
   it('new user with pure template', async () => {
     await pactum.spec()
       .useInteraction({
@@ -268,9 +273,4 @@ describe('Templates & Maps', () => {
       });
   });
 
-  after(() => {
-    stash.clearDataTemplates();
-    stash.clearDataMaps();
-  });
-
 });
\ No newline at end of file
diff --git a/test/component/withJson.spec.js b/test/component/withJson.spec.js
index ad15793..38a54ff 100644
--- a/test/component/withJson.spec.js
+++ b/test/component/withJson.spec.js
@@ -1,14 +1,20 @@
-const { spec, settings } = require('../../src/index');
+const { spec, settings, stash } = require('../../src/index');
 const expect = require('chai').expect;
 
 describe('withJson', () => {
 
   before(() => {
     settings.setDataDirectory('test/data');
+    stash.addDataTemplate({
+      'RESPONSE:SAMPLE': {
+        "key": "value-1"
+      }
+    });
   });
 
   after(() => {
     settings.setDataDirectory('data');
+    stash.clearDataTemplates();
   });
 
   it('with file in parent folder', async () => {
@@ -55,10 +61,29 @@ describe('withJson', () => {
       await spec()
         .post('http://localhost:9393/file')
         .withJson('invalid-file.json')
-    } catch(error) {
+    } catch (error) {
       err = error;
     }
     expect(err.message).equals(`File Not Found - 'invalid-file.json'`);
-  })
-  
+  });
+
+  it('with template name', async () => {
+    await spec()
+      .useInteraction({
+        request: {
+          method: 'POST',
+          path: '/file',
+          body: {
+            "key": "value-1"
+          }
+        },
+        response: {
+          status: 200
+        }
+      })
+      .post('http://localhost:9393/file')
+      .withJson('RESPONSE:SAMPLE')
+      .expectStatus(200);
+  });
+
 });
\ No newline at end of file