Skip to content

Commit

Permalink
feat(tests): Added unit tests for util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Sep 18, 2016
1 parent 95ecc3a commit 982897b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A webpack loader for Angular that enables string-based module loading with the Angular Router",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha -R spec spec",
"test:watch": "npm run test -- -w"
},
"keywords": [
"angular2",
Expand All @@ -24,5 +25,10 @@
"repository": {
"type": "git",
"url": "git+https://github.com/brandonroberts/angular2-router-loader.git"
},
"devDependencies": {
"mocha": "^3.0.2",
"pmock": "^0.2.3",
"should": "^11.1.0"
}
}
102 changes: 102 additions & 0 deletions spec/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var should = require('should');
var utils = require('../src/utils');

describe('Utils', function() {
var getRequireString = utils.getRequireString;

var path = 'path';
var name = 'name';

describe('getRequireString', function() {
it('should return a require statement', function() {
getRequireString(path, name).should.eql('require(\'path\')[\'name\']');
});
});

describe('getSyncLoader', function() {
var getSyncLoader = utils.getSyncLoader;

it('should return a synchronous require loadChildren statement', function() {
var result = [
'loadChildren: function() {\n',
' return ' + getRequireString(path, name) + ';\n',
'}'
];

getSyncLoader('path', 'name').should.eql(result.join(''));
});
});

describe('getRequireLoader', function() {
var getRequireLoader = utils.getRequireLoader;

it('should return an asynchronous require loadChildren statement', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {\n',
' (require as any).ensure([], function (require) {\n',
' resolve(' + getRequireString(path, name) + ');\n',
' });\n',
'})'
];

getRequireLoader('path', 'name').should.eql(result.join(''));
});
});

describe('getSystemLoader', function() {
var getSystemLoader = utils.getSystemLoader;

it('should return an asynchronous System.import loadChildren statement', function() {
var result = [
'loadChildren: () => System.import(\'' + path + '\')\n',
' .then(function(module) {\n',
' return module[\'' + name + '\'];\n',
' })'
];

getSystemLoader('path', 'name').should.eql(result.join(''));
});
});

describe('normalizeFilePath', function() {
var pmock = require('pmock');
var normalizeFilePath = utils.normalizeFilePath;
var env;

describe('for windows os', function() {
beforeEach(function() {
env = pmock.platform('win32');
});

it('should replace backslashes with forward slashes', function() {
normalizeFilePath('./path').should.eql('.\\\\path');
});

afterEach(function() {
env.reset();
});
});

describe('for non-windows os', function() {
beforeEach(function() {
env = pmock.platform('posix');
});

it('should not replace backslashes', function() {
normalizeFilePath('./path').should.eql('./path');
});

afterEach(function() {
env.reset();
});
});
});

describe('getFilename', function() {
var getFilename = utils.getFilename;

it('should return the filename for a given path without an extension', function() {
getFilename('path/to/module.ngfactory.ts').should.eql('module.ngfactory');
});
});
});

0 comments on commit 982897b

Please sign in to comment.