Skip to content

Commit 27ca338

Browse files
committed
Adds bundling and working unit tests
1 parent b2d4673 commit 27ca338

16 files changed

Lines changed: 310 additions & 49 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
node_modules/
2+
dist/

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bower.json
2-
tsconfig.json
2+
tsconfig.json
3+
npm-debug.log

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## v1.1.2
2+
Added unit tests for the service and project restructure updated to use the benefits of webpack and bundling.
13

24
## v1.1.1
35
Recovery fix and added interfaces.
@@ -33,7 +35,7 @@ declare class PubSubService{
3335
```
3436
-------
3537
## v1.0.0
36-
A simple publisher/subscriber service.
38+
A simple publisher/subscriber service.
3739

3840
### Class Overview
3941
```typescript

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
A simple publisher/subscriber service.
44

5+
## Contributers
6+
7+
- [Semih KEŞKEK](http://github.com/sqlProvider)
8+
- [Mert SUSUR](http://github.com/msusur)
9+
510
## Usage
611
- Import service in your codes or download via npm or bower.
712

813
`npm i --save angular2-pubsub` | `bower i --save angular2-pubsub`
914

10-
- Add service to App Module providers.
15+
- Add module bundle to imports in your application.
1116
```typescript
1217
...
1318

14-
import { EventDispatcherService } from './path/to/service/angular2-pubsub.service'; // <= HERE
19+
import { PubSubModule } from 'angular2-pubsub'; // <= HERE
1520

1621
@NgModule({
1722
declarations: [
@@ -22,9 +27,10 @@ declarations: [
2227
imports: [
2328
BrowserModule,
2429
FormsModule,
25-
HttpModule
30+
HttpModule,
31+
PubSubModule.forRoot() // <= AND HERE
2632
],
27-
providers: [EventDispatcherService], // <= AND HERE
33+
providers: [],
2834
bootstrap: [RootComponent]
2935
})
3036

@@ -37,7 +43,7 @@ bootstrap: [RootComponent]
3743
#### Class Overview
3844

3945
```typescript
40-
declare class PubSubService{
46+
declare class PubSubService {
4147
private events: Object;
4248
$pub(event: string, eventObject?: any): void;
4349
$sub(): undefined;
@@ -99,4 +105,15 @@ this.closeSidenavSub = this.pubsub.$sub('pleaseCloseSidenav').subscribe((from) =
99105
});
100106

101107
// => 0
102-
```
108+
```
109+
110+
## Build the source
111+
112+
Follow the steps to run the tests and build the source code.
113+
```sh
114+
npm install
115+
npm test
116+
npm run build
117+
```
118+
Commands above will generate the ready to use bundles under the `./dist` folder.
119+

bower.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "angular2-pubsub",
33
"description": "Pub/Sub service for Angular 2",
4-
"version": "1.1.1",
5-
"main": "",
4+
"version": "1.1.2",
5+
"main": "./umd/angular2-pubsub.js",
66
"authors": [
7-
"Semih KEŞKEK"
7+
"Semih KEŞKEK",
8+
"Mert SUSUR"
89
],
910
"license": "ISC",
1011
"keywords": [

karma-test-runner.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
Error.stackTraceLimit = Infinity;
3+
4+
require('reflect-metadata');
5+
require('zone.js/dist/zone');;
6+
require('zone.js/dist/long-stack-trace-zone');
7+
require('zone.js/dist/async-test');
8+
require('zone.js/dist/fake-async-test');
9+
require('zone.js/dist/sync-test');
10+
require('zone.js/dist/proxy');
11+
require('zone.js/dist/jasmine-patch');
12+
13+
var testing = require('@angular/core/testing');
14+
var browser = require('@angular/platform-browser-dynamic/testing');
15+
16+
testing.TestBed.initTestEnvironment(
17+
browser.BrowserDynamicTestingModule,
18+
browser.platformBrowserDynamicTesting()
19+
);
20+
21+
Object.assign(global, testing);
22+
23+
var testContext = require.context('./src', true, /\.spec\.ts/);
24+
25+
function requireAll(requireContext) {
26+
return requireContext.keys().map(requireContext);
27+
}
28+
29+
var modules = requireAll(testContext);

karma.conf.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = function (config) {
2+
config.set({
3+
browsers: ['PhantomJS'],
4+
frameworks: ['jasmine'],
5+
reporters: ['mocha'],
6+
singleRun: true,
7+
preprocessors: { './karma-test-runner.js': ['webpack', 'sourcemap'] },
8+
files: [
9+
{ pattern: 'node_modules/babel-polyfill/browser.js', instrument: false },
10+
{ pattern: './karma-test-runner.js', watched: false }
11+
],
12+
webpack: require('./webpack.config.test.js'),
13+
14+
webpackServer: { noInfo: true }
15+
});
16+
};

package.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
"name": "angular2-pubsub",
33
"version": "1.1.2",
44
"description": "Pub/Sub service for Angular 2",
5+
"main": "./umd/angular2-pubsub.js",
6+
"module": "./esm/src/index.js",
7+
"typings": "./esm/src/index.d.ts",
58
"repository": {
69
"type": "git",
710
"url": "git+https://github.com/sqlProvider/angular2-pubsub.git"
811
},
912
"scripts": {
10-
"test": "",
11-
"build": ""
13+
"test": "karma start",
14+
"clean": "rm -rf dist",
15+
"build-umd": "webpack --config webpack.config.umd.js",
16+
"build-angular": "ngc -p tsconfig-ngc.json",
17+
"copy-package-info": "cp ./package.json ./dist/package.json && cp ./bower.json ./dist/bower.json",
18+
"build": "npm run clean && npm run build-umd && npm run build-angular && npm run copy-package-info"
1219
},
1320
"keywords": [
1421
"Angular2",
@@ -19,6 +26,13 @@
1926
"name": "Semih KEŞKEK",
2027
"email": "keskeksmh@gmail.com"
2128
},
29+
"contributors": [
30+
{
31+
"name": "Mert Susur",
32+
"email": "mail@mertsusur.com",
33+
"url": "http://github.com/msusur"
34+
}
35+
],
2236
"license": "ISC",
2337
"bugs": {
2438
"url": "https://github.com/sqlProvider/angular2-pubsub/issues"
@@ -37,6 +51,7 @@
3751
},
3852
"devDependencies": {
3953
"@angular/compiler-cli": "^2.4.7",
54+
"@angular/platform-browser-dynamic": "^2.4.7",
4055
"@types/es6-shim": "^0.31.32",
4156
"@types/jasmine": "^2.5.41",
4257
"@types/node": "^7.0.5",
@@ -46,6 +61,7 @@
4661
"codelyzer": "^2.0.0",
4762
"commitizen": "^2.9.5",
4863
"compression-webpack-plugin": "^0.3.2",
64+
"extract-text-webpack-plugin": "^1.0.1",
4965
"imports-loader": "^0.7.0",
5066
"karma": "^1.4.1",
5167
"karma-chrome-launcher": "^2.0.0",
@@ -68,11 +84,11 @@
6884
"tslint": "^4.0.2",
6985
"tslint-loader": "^3.3.0",
7086
"typedoc": "^0.5.0",
71-
"typescript": "~2.0.3",
87+
"typescript": "^2.1.6",
7288
"validate-commit-msg": "^2.8.0",
7389
"webpack": "^1.13.0",
7490
"webpack-dev-server": "^1.16.1",
7591
"webpack-fix-default-import-plugin": "^1.0.1",
7692
"zone.js": "^0.7.4"
7793
}
78-
}
94+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* tslint:disable:no-unused-variable */
2+
3+
import { PubSubService } from './angular2-pubsub.service';
4+
import { Observable, Subscriber } from 'rxjs';
5+
6+
describe('PubSubService', (): void => {
7+
let pubService: PubSubService;
8+
9+
beforeEach(() => {
10+
pubService = new PubSubService();
11+
});
12+
13+
describe('$sub', (): void => {
14+
it('should throw an error when event is falsy', (): void => {
15+
expect(pubService.$sub.bind(undefined)).toThrow();
16+
});
17+
18+
it('should return an observable when there is no callback', (): void => {
19+
let result: any = pubService.$sub('test');
20+
expect(result instanceof Observable).toBeTruthy();
21+
});
22+
23+
it('should return a subscriber when there is a callback specified', (): void => {
24+
let result: any = pubService.$sub('test', (v: any): void => {
25+
'';
26+
});
27+
expect(result instanceof Subscriber).toBeTruthy();
28+
});
29+
});
30+
31+
describe('$pub', (): void => {
32+
it('should throw an error when event is falsy', (): void => {
33+
expect(pubService.$pub.bind(undefined)).toThrow();
34+
});
35+
36+
it('should throw an error when event is not registered', (): void => {
37+
expect(pubService.$pub.bind('not-registered')).toThrow();
38+
});
39+
40+
it('should publish with parameters if the event is registered', (): void => {
41+
let subscriberEventSpy: jasmine.Spy = jasmine.createSpy('subscriberEvent');
42+
pubService.$sub('new-event', subscriberEventSpy);
43+
44+
pubService.$pub('new-event', 'foo');
45+
46+
expect(subscriberEventSpy).toHaveBeenCalledWith('foo');
47+
});
48+
});
49+
});

src/angular2-pubsub.service.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,38 @@ const ServiceName: string = "PubSub Service";
77

88
@Injectable()
99
export class PubSubService implements IPubSubService {
10-
private events = {};
10+
private events = { };
1111

1212
constructor() { }
1313

14-
$sub(event: string): Observable<any>;
15-
$sub(event: string, callback: (value: any) => void): Subscription;
16-
$sub(event: string, callback: (value: any) => void, error: (error: any) => void): Subscription;
17-
$sub(event: string, callback: (value: any) => void, error: (error: any) => void, complete: () => void): Subscription;
18-
$sub(event: string, callback?: (value: any) => void, error?: (error: any) => void, complete?: () => void) {
19-
if (event == undefined) {
14+
public $sub(event: string, callback?: (value: any) => void, error?: (error: any) => void, complete?: () => void) {
15+
if (!event) {
2016
throw new Error(`[${ServiceName}] => Subscription method must get event name.`);
21-
};
17+
}
2218

2319
if (this.events[event] === undefined) {
2420
this.events[event] = new BehaviorSubject<any>(0);
2521
}
2622

27-
if (!callback || typeof callback !== 'function') {
23+
if (typeof callback !== 'function') {
2824
return this.events[event].asObservable();
29-
}
30-
else {
25+
} else {
3126
return this.events[event].asObservable().subscribe(callback, error, complete);
3227
}
3328
}
3429

35-
$pub(event: string, eventObject?: any) {
36-
if (event == undefined) {
30+
public $pub(event: string, eventObject?: any) {
31+
if (!event) {
3732
throw new Error(`[${ServiceName}] => Publish method must get event name.`);
38-
}
39-
else if (this.events[event] === undefined) {
33+
} else if (!this.events[event]) {
4034
throw new Error(`[${ServiceName}] => No recorded events found for ${event}.`);
4135
}
4236

4337
this.events[event].next(eventObject);
4438
}
4539
}
4640

47-
interface IPubSubService {
41+
export interface IPubSubService {
4842
$pub(event: string, eventObject?: any);
49-
$sub: I$sub;
43+
$sub(event: string, callback?: (value: any) => void, error?: (error: any) => void, complete?: () => void);
5044
}
51-
52-
interface I$sub {
53-
(event: string): Observable<any>;
54-
(event: string, callback: (value: any) => void): Subscription;
55-
(event: string, callback: (value: any) => void, error: (error: any) => void): Subscription;
56-
(event: string, callback: (value: any) => void, error: (error: any) => void, complete: () => void): Subscription;
57-
}
58-

0 commit comments

Comments
 (0)