From 623abd8e654eadcc1f615ceab6f900ff877e28ed Mon Sep 17 00:00:00 2001 From: Appurva Murawat Date: Wed, 31 Jul 2024 21:51:34 +0530 Subject: [PATCH] feat: add `isEvent` method in `Event` class --- CHANGELOG.yaml | 4 ++++ lib/collection/event.js | 13 ++++++++++++- test/unit/event.test.js | 23 +++++++++++++++++++++++ types/index.d.ts | 7 ++++++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index eab1aacf3..703acbb21 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,7 @@ +unreleased: + new features: + - Added `isEvent` method in Event class + 4.4.1: date: 2024-07-29 fixed bugs: diff --git a/lib/collection/event.js b/lib/collection/event.js index 0de498dde..86d44f43a 100644 --- a/lib/collection/event.js +++ b/lib/collection/event.js @@ -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 = { diff --git a/test/unit/event.test.js b/test/unit/event.test.js index 801fecc16..71647b287 100644 --- a/test/unit/event.test.js +++ b/test/unit/event.test.js @@ -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; + }); + }); }); diff --git a/types/index.d.ts b/types/index.d.ts index a680117da..470b3592c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 @@ -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 {