Skip to content

Commit 012946b

Browse files
authored
Merge pull request #1369 from postmanlabs/feature/add-is-event-helper
feat: add `isEvent` method in `Event` class
2 parents 135b0a1 + f6f29b0 commit 012946b

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
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-1369 Added `isEvent` method in Event class
4+
15
4.4.1:
26
date: 2024-07-29
37
fixed bugs:

lib/collection/event.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@ _.assign(Event, /** @lends Event */ {
8787
* @readOnly
8888
* @type {String}
8989
*/
90-
_postman_propertyName: 'Event'
90+
_postman_propertyName: 'Event',
91+
92+
/**
93+
* Check whether an object is an instance of {@link Event}.
94+
*
95+
* @param {*} obj -
96+
* @returns {Boolean}
97+
*/
98+
isEvent: function isPostmanEvent (obj) {
99+
return Boolean(obj) && ((obj instanceof Event) ||
100+
_.inSuperChain(obj.constructor, '_postman_propertyName', Event._postman_propertyName));
101+
}
91102
});
92103

93104
module.exports = {

test/unit/event.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,27 @@ describe('Event', function () {
168168
expect(afterJSON.script).to.not.have.property('packages');
169169
});
170170
});
171+
172+
describe('isEvent', function () {
173+
var rawEvent = {
174+
listen: 'test',
175+
id: 'my-global-script-1',
176+
script: {
177+
type: 'text/javascript',
178+
exec: 'console.log("hello");'
179+
}
180+
};
181+
182+
it('should return true for an Event instance', function () {
183+
expect(Event.isEvent(new Event(rawEvent))).to.be.true;
184+
});
185+
186+
it('should return false for a raw Event object', function () {
187+
expect(Event.isEvent(rawEvent)).to.be.false;
188+
});
189+
190+
it('should return false when called without arguments', function () {
191+
expect(Event.isEvent()).to.be.false;
192+
});
193+
});
171194
});

types/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for postman-collection 4.4.0
1+
// Type definitions for postman-collection 4.4.1
22
// Project: https://github.com/postmanlabs/postman-collection
33
// Definitions by: PostmanLabs
44
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -649,6 +649,11 @@ declare module "postman-collection" {
649649
* The script that is to be executed when this event is triggered.
650650
*/
651651
script: Script;
652+
/**
653+
* Check whether an object is an instance of Event.
654+
* @param obj - -
655+
*/
656+
static isEvent(obj: any): boolean;
652657
}
653658

654659
export namespace FormParam {

0 commit comments

Comments
 (0)