Skip to content

Commit

Permalink
Add key packages in Script
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Feb 22, 2024
1 parent a175ec3 commit 6b55bcf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
12 changes: 10 additions & 2 deletions examples/collection-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@
"id": "my-global-script-1",
"script": {
"type": "text/javascript",
"exec": "console.log(\"hello\");"
"exec": [
"const package1 = pm.require(\"package1\");",
"console.log(\"hello\", package1);"
],
"packages": { "package1": { "id": "script-package-1" } }
}
},
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": "console.log(\"hello\");"
"exec": [
"const package1 = pm.require(\"package1\");",
"console.log(\"hello\", package1);"
],
"packages": { "package1": { "id":"script-package-1" } }
}
}
],
Expand Down
15 changes: 15 additions & 0 deletions lib/collection/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ var _ = require('../util').lodash,

SCRIPT_NEWLINE_PATTERN = /\r?\n/g;

/**
* A map of package names to the IDs of the packages
*
* @typedef {Object.<string, { id: string }>} Packages
*/

_.inherit((

/**
Expand Down Expand Up @@ -49,6 +55,7 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
* @param {String} [options.type] Script type
* @param {String} [options.src] Script source url
* @param {String[]|String} [options.exec] Script to execute
* @param {Packages} [options.packages] Packages required by the script
*/
update: function (options) {
// no splitting is being done here, as string scripts are split right before assignment below anyway
Expand All @@ -62,6 +69,14 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
* @type {string}
*/
this.type = options.type || 'text/javascript';

/**
* The packages required by the script
*
* @type {Packages}
*/
this.packages = options.packages;

_.has(options, 'src') && (

/**
Expand Down
12 changes: 8 additions & 4 deletions test/unit/event-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('EventList', function () {
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
exec: 'console.log("hello");',
packages: [{ id: 'script-package-1', name: 'package1' }]
}
}];

Expand All @@ -35,7 +36,8 @@ describe('EventList', function () {
script: {
id: 'test-script-1',
type: 'text/javascript',
exec: 'console.log("hello");'
exec: 'console.log("hello");',
packages: { package1: { id: 'script-package-1' } }
}
}]),
eventListJSON;
Expand All @@ -46,7 +48,8 @@ describe('EventList', function () {
script: {
id: 'test-script-1',
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: { package1: { id: 'script-package-1' } }
}
}]);

Expand All @@ -63,7 +66,8 @@ describe('EventList', function () {
script: {
id: 'test-script-1',
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: { package1: { id: 'script-package-1' } }
}
});

Expand Down
31 changes: 28 additions & 3 deletions test/unit/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('Event', function () {
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
exec: 'console.log("hello");',
packages: {}
}
};

Expand Down Expand Up @@ -52,6 +53,7 @@ describe('Event', function () {
expect(postmanEvent).to.have.property('script').that.is.an('object');
expect(postmanEvent.script).to.have.property('type', 'text/javascript');
expect(postmanEvent.script).to.have.property('exec').that.is.an('array');
expect(postmanEvent.script).to.have.property('packages').that.is.an('object');
});
});

Expand Down Expand Up @@ -120,7 +122,8 @@ describe('Event', function () {
expect(eventJSON).to.have.property('script').that.has.property('id');
expect(eventJSON.script).to.deep.include({
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: {}
});
});

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

event.update({ script: { id: 'my-new-script' } });
Expand All @@ -142,5 +146,26 @@ describe('Event', function () {
expect(afterJSON.script).to.have.property('id', 'my-new-script');
expect(afterJSON.script.exec).to.be.undefined;
});

it('should not add packages key if not present', function () {
var rawEvent = {
listen: 'test',
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
}
},
event = new Event(rawEvent),
beforeJSON = event.toJSON(),
afterJSON;

expect(beforeJSON.script).to.not.have.property('packages');

event.update({ script: { exec: 'console.log("updated");' } });
afterJSON = event.toJSON();

expect(afterJSON.script).to.not.have.property('packages');
});
});
});
4 changes: 2 additions & 2 deletions test/unit/script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Script', function () {
var source = script.toSource();

expect(source).to.be.a('string');
expect(source).to.equal(rawScript.exec);
expect(source).to.equal(rawScript.exec.join('\n'));
});
});
});
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('Script', function () {

expect(jsonified).to.deep.include({
type: rawScript.type,
exec: rawScript.exec.split('\n')
exec: rawScript.exec
});
expect(jsonified.src).to.eql(rawScript.src);
});
Expand Down

0 comments on commit 6b55bcf

Please sign in to comment.