Skip to content

Commit 982897b

Browse files
feat(tests): Added unit tests for util functions
1 parent 95ecc3a commit 982897b

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "A webpack loader for Angular that enables string-based module loading with the Angular Router",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "mocha -R spec spec",
8+
"test:watch": "npm run test -- -w"
89
},
910
"keywords": [
1011
"angular2",
@@ -24,5 +25,10 @@
2425
"repository": {
2526
"type": "git",
2627
"url": "git+https://github.com/brandonroberts/angular2-router-loader.git"
28+
},
29+
"devDependencies": {
30+
"mocha": "^3.0.2",
31+
"pmock": "^0.2.3",
32+
"should": "^11.1.0"
2733
}
2834
}

spec/utils.spec.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var should = require('should');
2+
var utils = require('../src/utils');
3+
4+
describe('Utils', function() {
5+
var getRequireString = utils.getRequireString;
6+
7+
var path = 'path';
8+
var name = 'name';
9+
10+
describe('getRequireString', function() {
11+
it('should return a require statement', function() {
12+
getRequireString(path, name).should.eql('require(\'path\')[\'name\']');
13+
});
14+
});
15+
16+
describe('getSyncLoader', function() {
17+
var getSyncLoader = utils.getSyncLoader;
18+
19+
it('should return a synchronous require loadChildren statement', function() {
20+
var result = [
21+
'loadChildren: function() {\n',
22+
' return ' + getRequireString(path, name) + ';\n',
23+
'}'
24+
];
25+
26+
getSyncLoader('path', 'name').should.eql(result.join(''));
27+
});
28+
});
29+
30+
describe('getRequireLoader', function() {
31+
var getRequireLoader = utils.getRequireLoader;
32+
33+
it('should return an asynchronous require loadChildren statement', function() {
34+
var result = [
35+
'loadChildren: () => new Promise(function (resolve) {\n',
36+
' (require as any).ensure([], function (require) {\n',
37+
' resolve(' + getRequireString(path, name) + ');\n',
38+
' });\n',
39+
'})'
40+
];
41+
42+
getRequireLoader('path', 'name').should.eql(result.join(''));
43+
});
44+
});
45+
46+
describe('getSystemLoader', function() {
47+
var getSystemLoader = utils.getSystemLoader;
48+
49+
it('should return an asynchronous System.import loadChildren statement', function() {
50+
var result = [
51+
'loadChildren: () => System.import(\'' + path + '\')\n',
52+
' .then(function(module) {\n',
53+
' return module[\'' + name + '\'];\n',
54+
' })'
55+
];
56+
57+
getSystemLoader('path', 'name').should.eql(result.join(''));
58+
});
59+
});
60+
61+
describe('normalizeFilePath', function() {
62+
var pmock = require('pmock');
63+
var normalizeFilePath = utils.normalizeFilePath;
64+
var env;
65+
66+
describe('for windows os', function() {
67+
beforeEach(function() {
68+
env = pmock.platform('win32');
69+
});
70+
71+
it('should replace backslashes with forward slashes', function() {
72+
normalizeFilePath('./path').should.eql('.\\\\path');
73+
});
74+
75+
afterEach(function() {
76+
env.reset();
77+
});
78+
});
79+
80+
describe('for non-windows os', function() {
81+
beforeEach(function() {
82+
env = pmock.platform('posix');
83+
});
84+
85+
it('should not replace backslashes', function() {
86+
normalizeFilePath('./path').should.eql('./path');
87+
});
88+
89+
afterEach(function() {
90+
env.reset();
91+
});
92+
});
93+
});
94+
95+
describe('getFilename', function() {
96+
var getFilename = utils.getFilename;
97+
98+
it('should return the filename for a given path without an extension', function() {
99+
getFilename('path/to/module.ngfactory.ts').should.eql('module.ngfactory');
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)