Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isEvent method in Event class #1369

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
- GH-1369 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 an Event instance', function () {
expect(Event.isEvent(new Event(rawEvent))).to.be.true;
});

it('should return false for a raw Event 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
Loading