Skip to content

Commit

Permalink
test(*): Update and lint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Apr 20, 2017
1 parent 39f8a53 commit 5a54b1d
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 186 deletions.
32 changes: 17 additions & 15 deletions test/hookBuilderSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UIRouter, TransitionService, StateService, StateObject, PathNode } from
import { tree2Array } from "./_testUtils";
import { TransitionHookPhase } from "../src/transition/interface";
import { TestingPlugin } from "./_testingPlugin";
import {uniqR} from '../src/common/common';

describe('HookBuilder:', function() {
let uiRouter: UIRouter = null;
Expand All @@ -10,8 +11,7 @@ describe('HookBuilder:', function() {
let root: StateObject = null;

let log = "";
let hookNames = [ "onBefore", "onStart", "onExit", "onRetain", "onEnter", "onFinish", "onSuccess", "onError" ];
const hook = (name) => () => log += `${name};`;
const logHook = (name) => () => log += `${name};`;

beforeEach(() => {
log = "";
Expand All @@ -28,15 +28,17 @@ describe('HookBuilder:', function() {
C: {
D: {

}
}
}
}
},
},
},
},
};

tree2Array(statetree, true).forEach(state => uiRouter.stateRegistry.register(state));
});

afterEach(() => uiRouter.dispose());

let trans, trans2, hb, hb2, callback;
beforeEach(function() {
// Clean out the default UIRouter onBefore hooks
Expand All @@ -57,8 +59,8 @@ describe('HookBuilder:', function() {
trans2 = $trans.create(fromPath, $state.target("A", null));
hb2 = trans2._hookBuilder;

callback = hook('hook');
expect(typeof callback).toBe('function')
callback = logHook('hook');
expect(typeof callback).toBe('function');
});

const getFn = x => x['registeredHook']['callback'];
Expand All @@ -83,7 +85,7 @@ describe('HookBuilder:', function() {
});

it("should match a transition using a function", function() {
let deregister = trans.onBefore({to: (state) => state.name === 'A.B'}, callback);
trans.onBefore({to: (state) => state.name === 'A.B'}, callback);
expect(hb.buildHooksForPhase(TransitionHookPhase.BEFORE).map(getFn)).toEqual([]);

trans.onBefore({to: (state) => state.name === 'A.B.C'}, callback);
Expand All @@ -104,7 +106,7 @@ describe('HookBuilder:', function() {
});

it("should match a transition using a function", function() {
let deregister = trans.onBefore({from: (state) => state.name === 'A.B'}, callback);
trans.onBefore({from: (state) => state.name === 'A.B'}, callback);
expect(hb.buildHooksForPhase(TransitionHookPhase.BEFORE).map(getFn)).toEqual([]);

trans.onBefore({from: (state) => state.name === 'A'}, callback);
Expand Down Expand Up @@ -170,12 +172,12 @@ describe('HookBuilder:', function() {

it('; onBefore should not have a state context', function() {
trans.onBefore({}, callback);
expect(hb.buildHooksForPhase(TransitionHookPhase.BEFORE).map(context)).toEqual([null]);
expect(hb.buildHooksForPhase(TransitionHookPhase.BEFORE).map(context).reduce(uniqR, [])).toEqual([null]);
});

it('; onStart should not have a state context', function() {
trans.onStart({}, callback);
expect(hb.buildHooks(hookTypeByName('onStart')).map(context)).toEqual([null]);
expect(hb.buildHooks(hookTypeByName('onStart')).map(context).reduce(uniqR, [])).toEqual([null]);
});

it('; onEnter should be bound to the entering state(s)', function() {
Expand All @@ -200,13 +202,13 @@ describe('HookBuilder:', function() {

it('; onSuccess should not have a state context', function() {
trans.onSuccess({}, callback);
expect(hb.buildHooksForPhase(TransitionHookPhase.SUCCESS).map(context)).toEqual([null]);
expect(hb.buildHooksForPhase(TransitionHookPhase.SUCCESS).map(context).reduce(uniqR, [])).toEqual([null]);
});

it('; onError should not have a state context', function() {
trans.onStart({}, () => { throw new Error('shuckydarn') });
trans.onStart({}, () => { throw new Error('shuckydarn'); });
trans.onError({}, callback);
expect(hb.buildHooksForPhase(TransitionHookPhase.ERROR).map(context)).toEqual([null]);
expect(hb.buildHooksForPhase(TransitionHookPhase.ERROR).map(context).reduce(uniqR, [])).toEqual([null]);
});

});
Expand Down
18 changes: 9 additions & 9 deletions test/hooksSpec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { UIRouter } from "../src/router";
import {delay, tree2Array} from "./_testUtils";
import { find } from "../src/common/common";
import { StateService } from "../src/state/stateService";
import { StateDeclaration } from "../src/state/interface";
import { TestingPlugin } from "./_testingPlugin";
import { TransitionService } from "../src/transition/transitionService";
import {UIRouter} from '../src/router';
import {tree2Array} from './_testUtils';
import {find} from '../src/common/common';
import {StateService} from '../src/state/stateService';
import {StateDeclaration} from '../src/state/interface';
import {TestingPlugin} from './_testingPlugin';
import {TransitionService} from '../src/transition/transitionService';


let statetree = {
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("hooks", () => {

$state.go('A').then(() => {
expect(router.globals.current.name).toBe('AAA');
expect(router.globals.params.fooId).toBe('abc');
expect(router.globals.params['fooId']).toBe('abc');
done();
});
});
Expand Down Expand Up @@ -98,7 +98,7 @@ describe("hooks", () => {

$state.go('A').then(() => {
expect(router.globals.current.name).toBe('AAA');
expect(router.globals.params.fooId).toBe('FOO');
expect(router.globals.params['fooId']).toBe('FOO');
done();
});
});
Expand Down
76 changes: 37 additions & 39 deletions test/lazyLoadSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UIRouter, TransitionService, StateService } from "../src/index";
import * as vanilla from "../src/vanilla";

import { StateRegistry } from "../src/state/stateRegistry";
import { services } from "../src/common/coreservices";
import { UrlRouter } from "../src/url/urlRouter";
import {StateDeclaration} from "../src/state/interface";
import {tail} from "../src/common/common";
Expand All @@ -15,9 +14,6 @@ describe('future state', function () {
let $state: StateService;
let $urlRouter: UrlRouter;

const wait = (val?) =>
new Promise((resolve) => setTimeout(() => resolve(val)));

beforeEach(() => {
router = new UIRouter();
router.plugin(vanilla.servicesPlugin);
Expand Down Expand Up @@ -48,7 +44,7 @@ describe('future state', function () {

it('should get future states by state declaration object', () => {
let statedef = { name: 'future.**' };
let state = $registry.register(statedef);
$registry.register(statedef);
expect($registry.get(statedef)).toBe(statedef);
});

Expand Down Expand Up @@ -111,7 +107,7 @@ describe('future state', function () {
});

it('should not match future states with non-matching prefix', () => {
let state = $registry.register({ name: 'future.**' });
$registry.register({ name: 'future.**' });
expect($registry.matcher.find('futurX')).toBeFalsy();
expect($registry.matcher.find('futurX.lazystate')).toBeFalsy();
expect($registry.matcher.find('futurX.foo.bar.baz')).toBeFalsy();
Expand All @@ -133,7 +129,7 @@ describe('future state', function () {
});

it('should not match future states if the prefix does not match', () => {
let state = $registry.register({ name: 'future.**', url: '/future' });
$registry.register({ name: 'future.**', url: '/future' });
expect(match('/futurX')).toBeFalsy();
});

Expand Down Expand Up @@ -180,7 +176,7 @@ describe('future state', function () {

it('should pass a transition and the state context to the lazyLoad function', (done) => {
let objs = {};
var stateDefinition = {name: 'll', url: '/ll', lazyLoad: (trans, state) => (objs = { trans, state }, null) };
let stateDefinition = {name: 'll', url: '/ll', lazyLoad: (trans, state) => (objs = { trans, state }, null) };
$registry.register(stateDefinition);
$state.lazyLoad('ll').then(() => {
expect(objs['trans'] instanceof Transition).toBeTruthy();
Expand All @@ -205,9 +201,9 @@ describe('future state', function () {

Promise.all([
$state.lazyLoad('ll'),
$state.lazyLoad('ll')
$state.lazyLoad('ll'),
]).then((result) => {
expect(result).toEqual([1,1]);
expect(result).toEqual([1, 1]);
expect(lazyLoadCount).toBe(1);
done();
});
Expand All @@ -227,12 +223,12 @@ describe('future state', function () {
expect(err.message).toBe('doh');
expect(stateDeclaration.lazyLoad['_promise']).toBeUndefined();

$state.lazyLoad('ll').catch(err => {
expect(err).toBeDefined();
expect(err.message).toBe('doh');
$state.lazyLoad('ll').catch(err2 => {
expect(err2).toBeDefined();
expect(err2.message).toBe('doh');
expect(lazyLoadCount).toBe(2);
done();
})
});
});

expect(stateDeclaration.lazyLoad['_promise']).toBeDefined();
Expand All @@ -245,45 +241,46 @@ describe('future state', function () {

beforeEach(() => {
futureStateDef = {
name: 'A.**', url: '/a',
lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyStateDefA] }); })
name: 'A.**',
url: '/a',
lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyStateDefA] }); }),
};

$registry.register(futureStateDef)
$registry.register(futureStateDef);
});

it('should deregister the placeholder (future state)', (done) => {
expect($state.get().map(x=>x.name)).toEqual(["", "A.**"]);
expect($state.get().map(x => x.name)).toEqual(["", "A.**"]);
expect($state.get('A')).toBe(futureStateDef);
expect($state.get('A').lazyLoad).toBeDefined();

$state.go('A').then(() => {
expect($state.get().map(x=>x.name)).toEqual(["", "A"]);
expect($state.get().map(x => x.name)).toEqual(["", "A"]);
expect($state.get('A')).toBe(lazyStateDefA);
expect($state.get('A').lazyLoad).toBeUndefined();
expect($state.current.name).toBe('A');
done();
})
});
});

it('should register newly loaded states returned in the `states: ` array', (done) => {
expect($state.get('A')).toBe(futureStateDef);

$state.go('A').then(() => {
expect($state.get().map(x=>x.name)).toEqual(["", "A"]);
expect($state.get().map(x => x.name)).toEqual(["", "A"]);
expect($state.get('A')).toBe(lazyStateDefA);
expect($state.get('A').lazyLoad).toBeUndefined();
expect($state.current.name).toBe('A');
done();
})
});
});

it('should retry the original $state.go()', (done) => {
$state.go('A', { id: 'abc' }).then(() => {
expect($state.current.name).toBe('A');
expect($state.params).toEqualValues({ id: 'abc' });
done();
})
});
});

it('triggered by a URL sync should re-parse the URL to activate the lazy loaded state', (done) => {
Expand All @@ -310,31 +307,31 @@ describe('future state', function () {
beforeEach(() => {
// Re-create each time because the state is mutated: the lazyLoad function is removed after success
futureA = { name: 'A.**', url: '/a', lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyA, lazyAB] }); }) };
futureB = { name: 'B.**', url: '/b', lazyLoad: () => null, };
futureB = { name: 'B.**', url: '/b', lazyLoad: () => null };

$registry.register(futureA)
$registry.register(futureA);
});

it('should register all returned states and remove the placeholder', (done) => {
expect($state.get().map(x=>x.name)).toEqual(["", "A.**"]);
expect($state.get().map(x => x.name)).toEqual(["", "A.**"]);
expect($state.get('A')).toBe(futureA);
expect($state.get('A').lazyLoad).toBeDefined();

$state.go('A').then(() => {
expect($state.get().map(x=>x.name)).toEqual(["", "A", "A.B"]);
expect($state.get().map(x => x.name)).toEqual(["", "A", "A.B"]);
expect($state.get('A')).toBe(lazyA);
expect($state.get('A').lazyLoad).toBeUndefined();
expect($state.current.name).toBe('A');
done();
})
});
});

it('should allow transitions to non-loaded child states', (done) => {
$state.go('A.B', { id: 'abc' }).then(() => {
expect($state.current.name).toBe('A.B');
expect($state.params).toEqualValues({ id: 'abc' });
done();
})
});
});

it('should re-parse the URL to activate the final state', (done) => {
Expand Down Expand Up @@ -383,11 +380,12 @@ describe('future state', function () {

let count = 0;
let futureStateDef = {
name: 'A.**', url: '/a',
name: 'A.**',
url: '/a',
lazyLoad: () => new Promise(resolve => {
count++;
setTimeout(() => resolve({ states: [{ name: 'A', url: '/a' }] }), 50);
})
}),
};
$registry.register(futureStateDef);

Expand All @@ -414,10 +412,10 @@ describe('future state', function () {
} else {
resolve({ states: [{ name: 'A', url: '/a' }] });
}
})
}),
};

$registry.register(futureStateDef)
$registry.register(futureStateDef);
});

it('should not remove the placeholder', (done) => {
Expand Down Expand Up @@ -452,25 +450,25 @@ describe('future state', function () {
expect($state.current.name).toBe('A');

done();
})
})
})
});
});
});
});
});

describe('with a nested future state', () => {
let count, futureStateDefA, futureStateDefB, errors;
let futureStateDefA, futureStateDefB;
let lazyStateDefA = { name: 'A', url: '/a/:aid', params: {id: "adefault"} };
let lazyStateDefB = { name: 'A.B', url: '/b/:bid', params: {id: "bdefault"} };
beforeEach(() => {
futureStateDefA = {
name: 'A.**', url: '/a',
lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyStateDefA, futureStateDefB] }); })
lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyStateDefA, futureStateDefB] }); }),
};

futureStateDefB = {
name: 'A.B.**', url: '/b',
lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyStateDefB] }); })
lazyLoad: () => new Promise(resolve => { resolve({ states: [lazyStateDefB] }); }),
};

$registry.register(futureStateDefA);
Expand Down
Loading

0 comments on commit 5a54b1d

Please sign in to comment.