Skip to content

Commit 19c7186

Browse files
committed
feat(item): add support to get path of item
1 parent bd8a1f1 commit 19c7186

File tree

6 files changed

+307
-3
lines changed

6 files changed

+307
-3
lines changed

CHANGELOG.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
unreleased:
2+
new features:
3+
- GH-1339 Added getPath method to Item class
4+
15
4.2.1:
26
date: 2023-09-11
37
fixed bugs:
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
{
2+
"variables": [],
3+
"info": {
4+
"_postman_id": "e5f2e9cf-173b-c60a-7336-ac804a87d762",
5+
"description": "A simple V2 collection to test out multi level folder flows",
6+
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
7+
},
8+
"item": [
9+
{
10+
"id": "F1-id",
11+
"description": "",
12+
"item": [
13+
{
14+
"event": [
15+
{
16+
"listen": "test",
17+
"script": {
18+
"type": "text/javascript",
19+
"exec": [
20+
"tests[\"Status code is 200\"] = responseCode.code === 200;",
21+
"tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === 0;"
22+
]
23+
}
24+
}
25+
],
26+
"request": {
27+
"url": "https://postman-echo.com/get",
28+
"method": "GET",
29+
"header": [],
30+
"body": {},
31+
"description": ""
32+
},
33+
"response": [],
34+
"id": "F1.R1-id"
35+
},
36+
{
37+
"event": [
38+
{
39+
"listen": "test",
40+
"script": {
41+
"type": "text/javascript",
42+
"exec": [
43+
"tests[\"Status code is 200\"] = responseCode.code === 200;",
44+
"tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"1\";"
45+
]
46+
}
47+
}
48+
],
49+
"request": {
50+
"url": "https://postman-echo.com/get",
51+
"method": "GET",
52+
"header": [],
53+
"body": {},
54+
"description": ""
55+
},
56+
"response": [],
57+
"id": "F1.R2-id"
58+
},
59+
{
60+
"event": [
61+
{
62+
"listen": "test",
63+
"script": {
64+
"type": "text/javascript",
65+
"exec": [
66+
"tests[\"Status code is 200\"] = responseCode.code === 200;",
67+
"tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"2\";"
68+
]
69+
}
70+
}
71+
],
72+
"request": {
73+
"url": "https://postman-echo.com/get",
74+
"method": "GET",
75+
"header": [],
76+
"body": {},
77+
"description": ""
78+
},
79+
"response": [],
80+
"id": "F1.R3-id"
81+
}
82+
]
83+
},
84+
{
85+
"id": "F2-id",
86+
"description": "",
87+
"item": [
88+
{
89+
"id": "F2.F3-id",
90+
"description": "",
91+
"item": [
92+
{
93+
"id": "F2.F3.R1-id",
94+
"event": [
95+
{
96+
"listen": "prerequest",
97+
"script": {
98+
"type": "text/javascript",
99+
"exec": [
100+
"var count = parseInt(postman.getEnvironmentVariable(\"count\"));",
101+
"postman.setEnvironmentVariable(\"count\", isNaN(count) ? 0 : count + 1);"
102+
]
103+
}
104+
},
105+
{
106+
"listen": "test",
107+
"script": {
108+
"type": "text/javascript",
109+
"exec": [
110+
"tests[\"Status code is 200\"] = responseCode.code === 200;",
111+
"tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"3\";"
112+
]
113+
}
114+
}
115+
],
116+
"request": {
117+
"url": "https://postman-echo.com/get",
118+
"method": "GET",
119+
"header": [],
120+
"body": {},
121+
"description": ""
122+
},
123+
"response": []
124+
}
125+
]
126+
},
127+
{
128+
"description": "",
129+
"item": []
130+
},
131+
{
132+
"event": [
133+
{
134+
"listen": "test",
135+
"script": {
136+
"type": "text/javascript",
137+
"exec": [
138+
"tests[\"Status code is 200\"] = responseCode.code === 200;",
139+
"tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"4\";"
140+
]
141+
}
142+
}
143+
],
144+
"request": {
145+
"url": "https://postman-echo.com/get",
146+
"method": "GET",
147+
"header": [],
148+
"body": {},
149+
"description": ""
150+
},
151+
"response": []
152+
}
153+
]
154+
},
155+
{
156+
"event": [
157+
{
158+
"listen": "prerequest",
159+
"script": {
160+
"type": "text/javascript",
161+
"exec": [
162+
"var count = parseInt(postman.getEnvironmentVariable(\"count\"));",
163+
"postman.setEnvironmentVariable(\"count\", isNaN(count) ? 0 : count + 1);"
164+
]
165+
}
166+
},
167+
{
168+
"listen": "test",
169+
"script": {
170+
"type": "text/javascript",
171+
"exec": [
172+
"tests[\"Status code is 200\"] = responseCode.code === 200;",
173+
"tests[\"Request executed in correct order\"] = postman.getEnvironmentVariable(\"count\") === \"5\";"
174+
]
175+
}
176+
}
177+
],
178+
"request": {
179+
"url": "https://postman-echo.com/get",
180+
"method": "GET",
181+
"header": [],
182+
"body": {},
183+
"description": ""
184+
},
185+
"response": [],
186+
"id": "R1-id"
187+
}
188+
]
189+
}

examples/nested-v2-collection.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"item": [
1010
{
1111
"name": "F1",
12+
"id": "F1-id",
1213
"description": "",
1314
"item": [
1415
{
@@ -32,7 +33,8 @@
3233
"body": {},
3334
"description": ""
3435
},
35-
"response": []
36+
"response": [],
37+
"id": "F1.R1-id"
3638
},
3739
{
3840
"name": "F1.R2",
@@ -56,7 +58,8 @@
5658
"body": {},
5759
"description": ""
5860
},
59-
"response": []
61+
"response": [],
62+
"id": "F1.R2-id"
6063
},
6164
{
6265
"name": "F1.R3",
@@ -85,14 +88,17 @@
8588
},
8689
{
8790
"name": "F2",
91+
"id": "F2-id",
8892
"description": "",
8993
"item": [
9094
{
9195
"name": "F2.F3",
96+
"id": "F2.F3-id",
9297
"description": "",
9398
"item": [
9499
{
95100
"name": "F2.F3.R1",
101+
"id": "F2.F3.R1-id",
96102
"event": [
97103
{
98104
"listen": "prerequest",
@@ -187,7 +193,8 @@
187193
"body": {},
188194
"description": ""
189195
},
190-
"response": []
196+
"response": [],
197+
"id": "R1-id"
191198
}
192199
]
193200
}

lib/collection/item.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,26 @@ _.assign(Item.prototype, /** @lends Item.prototype */ {
329329
if (!this.request) { this.request = new Request(); } // worst case
330330

331331
return this.request.authorizeUsing(type, options);
332+
},
333+
334+
/**
335+
* Returns the path of the item
336+
*
337+
* @returns {Array<string>}
338+
*/
339+
getPath: function () {
340+
const path = [],
341+
pushItem = (item) => {
342+
path.push({
343+
id: item.id,
344+
name: item.name
345+
});
346+
};
347+
348+
pushItem(this);
349+
this.forEachParent({ withRoot: true }, pushItem);
350+
351+
return path.reverse();
332352
}
333353
});
334354

test/fixtures/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module.exports = {
55
collectionV2: require('../../examples/collection-v2.json'),
66
nestedCollectionV2: require('../../examples/nested-v2-collection.json'),
7+
nestedCollectionV2WithoutNames: require('../../examples/nested-v2-collection-without-name.json'),
78

89
rawUrls: [
910
// If adding to this list, add to the END, or you'll break a lot of tests which

test/unit/item.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,89 @@ describe('Item', function () {
193193
});
194194
});
195195

196+
describe('.getPath()', function () {
197+
it('should return correct path for 1 level nested item', function () {
198+
const collection = new sdk.Collection(fixtures.nestedCollectionV2),
199+
req = collection.oneDeep('R1');
200+
201+
expect(req.getPath()).to.deep.equal([
202+
{
203+
id: 'e5f2e9cf-173b-c60a-7336-ac804a87d762',
204+
name: 'multi-level-folders-v2'
205+
},
206+
{
207+
id: 'R1-id',
208+
name: 'R1'
209+
}
210+
]);
211+
});
212+
213+
it('should return correct path for 2 level nested item', function () {
214+
const collection = new sdk.Collection(fixtures.nestedCollectionV2),
215+
req = collection.oneDeep('F1.R1');
216+
217+
expect(req.getPath()).to.deep.equal([
218+
{
219+
id: 'e5f2e9cf-173b-c60a-7336-ac804a87d762',
220+
name: 'multi-level-folders-v2'
221+
},
222+
{
223+
id: 'F1-id',
224+
name: 'F1'
225+
},
226+
{
227+
id: 'F1.R1-id',
228+
name: 'F1.R1'
229+
}
230+
]);
231+
});
232+
233+
it('should return correct path for 3 level nested item', function () {
234+
const collection = new sdk.Collection(fixtures.nestedCollectionV2),
235+
req = collection.oneDeep('F2.F3.R1');
236+
237+
expect(req.getPath()).to.deep.equal([
238+
{
239+
id: 'e5f2e9cf-173b-c60a-7336-ac804a87d762',
240+
name: 'multi-level-folders-v2'
241+
},
242+
{
243+
id: 'F2-id',
244+
name: 'F2'
245+
},
246+
{
247+
id: 'F2.F3-id',
248+
name: 'F2.F3'
249+
},
250+
{
251+
id: 'F2.F3.R1-id',
252+
name: 'F2.F3.R1'
253+
}
254+
]);
255+
});
256+
257+
it('should return path as empty array for item without name', function () {
258+
const collection = new sdk.Collection(fixtures.nestedCollectionV2WithoutNames),
259+
// This is searching by id, as the item doesn't have a defined name
260+
r1 = collection.oneDeep('F1.R1-id');
261+
262+
expect(r1.getPath()).to.deep.equal([
263+
{
264+
id: 'e5f2e9cf-173b-c60a-7336-ac804a87d762',
265+
name: undefined
266+
},
267+
{
268+
id: 'F1-id',
269+
name: undefined
270+
},
271+
{
272+
id: 'F1.R1-id',
273+
name: undefined
274+
}
275+
]);
276+
});
277+
});
278+
196279
describe('.getAuth()', function () {
197280
var item,
198281
folder,

0 commit comments

Comments
 (0)