Repository: pactumjs/pactum
Pull request #201 (2022-09-03 07:27:15)

fix: use data management in data function handlers
closes: #197 


Patch:
diff --git a/package-lock.json b/package-lock.json
index ff1be38..b64f974 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.2.0",
+  "version": "3.2.1",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index 80c73b2..033d8e2 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "pactum",
-  "version": "3.2.0",
+  "version": "3.2.1",
   "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/helpers/dataProcessor.js b/src/helpers/dataProcessor.js
index e42ef8f..9df7c2b 100644
--- a/src/helpers/dataProcessor.js
+++ b/src/helpers/dataProcessor.js
@@ -103,7 +103,8 @@ const dataProcessor = {
         if (refType === 'F') {
           const [handlerName, ..._args] = refValue.split(':');
           const handlerFun = handler.getDataFuncHandler(handlerName);
-          values.push(handlerFun({ args: _args.length > 0 ? _args[0].split(',') : _args }));
+          const handler_data = handlerFun({ args: _args.length > 0 ? _args[0].split(',') : _args });
+          values.push(this.processDataRefs(handler_data));
         }
         if (refType === 'S') {
           const value = jq(refValue, { data: stash.getDataStore() }).value;


Test patch:
diff --git a/test/unit/dataProcessor.spec.js b/test/unit/dataProcessor.spec.js
index d6dae2a..8b02b86 100644
--- a/test/unit/dataProcessor.spec.js
+++ b/test/unit/dataProcessor.spec.js
@@ -2,6 +2,7 @@ const expect = require('chai').expect;
 
 const dp = require('../../src/helpers/dataProcessor');
 const stash = require('../../src/exports/stash');
+const handler = require('../../src/exports/handler');
 const config = require('../../src/config');
 
 describe('Data Processing - Templates', () => {
@@ -779,4 +780,51 @@ describe('Data Processing - Invalid Data', () => {
     stash.clearDataMaps();
   });
 
+});
+
+
+describe('Data Processing - Functions', () => {
+
+  it('should return object data', () => {
+    handler.addDataFuncHandler('GetFirstName', () => {
+      return { user: 'jon' }
+    });
+    const data = dp.processData('$F{GetFirstName}');
+    expect(data).deep.equals({ user: 'jon' });
+  });
+
+  it('should return data with plane data map patterns', () => {
+    handler.addDataFuncHandler('GetFirstName', () => {
+      return '$M{User.FirstName}'
+    })
+    stash.addDataMap({
+      User: {
+        FirstName: 'Jon'
+      }
+    });
+    dp.processMaps();
+    const data = dp.processData('$F{GetFirstName}');
+    expect(data).deep.equals('Jon');
+  });
+
+  it('should return data with data map patterns inside object', () => {
+    handler.addDataFuncHandler('GetFirstName', () => {
+      return { name: '$M{User.FirstName}' }
+    })
+    stash.addDataMap({
+      User: {
+        FirstName: 'Jon'
+      }
+    });
+    dp.processMaps();
+    const data = dp.processData('$F{GetFirstName}');
+    expect(data).deep.equals({ name: 'Jon' });
+  });
+
+  afterEach(() => {
+    config.data.ref.map.enabled = false;
+    config.data.ref.map.processed = false;
+    stash.clearDataMaps();
+  });
+
 });
\ No newline at end of file