aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIdan Amit <ia096e@intl.att.com>2018-08-13 17:58:53 +0300
committerIdan Amit <ia096e@intl.att.com>2018-08-13 18:55:16 +0300
commite7154335e26e3e68bedd7ef09831982a33ab3aeb (patch)
treeb71ff637504073ff884aa28a4955d573ddcab8c6
parentde26cd4b417cac55e38b8830b729d6443564f2fc (diff)
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 <ia096e@intl.att.com>
-rw-r--r--.gitignore6
-rw-r--r--.npmignore6
-rw-r--r--jest.config.js20
-rw-r--r--lib/base-pubsub.spec.ts183
-rw-r--r--lib/plugin-pubusb.spec.ts53
-rw-r--r--package.json16
-rw-r--r--pom.xml31
-rw-r--r--version.properties2
8 files changed, 306 insertions, 11 deletions
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": [
+ "<rootDir>/lib"
+ ],
+ "transform": {
+ "^.+\\.ts?$": "ts-jest"
+ },
+ "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$",
+ "moduleFileExtensions": [
+ "ts",
+ "tsx",
+ "js",
+ "jsx",
+ "json",
+ "node"
+ ],
+ "testURL": "http://localhost/",
+ "coverageDirectory": "<rootDir>/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<string, Array<string>>();
+ 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<string, Array<string>>();
+ 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<string> = ['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 @@
<groupId>org.openecomp.sdc</groupId>
<artifactId>sdc-pubsub</artifactId>
- <version>1.0.20</version>
+ <version>1.0.21</version>
<packaging>pom</packaging>
<name>SDC Plugin Pubsub</name>
<properties>
<nexus.proxy>https://nexus.onap.org</nexus.proxy>
<staging.profile.id>176c31dfe190a</staging.profile.id>
+
+ <sonar.typescript.node>node</sonar.typescript.node>
+ <sonar.sources>lib</sonar.sources>
+ <sonar.tests>lib</sonar.tests>
+ <sonar.test.inclusions>lib/*.spec.ts</sonar.test.inclusions>
+ <sonar.typescript.lcov.reportPaths>coverage/lcov.info</sonar.typescript.lcov.reportPaths>
+ <sonar.testExecutionReportPaths>coverage/test-report.xml</sonar.testExecutionReportPaths>
</properties>
<build>
@@ -20,7 +27,7 @@
<version>3.1.0</version>
<executions>
<execution>
- <id>clean dist folder and compiled files</id>
+ <id>clean dist and coverage folder and compiled files</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
@@ -39,6 +46,9 @@
<directory>${basedir}/dist</directory>
</fileset>
<fileset>
+ <directory>${basedir}/coverage</directory>
+ </fileset>
+ <fileset>
<directory>${basedir}/lib</directory>
<includes>
<include>*.d.ts</include>
@@ -91,6 +101,17 @@
</execution>
<execution>
+ <id>npm test</id>
+ <goals>
+ <goal>npm</goal>
+ </goals>
+ <configuration>
+ <arguments>test -- --coverage</arguments>
+ <npmInheritsProxyConfigFromMaven>false</npmInheritsProxyConfigFromMaven>
+ </configuration>
+ </execution>
+
+ <execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
@@ -101,6 +122,12 @@
</execution>
</executions>
</plugin>
+
+ <plugin>
+ <groupId>org.sonarsource.scanner.maven</groupId>
+ <artifactId>sonar-maven-plugin</artifactId>
+ <version>3.0.2</version>
+ </plugin>
<!-- Staging Plugin -->
<plugin>
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}