Skip to content

Commit 2f22948

Browse files
committed
Added testing framework and first tests
1 parent 6f2c873 commit 2f22948

File tree

11 files changed

+327
-5
lines changed

11 files changed

+327
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
coverage

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
coverage

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,16 @@ this._tokenService.updatePassword(
141141

142142
#### Example:
143143
```javascript
144-
this._tokenService.get('myResource/1').map(res => res.json()).subscribe(
144+
this._tokenService.get('my-resource/1').map(res => res.json()).subscribe(
145145
res => console.log(res),
146146
error => console.log(error)
147147
);
148148
```
149149

150+
## Testing
151+
```bash
152+
npm test
153+
```
154+
150155
## License
151156
The MIT License (see the [LICENSE](https://github.com/neroniaky/angular2-token/blob/master/LICENSE) file for the full text)

config/karma.conf.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module.exports = function (config) {
2+
var testWebpackConfig = require('./webpack.test.js');
3+
4+
var configuration = {
5+
6+
basePath: '',
7+
frameworks: ['jasmine'],
8+
exclude: [],
9+
files: [{ pattern: './config/spec-bundle.js', watched: false }],
10+
preprocessors: { './config/spec-bundle.js': ['coverage', 'webpack', 'sourcemap'] },
11+
webpack: testWebpackConfig,
12+
13+
coverageReporter: {
14+
dir: 'coverage/',
15+
reporters: [
16+
{ type: 'text-summary' },
17+
{ type: 'json' },
18+
{ type: 'html' }
19+
]
20+
},
21+
22+
webpackServer: { noInfo: true },
23+
reporters: ['mocha', 'coverage'],
24+
port: 9876,
25+
colors: true,
26+
logLevel: config.LOG_INFO,
27+
autoWatch: false,
28+
29+
browsers: [
30+
'Chrome'
31+
],
32+
33+
customLaunchers: {
34+
Chrome_travis_ci: {
35+
base: 'Chrome',
36+
flags: ['--no-sandbox']
37+
}
38+
},
39+
40+
singleRun: true
41+
};
42+
43+
if (process.env.TRAVIS) {
44+
configuration.browsers = ['Chrome_travis_ci'];
45+
}
46+
47+
config.set(configuration);
48+
};

config/spec-bundle.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Error.stackTraceLimit = Infinity;
2+
3+
require('core-js/es6');
4+
require('core-js/es7/reflect');
5+
6+
// Typescript emit helpers polyfill
7+
require('ts-helpers');
8+
9+
require('zone.js/dist/zone');
10+
require('zone.js/dist/long-stack-trace-zone');
11+
require('zone.js/dist/jasmine-patch');
12+
require('zone.js/dist/async-test');
13+
require('zone.js/dist/fake-async-test');
14+
require('zone.js/dist/sync-test');
15+
16+
// RxJS
17+
require('rxjs/Rx');
18+
19+
var testing = require('@angular/core/testing');
20+
var browser = require('@angular/platform-browser-dynamic/testing');
21+
22+
testing.setBaseTestProviders(
23+
browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
24+
browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
25+
);
26+
27+
var testContext = require.context('../src', true, /\.spec\.ts/);
28+
29+
function requireAll(requireContext) {
30+
return requireContext.keys().map(requireContext);
31+
}
32+
33+
var modules = requireAll(testContext);

config/webpack.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports = {
2+
3+
devtool: 'inline-source-map',
4+
5+
resolve: {
6+
extensions: ['', '.ts', '.js'],
7+
root: '../src',
8+
},
9+
10+
module: {
11+
preLoaders: [{
12+
test: /\.js$/,
13+
loader: 'source-map-loader',
14+
exclude: [
15+
'../node_modules/rxjs',
16+
'../node_modules/@angular'
17+
]
18+
}],
19+
loaders: [{
20+
test: /\.ts$/,
21+
loader: 'awesome-typescript-loader',
22+
query: {
23+
compilerOptions: {
24+
removeComments: true
25+
}
26+
},
27+
exclude: [/\.e2e\.ts$/]
28+
}],
29+
postLoaders: [{
30+
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
31+
include: '../src',
32+
exclude: [
33+
/\.(e2e|spec)\.ts$/,
34+
/node_modules/
35+
]
36+
}]
37+
},
38+
39+
node: {
40+
global: 'window',
41+
process: false,
42+
crypto: 'empty',
43+
module: false,
44+
clearImmediate: false,
45+
setImmediate: false
46+
}
47+
};

karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./config/karma.conf.js');

package.json

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "./angular2-token.js",
66
"typings": "./angular2-token.d.ts",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "karma start"
99
},
1010
"keywords": [
1111
"angular2",
@@ -18,6 +18,50 @@
1818
"@angular/common": "2.0.0-rc.4",
1919
"rxjs": "5.0.0-beta.6"
2020
},
21+
"devDependencies": {
22+
"@angular/common": "2.0.0-rc.4",
23+
"@angular/compiler": "2.0.0-rc.4",
24+
"@angular/core": "2.0.0-rc.4",
25+
"@angular/http": "2.0.0-rc.4",
26+
"@angular/platform-browser": "2.0.0-rc.4",
27+
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
28+
"@angular/platform-server": "2.0.0-rc.4",
29+
"ie-shim": "^0.1.0",
30+
"rxjs": "5.0.0-beta.6",
31+
"typescript": "^2.0.0-beta",
32+
"zone.js": "~0.6.12",
33+
34+
"@types/core-js": "^0.9.28",
35+
"@types/hammerjs": "^2.0.28",
36+
"@types/jasmine": "^2.2.29",
37+
"@types/node": "^4.0.29",
38+
"@types/source-map": "^0.1.26",
39+
"@types/webpack": "^1.12.29",
40+
"@types/protractor": "^1.5.16",
41+
42+
"awesome-typescript-loader": "1.1.1",
43+
"source-map-loader": "^0.1.5",
44+
"istanbul-instrumenter-loader": "^0.2.0",
45+
46+
"protractor": "^3.2.2",
47+
48+
"core-js": "^2.4.0",
49+
"http-server": "^0.9.0",
50+
51+
"jasmine-core": "^2.4.1",
52+
"karma": "^0.13.22",
53+
"karma-chrome-launcher": "^1.0.1",
54+
"karma-coverage": "^1.0.0",
55+
"karma-jasmine": "^1.0.2",
56+
"karma-mocha-reporter": "^2.0.0",
57+
"karma-sourcemap-loader": "^0.3.7",
58+
"karma-webpack": "1.7.0",
59+
"remap-istanbul": "^0.6.3",
60+
61+
"ts-helpers": "1.1.1",
62+
"ts-node": "^0.9.1",
63+
"webpack": "^1.13.1"
64+
},
2165
"author": "Jan-Philipp Riethmacher <neroniaky@gmail.com>",
2266
"license": "MIT",
2367
"repository" :

src/angular2-token.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import {Injectable} from '@angular/core';
2-
import {Http, Response, Headers, RequestOptions} from '@angular/http';
3-
import {Observable} from 'rxjs/Observable';
1+
import { Injectable } from '@angular/core';
2+
import { Http, Response, Headers, RequestOptions } from '@angular/http';
3+
import { Observable } from 'rxjs/Observable';
44
import 'rxjs/add/operator/share';
5+
import 'rxjs/add/operator/map';
56

67
import {
78
UserType,

src/angular2-token.spec.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Http, BaseRequestOptions, Response, ResponseOptions, Headers } from '@angular/http';
2+
import { MockBackend } from '@angular/http/testing';
3+
import { provide } from '@angular/core';
4+
import { inject, addProviders } from '@angular/core/testing';
5+
6+
import { Angular2TokenService } from './';
7+
8+
describe('Angular2TokenService', () => {
9+
10+
// Init common test data
11+
let tokenType = 'Bearer';
12+
let uid = 'test@test.com';
13+
let accessToken = 'fJypB1ugmWHJfW6CELNfug';
14+
let client = '5dayGs4hWTi4eKwSifu_mg';
15+
let expiry = '1472108318';
16+
17+
let emptyHeaders = new Headers({
18+
'content-Type': 'application/json'
19+
});
20+
let tokenHeaders = new Headers({
21+
'content-Type': 'application/json',
22+
'token-type': tokenType,
23+
'uid': uid,
24+
'access-token': accessToken,
25+
'client': client,
26+
'expiry': expiry
27+
});
28+
29+
beforeEach(() => {
30+
// Inject HTTP and Angular2TokenService
31+
addProviders([
32+
BaseRequestOptions,
33+
MockBackend,
34+
provide(Http, {
35+
useFactory:
36+
function (backend, defaultOptions) {
37+
return new Http(backend, defaultOptions);
38+
},
39+
deps: [MockBackend, BaseRequestOptions]
40+
}),
41+
Angular2TokenService
42+
]);
43+
44+
// Fake Local Storage
45+
var store = {};
46+
47+
spyOn(localStorage, 'getItem').and.callFake((key: string): String => {
48+
return store[key] || null;
49+
});
50+
spyOn(localStorage, 'removeItem').and.callFake((key: string): void => {
51+
delete store[key];
52+
});
53+
spyOn(localStorage, 'setItem').and.callFake((key: string, value: string): string => {
54+
return store[key] = <string>value;
55+
});
56+
spyOn(localStorage, 'clear').and.callFake(() => {
57+
store = {};
58+
});
59+
});
60+
61+
it('logIn method should send data', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
62+
63+
let logInData = {
64+
email: 'test@test.de',
65+
password: 'password'
66+
}
67+
68+
mockBackend.connections.subscribe(
69+
c => expect(c.request.getBody()).toEqual(JSON.stringify(logInData))
70+
);
71+
72+
tokenService.init();
73+
tokenService.logIn(logInData.email, logInData.password);
74+
}));
75+
76+
it('logIn method should receive headers and set local storage', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
77+
78+
let logInData = {
79+
email: 'test@test.de',
80+
password: 'password'
81+
}
82+
83+
mockBackend.connections.subscribe(
84+
c => c.mockRespond(new Response(
85+
new ResponseOptions({
86+
headers: tokenHeaders
87+
})
88+
))
89+
);
90+
91+
tokenService.init();
92+
tokenService.logIn(logInData.email, logInData.password);
93+
94+
expect(localStorage.getItem('accessToken')).toEqual(accessToken);
95+
expect(localStorage.getItem('client')).toEqual(client);
96+
expect(localStorage.getItem('expiry')).toEqual(expiry);
97+
expect(localStorage.getItem('tokenType')).toEqual(tokenType);
98+
expect(localStorage.getItem('uid')).toEqual(uid);
99+
}));
100+
101+
});

0 commit comments

Comments
 (0)