Skip to content

Commit b9a4fff

Browse files
committed
Add key packages in Script
1 parent a175ec3 commit b9a4fff

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

examples/collection-v2.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,22 @@
4242
"id": "my-global-script-1",
4343
"script": {
4444
"type": "text/javascript",
45-
"exec": "console.log(\"hello\");"
45+
"exec": [
46+
"const package1 = pm.require(\"package1\");",
47+
"console.log(\"hello\", package1);"
48+
],
49+
"packages": { "package1": { "id": "script-package-1" } }
4650
}
4751
},
4852
{
4953
"listen": "prerequest",
5054
"script": {
5155
"type": "text/javascript",
52-
"exec": "console.log(\"hello\");"
56+
"exec": [
57+
"const package1 = pm.require(\"package1\");",
58+
"console.log(\"hello\", package1);"
59+
],
60+
"packages": { "package1": { "id":"script-package-1" } }
5361
}
5462
}
5563
],

lib/collection/script.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ var _ = require('../util').lodash,
66

77
SCRIPT_NEWLINE_PATTERN = /\r?\n/g;
88

9+
/**
10+
* A map of package names to the IDs of the packages
11+
*
12+
* @typedef {Object.<string, { id: string }>} Packages
13+
*/
14+
915
_.inherit((
1016

1117
/**
@@ -49,6 +55,7 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
4955
* @param {String} [options.type] Script type
5056
* @param {String} [options.src] Script source url
5157
* @param {String[]|String} [options.exec] Script to execute
58+
* @param {Packages} [options.packages] Packages required by the script
5259
*/
5360
update: function (options) {
5461
// no splitting is being done here, as string scripts are split right before assignment below anyway
@@ -62,6 +69,14 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
6269
* @type {string}
6370
*/
6471
this.type = options.type || 'text/javascript';
72+
73+
/**
74+
* The packages required by the script
75+
*
76+
* @type {Packages}
77+
*/
78+
this.packages = options.packages;
79+
6580
_.has(options, 'src') && (
6681

6782
/**

test/unit/event-list.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ describe('EventList', function () {
1010
id: 'my-global-script-1',
1111
script: {
1212
type: 'text/javascript',
13-
exec: 'console.log("hello");'
13+
exec: 'console.log("hello");',
14+
packages: [{ id: 'script-package-1', name: 'package1' }]
1415
}
1516
}];
1617

@@ -35,7 +36,8 @@ describe('EventList', function () {
3536
script: {
3637
id: 'test-script-1',
3738
type: 'text/javascript',
38-
exec: 'console.log("hello");'
39+
exec: 'console.log("hello");',
40+
packages: { package1: { id: 'script-package-1' } }
3941
}
4042
}]),
4143
eventListJSON;
@@ -46,7 +48,8 @@ describe('EventList', function () {
4648
script: {
4749
id: 'test-script-1',
4850
type: 'text/javascript',
49-
exec: ['console.log("hello");']
51+
exec: ['console.log("hello");'],
52+
packages: { package1: { id: 'script-apckage-1' } }
5053
}
5154
}]);
5255

@@ -63,7 +66,8 @@ describe('EventList', function () {
6366
script: {
6467
id: 'test-script-1',
6568
type: 'text/javascript',
66-
exec: ['console.log("hello");']
69+
exec: ['console.log("hello");'],
70+
packages: { package1: { id: 'script-apckage-1' } }
6771
}
6872
});
6973

test/unit/event.test.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Event', function () {
1111
id: 'my-global-script-1',
1212
script: {
1313
type: 'text/javascript',
14-
exec: 'console.log("hello");'
14+
exec: 'console.log("hello");',
15+
packages: {}
1516
}
1617
};
1718

@@ -52,6 +53,7 @@ describe('Event', function () {
5253
expect(postmanEvent).to.have.property('script').that.is.an('object');
5354
expect(postmanEvent.script).to.have.property('type', 'text/javascript');
5455
expect(postmanEvent.script).to.have.property('exec').that.is.an('array');
56+
expect(postmanEvent.script).to.have.property('packages').that.is.an('object');
5557
});
5658
});
5759

@@ -120,7 +122,8 @@ describe('Event', function () {
120122
expect(eventJSON).to.have.property('script').that.has.property('id');
121123
expect(eventJSON.script).to.deep.include({
122124
type: 'text/javascript',
123-
exec: ['console.log("hello");']
125+
exec: ['console.log("hello");'],
126+
packages: {}
124127
});
125128
});
126129

@@ -132,7 +135,8 @@ describe('Event', function () {
132135
expect(beforeJSON).to.have.property('script').that.has.property('id');
133136
expect(beforeJSON.script).to.deep.include({
134137
type: 'text/javascript',
135-
exec: ['console.log("hello");']
138+
exec: ['console.log("hello");'],
139+
packages: {}
136140
});
137141

138142
event.update({ script: { id: 'my-new-script' } });
@@ -142,5 +146,26 @@ describe('Event', function () {
142146
expect(afterJSON.script).to.have.property('id', 'my-new-script');
143147
expect(afterJSON.script.exec).to.be.undefined;
144148
});
149+
150+
it('should not add packages key if not present', function () {
151+
var rawEvent = {
152+
listen: 'test',
153+
id: 'my-global-script-1',
154+
script: {
155+
type: 'text/javascript',
156+
exec: 'console.log("hello");'
157+
}
158+
},
159+
event = new Event(rawEvent),
160+
beforeJSON = event.toJSON(),
161+
afterJSON;
162+
163+
expect(beforeJSON.script).to.not.have.property('packages');
164+
165+
event.update({ script: { exec: 'console.log("updated");' } });
166+
afterJSON = event.toJSON();
167+
168+
expect(afterJSON.script).to.not.have.property('packages');
169+
});
145170
});
146171
});

0 commit comments

Comments
 (0)