From e7154335e26e3e68bedd7ef09831982a33ab3aeb Mon Sep 17 00:00:00 2001 From: Idan Amit Date: Mon, 13 Aug 2018 17:58:53 +0300 Subject: Added tests to sdc-pubsub Added tests files and coverage to sdc-pubsub Change-Id: I45146ae8d18f229d92afb60208532a1a0ca4192a Issue-ID: SDC-1542 Signed-off-by: Idan Amit --- .gitignore | 6 +- .npmignore | 6 +- jest.config.js | 20 +++++ lib/base-pubsub.spec.ts | 183 ++++++++++++++++++++++++++++++++++++++++++++++ lib/plugin-pubusb.spec.ts | 53 ++++++++++++++ package.json | 16 +++- pom.xml | 31 +++++++- version.properties | 2 +- 8 files changed, 306 insertions(+), 11 deletions(-) create mode 100644 jest.config.js create mode 100644 lib/base-pubsub.spec.ts create mode 100644 lib/plugin-pubusb.spec.ts diff --git a/.gitignore b/.gitignore index e48b1b3..3db6931 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ node_modules/ **/*.js.map **/*.js !webpack.config.js -npm-debug.log +npm-debug.* sdc-pubsub.iml -node/ \ No newline at end of file +node/ +!/jest.config.js +/coverage/ diff --git a/.npmignore b/.npmignore index 56d2e84..f72406b 100644 --- a/.npmignore +++ b/.npmignore @@ -9,8 +9,10 @@ version.properties .gitattributes .idea/ .gitignore -.npm-debug.log +npm-debug.* INFO.yaml sdc-pubsub.iml node/ -pom.xml \ No newline at end of file +pom.xml +/coverage/ +/jest.config.js diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..f4b1f87 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,20 @@ +module.exports = { + "roots": [ + "/lib" + ], + "transform": { + "^.+\\.ts?$": "ts-jest" + }, + "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$", + "moduleFileExtensions": [ + "ts", + "tsx", + "js", + "jsx", + "json", + "node" + ], + "testURL": "http://localhost/", + "coverageDirectory": "/coverage", + "testResultsProcessor": "jest-sonar-reporter" +}; \ No newline at end of file diff --git a/lib/base-pubsub.spec.ts b/lib/base-pubsub.spec.ts new file mode 100644 index 0000000..5737b18 --- /dev/null +++ b/lib/base-pubsub.spec.ts @@ -0,0 +1,183 @@ +declare const window: Window; + +import {BasePubSub} from './base-pubsub'; + +describe('BasePubSub Tests', () => { + let basePubSub: BasePubSub; + + let testSub: string = 'testSub'; + let testWindow = window; + let testSubUrl: string = 'http://127.0.0.1'; + + beforeEach(() => { + basePubSub = new BasePubSub('testId'); + }); + + describe('constructor tests', () => { + it('should init class property', () => { + expect(basePubSub.subscribers.size).toBe(0); + expect(basePubSub.eventsCallbacks.length).toBe(0); + expect(basePubSub.eventsToWait.size).toBe(0); + expect(basePubSub.clientId).toBe('testId'); + expect(basePubSub.lastEventNotified).toBe(''); + }); + }); + + describe('register function tests', () => { + + it('Should add new subscriber with the sent url to subscribers array ' + + 'when calling register function with url', () => { + basePubSub.register(testSub, testWindow, testSubUrl); + + let actualSub = basePubSub.subscribers.get(testSub); + + expect(basePubSub.subscribers.size).toBe(1); + expect(actualSub.window).toBe(testWindow); + expect(actualSub.locationUrl).toBe(testSubUrl); + }); + + it('Should add new subscriber with the window location.href to subscribers array ' + + 'when calling register function without url', () => { + basePubSub.register(testSub, testWindow, undefined); + + let actualSub = basePubSub.subscribers.get(testSub); + + expect(basePubSub.subscribers.size).toBe(1); + expect(actualSub.window).toBe(testWindow); + expect(actualSub.locationUrl).toBe(window.location.href); + }); + }); + + describe('unregister function tests', () => { + + it('Should remove subscriber from subscribers list', () => { + basePubSub.register(testSub, testWindow, testSubUrl); + + expect(basePubSub.subscribers.size).toBe(1); + + basePubSub.unregister(testSub); + + expect(basePubSub.subscribers.size).toBe(0); + }); + }); + + describe('on function tests', () => { + let callback = () => {return true}; + + it('Should add new callback to events callback array', () => { + basePubSub.on(callback); + + expect(basePubSub.eventsCallbacks.length).toBe(1); + + let actualCallback = basePubSub.eventsCallbacks[0]; + + expect(actualCallback).toBe(callback); + }); + + it('Should not add callback to events callback array if it already exists', () => { + basePubSub.on(callback); + + expect(basePubSub.eventsCallbacks.length).toBe(1); + + basePubSub.on(callback); + + expect(basePubSub.eventsCallbacks.length).toBe(1); + }); + }); + + describe('off function tests', () => { + let callback = () => {return true}; + + it('Should remove callback from events callback array', () => { + basePubSub.on(callback); + + expect(basePubSub.eventsCallbacks.length).toBe(1); + + basePubSub.off(callback); + + expect(basePubSub.eventsCallbacks.length).toBe(0); + }); + }); + + describe('isWaitingForEvent function tests', () => { + let eventsMap = new Map>(); + eventsMap.set('eventsKey', ['WINDOW_OUT']); + + beforeEach(() => { + basePubSub.eventsToWait = eventsMap; + }); + + it('Should return true when the event is found in the events to wait array', () => { + let isWaiting = basePubSub.isWaitingForEvent('WINDOW_OUT'); + + expect(isWaiting).toBeTruthy(); + }); + + it('Should return false when the event is not found in the events to wait array', () => { + let isWaiting = basePubSub.isWaitingForEvent('CHECK_IN'); + + expect(isWaiting).toBeFalsy(); + }); + }); + + describe('notify function tests', () => { + let eventType: string = 'CHECK_IN'; + let callback; + beforeEach(() => { + callback = jest.fn(); + }); + + it('should only update the last event notified property when no subscribers registered', () => { + basePubSub.notify(eventType); + + expect(basePubSub.lastEventNotified).toBe(eventType); + }); + + it('should call post message with the right parameters when there are subscribers registered', () => { + testWindow.postMessage = jest.fn(); + basePubSub.register(testSub, testWindow, testSubUrl); + basePubSub.notify(eventType); + + let sub = basePubSub.subscribers.get(testSub); + + let eventObj = { + type: eventType, + data: undefined, + originId: basePubSub.clientId + }; + + expect(sub.window.postMessage).toHaveBeenCalledWith(eventObj, sub.locationUrl); + }); + + it('should execute the callback function when calling notify with subscription function with no subscribers', () => { + let callback = jest.fn(); + + basePubSub.notify(eventType).subscribe(callback); + + expect(callback).toHaveBeenCalled(); + }); + + it('should execute the callback function when calling notify with subscription function ' + + 'with connected subscribers after all been notified', () => { + basePubSub.register(testSub, testWindow, testSubUrl); + + basePubSub.notify(eventType).subscribe(callback); + + expect(callback).toHaveBeenCalled(); + }); + + it('should register an action completed function to pub sub when an event that is in the events to wait list ' + + 'is being fired', () => { + let eventsMap = new Map>(); + eventsMap.set(testSub, ['CHECK_IN']); + basePubSub.on = jest.fn(); + + basePubSub.register(testSub, testWindow, testSubUrl); + basePubSub.eventsToWait = eventsMap; + + basePubSub.notify(eventType).subscribe(callback); + + expect(basePubSub.on).toHaveBeenCalled(); + }); + }) +}); \ No newline at end of file diff --git a/lib/plugin-pubusb.spec.ts b/lib/plugin-pubusb.spec.ts new file mode 100644 index 0000000..1eb6eda --- /dev/null +++ b/lib/plugin-pubusb.spec.ts @@ -0,0 +1,53 @@ +import {PluginPubSub} from './plugin-pubsub'; + +declare const window: Window; + +describe('BasePubSub Tests', () => { + let pluginPubSub: PluginPubSub; + + let testSub: string = 'testSub'; + let testParentUrl: string = 'http://127.0.0.1'; + let testEventsToWait: Array = ['CHECK_IN', 'WINDOW_OUT']; + + beforeEach(() => { + pluginPubSub = new PluginPubSub(testSub, testParentUrl, testEventsToWait); + }); + + describe('constructor tests', () => { + it('should init class property', () => { + expect(pluginPubSub.subscribers.size).toBe(1); + expect(pluginPubSub.eventsCallbacks.length).toBe(0); + expect(pluginPubSub.eventsToWait.size).toBe(0); + expect(pluginPubSub.clientId).toBe('testSub'); + }); + }); + + describe('subscribe function tests', () => { + it('should call notify function with the PLUGIN_REGISTER event and the register data', () => { + pluginPubSub.notify = jest.fn(); + + let wantedRegisterData = { + pluginId: testSub, + eventsToWait: [] + }; + + pluginPubSub.subscribe(); + + expect(pluginPubSub.notify).toHaveBeenCalledWith('PLUGIN_REGISTER', wantedRegisterData); + }) + }); + + describe('unsubscribe function tests', () => { + it('should call notify function with the PLUGIN_UNREGISTER event and the unregister data', () => { + pluginPubSub.notify = jest.fn(); + + let wantedUnregisterData = { + pluginId: testSub, + }; + + pluginPubSub.unsubscribe(); + + expect(pluginPubSub.notify).toHaveBeenCalledWith('PLUGIN_UNREGISTER', wantedUnregisterData); + }) + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index 58bafb7..a075e40 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ { "name": "sdc-pubsub", - "version": "1.0.20", + "version": "1.0.21", "description": "Publish Subscribe library using post message for sdc plugins", "main": "index.js", "author": "Idan Amit", "license": "Apache-2.0", "scripts": { "clean": "rimraf dist", - "build": "tsc && webpack --mode development" + "build": "tsc && webpack --mode development", + "test": "jest" }, "keywords": [ "sdc", @@ -15,11 +16,18 @@ "sdc-pubsub", "onap" ], + "jestSonar": { + "reportPath": "coverage" + }, "devDependencies": { + "@types/jest": "^23.3.1", "awesome-typescript-loader": "^3.1.3", + "jest": "^23.4.2", + "jest-sonar-reporter": "^2.0.0", + "rimraf": "^2.6.2", + "ts-jest": "^23.0.1", "typescript": "2.7.2", "webpack": "4.12.0", - "webpack-cli": "^3.1.0", - "rimraf": "^2.6.2" + "webpack-cli": "^3.1.0" } } diff --git a/pom.xml b/pom.xml index 8407634..d45552a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,13 +4,20 @@ org.openecomp.sdc sdc-pubsub - 1.0.20 + 1.0.21 pom SDC Plugin Pubsub https://nexus.onap.org 176c31dfe190a + + node + lib + lib + lib/*.spec.ts + coverage/lcov.info + coverage/test-report.xml @@ -20,7 +27,7 @@ 3.1.0 - clean dist folder and compiled files + clean dist and coverage folder and compiled files clean clean @@ -38,6 +45,9 @@ ${basedir}/dist + + ${basedir}/coverage + ${basedir}/lib @@ -90,6 +100,17 @@ + + npm test + + npm + + + test -- --coverage + false + + + npm run build @@ -101,6 +122,12 @@ + + + org.sonarsource.scanner.maven + sonar-maven-plugin + 3.0.2 + diff --git a/version.properties b/version.properties index af317a0..b7b958c 100644 --- a/version.properties +++ b/version.properties @@ -5,7 +5,7 @@ major=1 minor=0 -patch=20 +patch=21 base_version=${major}.${minor}.${patch} -- cgit 1.2.3-korg