Skip to content

Commit be50e6d

Browse files
chore(karma): Create testing harness
Closes #1
1 parent f1dd113 commit be50e6d

File tree

12 files changed

+382
-160
lines changed

12 files changed

+382
-160
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ script:
3434
npm install ./ui-router-core ;
3535
fi
3636
- npm run build
37+
- npm run test
3738

3839
sudo: false
3940

karma.conf.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Karma configuration file
2+
var karma = require('karma');
3+
4+
module.exports = function (karma) {
5+
var config = {
6+
singleRun: true,
7+
autoWatch: false,
8+
autoWatchInterval: 0,
9+
10+
// level of logging
11+
// possible values: LOG_DISABLE, LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG
12+
logLevel: "warn",
13+
// possible values: 'dots', 'progress'
14+
reporters: 'dots',
15+
colors: true,
16+
17+
port: 8080,
18+
19+
// base path, that will be used to resolve files and exclude
20+
basePath: '.',
21+
22+
// Start these browsers, currently available:
23+
// Chrome, ChromeCanary, Firefox, Opera, Safari, PhantomJS
24+
browsers: ['PhantomJS'],
25+
26+
frameworks: ['jasmine'],
27+
28+
plugins: [
29+
require('karma-webpack'),
30+
require('karma-sourcemap-loader'),
31+
require('karma-jasmine'),
32+
require('karma-phantomjs-launcher'),
33+
require('karma-chrome-launcher')
34+
],
35+
36+
webpack: {
37+
devtool: 'inline-source-map',
38+
39+
resolve: {
40+
modules: ['node_modules'],
41+
extensions: ['.js', '.ts']
42+
},
43+
44+
module: {
45+
rules: [
46+
{
47+
test: /\.ts$/,
48+
loader: "awesome-typescript-loader",
49+
options: {
50+
noEmit: true,
51+
tsconfig: "test/tsconfig.json",
52+
configFileName: "test/tsconfig.json",
53+
}
54+
}
55+
]
56+
},
57+
58+
},
59+
60+
webpackMiddleware: {
61+
stats: { chunks: false },
62+
},
63+
64+
files: ['test/index.js'],
65+
66+
preprocessors: {
67+
'test/index.js': ['webpack', 'sourcemap'],
68+
},
69+
70+
};
71+
72+
karma.set(config);
73+
};
74+

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"clean": "shx rm -rf lib lib-esm _bundles _dec",
77
"compile": "npm run clean && ngc",
88
"bundle": "rollup -c && rollup -c --environment MINIFY",
9-
"package": "npm run bundle",
109
"build": "npm run compile && npm run bundle",
10+
"test": "karma start",
11+
"debug": "karma start --singleRun=false --autoWatch=true --autoWatchInterval=1 --browsers=Chrome",
12+
"package": "npm run bundle",
1113
"docs": "typedoc --tsconfig tsconfig.typedoc.json --readme README.md --name 'ui-router-ng2' --theme node_modules/ui-router-typedoc-themes/bin/default --out _doc --internal-aliases internal,coreapi,ng2api --external-aliases internalapi,external --navigation-label-globals ui-router-ng2",
1214
"prepublish": "npm run build"
1315
},
@@ -64,13 +66,15 @@
6466
"conventional-changelog": "^1.1.0",
6567
"conventional-changelog-cli": "^1.1.1",
6668
"jasmine-core": "^2.4.1",
67-
"karma": "^1.2.0",
68-
"karma-chrome-launcher": "~0.1.0",
69+
"karma": "^1.5.0",
70+
"karma-chrome-launcher": "^0.1.12",
6971
"karma-coverage": "^0.5.3",
70-
"karma-jasmine": "^1.0.2",
72+
"karma-jasmine": "^1.1.0",
7173
"karma-phantomjs-launcher": "^1.0.2",
7274
"karma-script-launcher": "~0.1.0",
75+
"karma-sourcemap-loader": "^0.3.7",
7376
"karma-systemjs": "^0.7.2",
77+
"karma-webpack": "^2.0.2",
7478
"readline-sync": "^1.4.5",
7579
"rollup": "^0.41.4",
7680
"rollup-plugin-commonjs": "^7.0.0",
@@ -90,7 +94,7 @@
9094
"typedoc-plugin-ui-router": "^1.0.0",
9195
"typescript": "~2.0.3",
9296
"ui-router-typedoc-themes": "^1.0.0",
93-
"webpack": "2.2.0",
97+
"webpack": "^2.2.0",
9498
"webpack-dev-server": "2.2.0",
9599
"webpack-visualizer-plugin": "^0.1.10",
96100
"zone.js": "^0.6.21"

test/bootstrap.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
2+
import { TestBed } from '@angular/core/testing';
3+
4+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
5+

test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// require all source files ending in "Spec" from the
2+
// current directory and all subdirectories
3+
4+
require('core-js');
5+
// require('zone.js');
6+
7+
require('zone.js/dist/zone');
8+
require('zone.js/dist/long-stack-trace-zone');
9+
require('zone.js/dist/proxy');
10+
require('zone.js/dist/sync-test');
11+
require('zone.js/dist/jasmine-patch');
12+
require('zone.js/dist/async-test');
13+
require('zone.js/dist/fake-async-test');
14+
15+
// Init testing harness
16+
require('./bootstrap');
17+
18+
// Add ui-router-ng2
19+
require('../src/index');
20+
21+
var testsContext = require['context'](".", true, /\.spec$/);
22+
testsContext.keys().forEach(testsContext);

test/ngModule/foo/foo.module.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { UIRouterModule } from '../../../src/uiRouterNgModule';
2+
import { Component, NgModule } from '@angular/core';
3+
4+
@Component({ selector: 'foo', template: 'FOO' })
5+
export class FooComponent { }
6+
7+
@Component({ selector: 'child1', template: 'CHILD1' })
8+
export class Child1Component { }
9+
10+
@Component({ selector: 'child2', template: 'CHILD2' })
11+
export class Child2Component { }
12+
13+
14+
export const foo = { name: 'foo', url: '/foo', component: FooComponent };
15+
export const child1 = { name: 'foo.child1', url: '/child1', component: Child1Component };
16+
export const child2 = { name: 'foo.child2', url: '/child2', component: Child2Component };
17+
18+
@NgModule({
19+
declarations: [FooComponent, Child1Component, Child2Component],
20+
imports: [UIRouterModule.forChild({ states: [ foo, child1, child2 ]})],
21+
})
22+
export class FooModule {}
23+
24+
@NgModule({
25+
declarations: [FooComponent, Child1Component, Child2Component],
26+
imports: [UIRouterModule.forChild({ states: [ child1, child2, foo ]})],
27+
})
28+
export class FooModuleOutOfOrder {}
29+

test/ngModule/lazyModule.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { async, inject, TestBed } from '@angular/core/testing';
2+
import { UIRouterModule } from '../../src/uiRouterNgModule';
3+
import { UIView } from '../../src/directives/uiView';
4+
import { memoryLocationPlugin, UIRouter } from 'ui-router-core';
5+
import { NgModuleFactoryLoader, SystemJsNgModuleLoader } from '@angular/core';
6+
7+
declare var System;
8+
9+
let futureFoo = {
10+
name: 'foo.**',
11+
url: '/foo',
12+
loadChildren: () => System.import('./foo/foo.module').then(x => x.FooModule)
13+
};
14+
15+
function configFn(router: UIRouter) {
16+
router.plugin(memoryLocationPlugin);
17+
}
18+
19+
beforeEach(() => {
20+
let routerModule = UIRouterModule.forRoot({ useHash: true, states: [futureFoo], config: configFn });
21+
22+
TestBed.configureTestingModule({
23+
declarations: [],
24+
imports: [routerModule],
25+
providers: [
26+
{ provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader },
27+
]
28+
});
29+
});
30+
31+
describe('lazy loading', () => {
32+
33+
it('should lazy load a module', async(inject([UIRouter], (router: UIRouter) => {
34+
const fixture = TestBed.createComponent(UIView);
35+
fixture.detectChanges();
36+
37+
let { stateRegistry, stateService, globals } = router;
38+
39+
let names = stateRegistry.get().map(state => state.name).sort();
40+
expect(names.length).toBe(2);
41+
expect(names).toEqual(['', 'foo.**']);
42+
43+
stateService.go('foo').then(() => {
44+
expect(globals.current.name).toBe('foo');
45+
46+
names = stateRegistry.get().map(state => state.name).sort();
47+
expect(names.length).toBe(4);
48+
expect(names).toEqual(['', 'foo', 'foo.child1', 'foo.child2']);
49+
})
50+
})));
51+
52+
53+
});
54+

test/testUtils.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import {pick, extend, forEach, omit} from "../src/core";
2-
import {map} from "../src/common/common";
3-
1+
import { forEach, map, omit, pick } from 'ui-router-core';
2+
import { TestBed } from '@angular/core/testing';
43
let stateProps = ["resolve", "resolvePolicy", "data", "template", "templateUrl", "url", "name", "params"];
54

65
export function tree2Array(tree, inheritName) {
76

87
function processState(parent, state, name) {
9-
var substates = omit.apply(null, [state].concat(stateProps));
10-
var thisState = pick.apply(null, [state].concat(stateProps));
8+
let substates = omit.apply(null, [state].concat(stateProps));
9+
let thisState = pick.apply(null, [state].concat(stateProps));
1110
thisState.name = name;
1211
if (!inheritName) thisState.parent = parent;
1312

@@ -27,19 +26,19 @@ export function tree2Array(tree, inheritName) {
2726
}
2827

2928
export function PromiseResult(promise?) {
30-
var self = this, _promise: Promise;
31-
var resolve, reject, complete;
29+
let self = this, _promise: Promise<any>;
30+
let resolve, reject, complete;
3231

3332
this.setPromise = function(promise) {
3433
if (_promise) {
3534
throw new Error("Already have with'd a promise.");
3635
}
3736

38-
var onfulfilled = (data) =>
37+
let onfulfilled = (data) =>
3938
resolve = data || true;
40-
var onrejected = (err) =>
39+
let onrejected = (err) =>
4140
reject = err || true;
42-
var done = () =>
41+
let done = () =>
4342
complete = true;
4443

4544
_promise = promise;

test/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"emitDecoratorMetadata": true,
4+
"experimentalDecorators": true,
5+
"moduleResolution": "node",
6+
"module": "es6",
7+
"target": "es5",
8+
"lib": [ "es6", "dom" ],
9+
"allowSyntheticDefaultImports": true,
10+
"outDir": "lib",
11+
"declaration": true,
12+
"sourceMap": true
13+
},
14+
"angularCompilerOptions": {
15+
"skipTemplateCodegen": true,
16+
"strictMetadataEmit": true
17+
},
18+
"files": [
19+
"./index.js"
20+
]
21+
}

test/uiView.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import "ui-router-ng2";
2-
3-
fdescribe("foo", () => {
1+
describe("foo", () => {
42
it("should work", () => {
5-
expect(true).toBeFalsy();
3+
expect(true).toBeTruthy();
64
});
75
});

0 commit comments

Comments
 (0)