Skip to content

Commit

Permalink
feat: add isEvent method in Event class
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Jul 31, 2024
1 parent a938d04 commit 623abd8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 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:
- Added `isEvent` method in Event class

4.4.1:
date: 2024-07-29
fixed bugs:
Expand Down
13 changes: 12 additions & 1 deletion lib/collection/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,18 @@ _.assign(Event, /** @lends Event */ {
* @readOnly
* @type {String}
*/
_postman_propertyName: 'Event'
_postman_propertyName: 'Event',

/**
* Check whether an object is an instance of {@link Event}.
*
* @param {*} obj -
* @returns {Boolean}
*/
isEvent: function isPostmanEvent (obj) {
return Boolean(obj) && ((obj instanceof Event) ||
_.inSuperChain(obj.constructor, '_postman_propertyName', Event._postman_propertyName));
}
});

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,27 @@ describe('Event', function () {
expect(afterJSON.script).to.not.have.property('packages');
});
});

describe('isEvent', function () {
var rawEvent = {
listen: 'test',
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
}
};

it('should return true for a VariableScope instance', function () {
expect(Event.isEvent(new Event(rawEvent))).to.be.true;
});

it('should return false for a raw VariableScope object', function () {
expect(Event.isEvent(rawEvent)).to.be.false;
});

it('should return false when called without arguments', function () {
expect(Event.isEvent()).to.be.false;
});
});
});
7 changes: 6 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for postman-collection 4.4.0
// Type definitions for postman-collection 4.4.1
// Project: https://github.com/postmanlabs/postman-collection
// Definitions by: PostmanLabs
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
Expand Down Expand Up @@ -649,6 +649,11 @@ declare module "postman-collection" {
* The script that is to be executed when this event is triggered.
*/
script: Script;
/**
* Check whether an object is an instance of Event.
* @param obj - -
*/
static isEvent(obj: any): boolean;
}

export namespace FormParam {
Expand Down

0 comments on commit 623abd8

Please sign in to comment.