From 355eddb0ba74d1f42fa5735afc9ea4913fb27b6a Mon Sep 17 00:00:00 2001 From: Vedanta Krishna Date: Thu, 16 Nov 2023 17:46:39 +0530 Subject: [PATCH] feat(item): add support to get path of item --- CHANGELOG.yaml | 4 + .../nested-v2-collection-without-name.json | 189 ++++++++++++++++++ examples/nested-v2-collection.json | 13 +- lib/collection/item.js | 15 ++ test/fixtures/index.js | 1 + test/unit/item.test.js | 48 +++++ 6 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 examples/nested-v2-collection-without-name.json diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index df8c18a38..bb8a4c186 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,7 @@ +unreleased: + new features: + - GH-1339 Added getPath method to Item class + 4.2.1: date: 2023-09-11 fixed bugs: diff --git a/examples/nested-v2-collection-without-name.json b/examples/nested-v2-collection-without-name.json new file mode 100644 index 000000000..cdbf46829 --- /dev/null +++ b/examples/nested-v2-collection-without-name.json @@ -0,0 +1,189 @@ +{ + "variables": [], + "info": { + "_postman_id": "e5f2e9cf-173b-c60a-7336-ac804a87d762", + "description": "A simple V2 collection to test out multi level folder flows", + "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" + }, + "item": [ + { + "id": "F1-id", + "description": "", + "item": [ + { + "event": [ + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === 0;" + ] + } + } + ], + "request": { + "url": "https://postman-echo.com/get", + "method": "GET", + "header": [], + "body": {}, + "description": "" + }, + "response": [], + "id": "F1.R1-id" + }, + { + "event": [ + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"1\";" + ] + } + } + ], + "request": { + "url": "https://postman-echo.com/get", + "method": "GET", + "header": [], + "body": {}, + "description": "" + }, + "response": [], + "id": "F1.R2-id" + }, + { + "event": [ + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"2\";" + ] + } + } + ], + "request": { + "url": "https://postman-echo.com/get", + "method": "GET", + "header": [], + "body": {}, + "description": "" + }, + "response": [], + "id": "F1.R3-id" + } + ] + }, + { + "id": "F2-id", + "description": "", + "item": [ + { + "id": "F2.F3-id", + "description": "", + "item": [ + { + "id": "F2.F3.R1-id", + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "var count = parseInt(postman.getEnvironmentVariable(\"count\"));", + "postman.setEnvironmentVariable(\"count\", isNaN(count) ? 0 : count + 1);" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"3\";" + ] + } + } + ], + "request": { + "url": "https://postman-echo.com/get", + "method": "GET", + "header": [], + "body": {}, + "description": "" + }, + "response": [] + } + ] + }, + { + "description": "", + "item": [] + }, + { + "event": [ + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"4\";" + ] + } + } + ], + "request": { + "url": "https://postman-echo.com/get", + "method": "GET", + "header": [], + "body": {}, + "description": "" + }, + "response": [] + } + ] + }, + { + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "var count = parseInt(postman.getEnvironmentVariable(\"count\"));", + "postman.setEnvironmentVariable(\"count\", isNaN(count) ? 0 : count + 1);" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"5\";" + ] + } + } + ], + "request": { + "url": "https://postman-echo.com/get", + "method": "GET", + "header": [], + "body": {}, + "description": "" + }, + "response": [], + "id": "R1-id" + } + ] +} diff --git a/examples/nested-v2-collection.json b/examples/nested-v2-collection.json index 17385af60..2740c6c3f 100644 --- a/examples/nested-v2-collection.json +++ b/examples/nested-v2-collection.json @@ -9,6 +9,7 @@ "item": [ { "name": "F1", + "id": "F1-id", "description": "", "item": [ { @@ -32,7 +33,8 @@ "body": {}, "description": "" }, - "response": [] + "response": [], + "id": "F1.R1-id" }, { "name": "F1.R2", @@ -56,7 +58,8 @@ "body": {}, "description": "" }, - "response": [] + "response": [], + "id": "F1.R2-id" }, { "name": "F1.R3", @@ -85,14 +88,17 @@ }, { "name": "F2", + "id": "F2-id", "description": "", "item": [ { "name": "F2.F3", + "id": "F2.F3-id", "description": "", "item": [ { "name": "F2.F3.R1", + "id": "F2.F3.R1-id", "event": [ { "listen": "prerequest", @@ -187,7 +193,8 @@ "body": {}, "description": "" }, - "response": [] + "response": [], + "id": "R1-id" } ] } diff --git a/lib/collection/item.js b/lib/collection/item.js index 4bc131c2b..9186f0b75 100644 --- a/lib/collection/item.js +++ b/lib/collection/item.js @@ -329,6 +329,21 @@ _.assign(Item.prototype, /** @lends Item.prototype */ { if (!this.request) { this.request = new Request(); } // worst case return this.request.authorizeUsing(type, options); + }, + + /** + * Returns the path of the item + * + * @returns {Array} + */ + getPath: function () { + const path = [], + pushItem = (item) => { path.push(item.name); }; + + pushItem(this); + this.forEachParent({ withRoot: true }, pushItem); + + return path.reverse(); } }); diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 644ceb519..7e565c3e1 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -4,6 +4,7 @@ module.exports = { collectionV2: require('../../examples/collection-v2.json'), nestedCollectionV2: require('../../examples/nested-v2-collection.json'), + nestedCollectionV2WithoutNames: require('../../examples/nested-v2-collection-without-name.json'), rawUrls: [ // If adding to this list, add to the END, or you'll break a lot of tests which diff --git a/test/unit/item.test.js b/test/unit/item.test.js index bbb66c371..ac5108ff1 100644 --- a/test/unit/item.test.js +++ b/test/unit/item.test.js @@ -193,6 +193,54 @@ describe('Item', function () { }); }); + describe('.getPath()', function () { + it('should return correct path for 1 level nested item', function () { + const collection = new sdk.Collection(fixtures.nestedCollectionV2), + req = collection.oneDeep('R1'); + + expect(req.getPath()).to.deep.equal([ + 'multi-level-folders-v2', + 'R1' + ]); + }); + + it('should return correct path for 2 level nested item', function () { + const collection = new sdk.Collection(fixtures.nestedCollectionV2), + req = collection.oneDeep('F1.R1'); + + expect(req.getPath()).to.deep.equal([ + 'multi-level-folders-v2', + 'F1', + 'F1.R1' + ]); + }); + + it('should return correct path for 3 level nested item', function () { + const collection = new sdk.Collection(fixtures.nestedCollectionV2), + req = collection.oneDeep('F2.F3.R1'); + + expect(req.getPath()).to.deep.equal([ + 'multi-level-folders-v2', + 'F2', + 'F2.F3', + 'F2.F3.R1' + ]); + }); + + it('should return path as array of undefined for items without name', function () { + const collection = new sdk.Collection(fixtures.nestedCollectionV2WithoutNames), + // This is searching by id, as the item doesn't have a defined name + r1 = collection.oneDeep('F1.R1-id'); + + expect(r1.getPath()).to.deep.equal([ + undefined, + undefined, + undefined + ]); + }); + }); + + describe('.getAuth()', function () { var item, folder,