Skip to content

Commit

Permalink
feat(item): add support to get path of item
Browse files Browse the repository at this point in the history
  • Loading branch information
vedkribhu committed Nov 10, 2023
1 parent bd8a1f1 commit 2308b8c
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
unreleased:
new features:
- GH-1339 Added getPath method to Item class

4.2.1:
date: 2023-09-11
fixed bugs:
Expand Down
182 changes: 182 additions & 0 deletions examples/nested-v2-collection-without-name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"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": [
{
"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": []
},
{
"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": []
},
{
"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": []
}
]
},
{
"description": "",
"item": [
{
"description": "",
"item": [
{
"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": []
}
]
}
17 changes: 17 additions & 0 deletions lib/collection/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ _.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<string>}
*/
getPath: function () {
const path = [],
pushItem = (item) => {
path.push(item.name || '');
};

pushItem(this);
this.forEachParent({ withRoot: true }, pushItem);

return path.reverse();
}
});

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/unit/item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ 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 empty array for item without name', function () {
const collection = new sdk.Collection(fixtures.nestedCollectionV2WithoutNames),
f1 = collection.items.members[0],
r1 = f1.items.members[0];

expect(r1.getPath()).to.deep.equal(['', '', '']);
});
});

describe('.getAuth()', function () {
var item,
folder,
Expand Down

0 comments on commit 2308b8c

Please sign in to comment.