Skip to content

Commit 0d22bd8

Browse files
author
christhielen
committed
test(resolve): Added tests (some failing) for state resolves during transitions
1 parent b118746 commit 0d22bd8

File tree

1 file changed

+155
-60
lines changed

1 file changed

+155
-60
lines changed

test/resolveSpec.js

Lines changed: 155 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,80 @@ var resolve = uiRouter.resolve,
1111
Path = resolve.Path,
1212
PathElement = resolve.PathElement;
1313

14-
describe('Resolvables system:', function () {
15-
var statesTree, statesMap = {};
16-
var emptyPath;
17-
var counts;
18-
var asyncCount;
19-
20-
beforeEach(inject(function ($transition, $injector) {
21-
uiRouter.angular1.runtime.setRuntimeInjector($injector);
22-
emptyPath = new Path([]);
23-
asyncCount = 0;
24-
}));
25-
26-
beforeEach(function () {
27-
counts = { _J: 0, _J2: 0, _K: 0, _L: 0, _M: 0};
28-
states = {
29-
A: { resolve: { _A: function () { return "A"; }, _A2: function() { return "A2"; }},
30-
B: { resolve: { _B: function () { return "B"; }, _B2: function() { return "B2"; }},
31-
C: { resolve: { _C: function (_A, _B) { return _A + _B + "C"; }, _C2: function() { return "C2"; }},
32-
D: { resolve: { _D: function (_D2) { return "D1" + _D2; }, _D2: function () { return "D2"; }} }
33-
}
34-
},
35-
E: { resolve: { _E: function() { return "E"; } },
36-
F: { resolve: { _E: function() { return "_E"; }, _F: function(_E) { return _E + "F"; }} }
37-
},
38-
G: { resolve: { _G: function() { return "G"; } },
39-
H: { resolve: { _G: function(_G) { return _G + "_G"; }, _H: function(_G) { return _G + "H"; } } }
40-
},
41-
I: { resolve: { _I: function(_I) { return "I"; } } }
14+
///////////////////////////////////////////////
15+
16+
var statesTree, statesMap = {};
17+
var emptyPath;
18+
var counts, expectCounts;
19+
var asyncCount;
20+
21+
beforeEach(function () {
22+
counts = { _J: 0, _J2: 0, _K: 0, _L: 0, _M: 0};
23+
expectCounts = angular.copy(counts);
24+
states = {
25+
A: { resolve: { _A: function () { return "A"; }, _A2: function() { return "A2"; }},
26+
B: { resolve: { _B: function () { return "B"; }, _B2: function() { return "B2"; }},
27+
C: { resolve: { _C: function (_A, _B) { return _A + _B + "C"; }, _C2: function() { return "C2"; }},
28+
D: { resolve: { _D: function (_D2) { return "D1" + _D2; }, _D2: function () { return "D2"; }} }
29+
}
30+
},
31+
E: { resolve: { _E: function() { return "E"; } },
32+
F: { resolve: { _E: function() { return "_E"; }, _F: function(_E) { return _E + "F"; }} }
33+
},
34+
G: { resolve: { _G: function() { return "G"; } },
35+
H: { resolve: { _G: function(_G) { return _G + "_G"; }, _H: function(_G) { return _G + "H"; } } }
4236
},
43-
J: { resolve: { _J: function() { counts['_J']++; return "J"; }, _J2: function(_J) { counts['_J2']++; return _J + "J2"; } },
44-
K: { resolve: { _K: function(_J2) { counts['_K']++; return _J2 + "K"; }},
45-
L: { resolve: { _L: function(_K) { counts['_L']++; return _K + "L"; }},
46-
M: { resolve: { _M: function(_L) { counts['_M']++; return _L + "M"; }} }
47-
}
48-
},
49-
N: {
50-
resolve: { _N: function(_J) { return _J + "N"; }, _N2: function(_J) { return _J + "N2"; }, _N3: function(_J) { return _J + "N3"; } },
51-
resolvePolicy: { _N: "eager", _N2: "lazy", _N3: "jit" }
37+
I: { resolve: { _I: function(_I) { return "I"; } } }
38+
},
39+
J: { resolve: { _J: function() { counts['_J']++; return "J"; }, _J2: function(_J) { counts['_J2']++; return _J + "J2"; } },
40+
K: { resolve: { _K: function(_J2) { counts['_K']++; return _J2 + "K"; }},
41+
L: { resolve: { _L: function(_K) { counts['_L']++; return _K + "L"; }},
42+
M: { resolve: { _M: function(_L) { counts['_M']++; return _L + "M"; }} }
5243
}
5344
},
54-
O: { resolve: { _O: function(_O2) { return _O2 + "O"; }, _O2: function(_O) { return _O + "O2"; } }
45+
N: {
46+
resolve: { _N: function(_J) { return _J + "N"; }, _N2: function(_J) { return _J + "N2"; }, _N3: function(_J) { return _J + "N3"; } },
47+
resolvePolicy: { _N: "eager", _N2: "lazy", _N3: "jit" }
5548
}
56-
};
49+
},
50+
O: { resolve: { _O: function(_O2) { return _O2 + "O"; }, _O2: function(_O) { return _O + "O2"; } }
51+
}
52+
};
5753

58-
var stateProps = ["resolve", "resolvePolicy"];
59-
statesTree = loadStates({}, states, '');
54+
var stateProps = ["resolve", "resolvePolicy"];
55+
statesTree = loadStates({}, states, '');
6056

61-
function loadStates(parent, state, name) {
62-
var thisState = pick.apply(null, [state].concat(stateProps));
63-
var substates = omit.apply(null, [state].concat(stateProps));
57+
function loadStates(parent, state, name) {
58+
var thisState = pick.apply(null, [state].concat(stateProps));
59+
var substates = omit.apply(null, [state].concat(stateProps));
6460

65-
thisState.name = name;
66-
thisState.parent = parent.name;
67-
thisState.data = { children: [] };
61+
thisState.name = name;
62+
thisState.parent = parent.name;
63+
thisState.data = { children: [] };
6864

69-
angular.forEach(substates, function (value, key) {
70-
thisState.data.children.push(loadStates(thisState, value, key));
71-
});
72-
statesMap[name] = thisState;
73-
return thisState;
74-
}
65+
angular.forEach(substates, function (value, key) {
66+
thisState.data.children.push(loadStates(thisState, value, key));
67+
});
68+
statesMap[name] = thisState;
69+
return thisState;
70+
}
7571
// console.log(map(makePath([ "A", "B", "C" ]), function(s) { return s.name; }));
76-
});
72+
});
7773

78-
function makePath(names) {
79-
return new Path(map(names, function(name) { return statesMap[name]; }));
80-
}
74+
function makePath(names) {
75+
return new Path(map(names, function(name) { return statesMap[name]; }));
76+
}
8177

82-
function getResolvedData(pathContext) {
83-
return map(pathContext.getResolvables(), function(r) { return r.data; });
84-
}
78+
function getResolvedData(pathContext) {
79+
return map(pathContext.getResolvables(), function(r) { return r.data; });
80+
}
81+
82+
describe('Resolvables system:', function () {
83+
beforeEach(inject(function ($transition, $injector) {
84+
uiRouter.angular1.runtime.setRuntimeInjector($injector);
85+
emptyPath = new Path([]);
86+
asyncCount = 0;
87+
}));
8588

8689
describe('PathElement.resolvePathElement()', function () {
8790
it('should resolve all resolves in a PathElement', inject(function ($q) {
@@ -475,3 +478,95 @@ describe('Resolvables system:', function () {
475478
// TODO: Implement and test injection into controllers
476479
});
477480

481+
describe("State transitions with resolves", function() {
482+
beforeEach(module(function($stateProvider) {
483+
// allow tests to specify controllers after registration
484+
function controllerProvider(state) {
485+
return function() {
486+
return statesMap[state.name].controller || function emptyController() {}
487+
}
488+
}
489+
490+
angular.forEach(statesMap, function(state, key) {
491+
if (!key) return;
492+
state.template = "<div ui-view></div> state"+key;
493+
state.controllerProvider = controllerProvider(state);
494+
$stateProvider.state(key, state);
495+
});
496+
}));
497+
498+
var $state, $transition, $q, $compile, $rootScope, $scope, $timeout;
499+
beforeEach(inject(function (_$transition_, _$state_, _$q_, _$compile_, _$rootScope_, _$timeout_, $injector) {
500+
$state = _$state_;
501+
$transition = _$transition_;
502+
$q = _$q_;
503+
$compile = _$compile_;
504+
$rootScope = _$rootScope_;
505+
$timeout = _$timeout_;
506+
$scope = $rootScope.$new();
507+
uiRouter.angular1.runtime.setRuntimeInjector($injector);
508+
emptyPath = new Path([]);
509+
asyncCount = 0;
510+
$compile(angular.element("<div ui-view></div>"))($scope);
511+
}));
512+
513+
function flush() {
514+
$q.flush();
515+
$timeout.flush();
516+
}
517+
518+
function testGo(state, params, options) {
519+
$state.go(state, params, options);
520+
$q.flush();
521+
expect($state.current).toBe($state.get(state));
522+
}
523+
524+
it("should not resolve jit resolves that are not injected anywhere", inject(function() {
525+
testGo("J");
526+
expect(counts).toEqualData(expectCounts);
527+
testGo("K");
528+
expect(counts).toEqualData(expectCounts);
529+
}));
530+
531+
it("should invoke jit resolves when they are injected", inject(function() {
532+
statesMap.J.controller = function JController(_J) { };
533+
534+
testGo("J", {}, {trace: true});
535+
expectCounts._J++;
536+
expect(counts).toEqualData(expectCounts);
537+
}));
538+
539+
it("should invoke jit resolves only when injected", inject(function() {
540+
statesMap.K.controller = function KController(_K) { };
541+
542+
testGo("J");
543+
expect(counts).toEqualData(expectCounts);
544+
545+
testGo("K");
546+
expectCounts._K++;
547+
expectCounts._J++;
548+
expectCounts._J2++;
549+
expect(counts).toEqualData(expectCounts);
550+
}));
551+
552+
it("should not re-invoke jit resolves", inject(function() {
553+
statesMap.J.controller = function KController(_J) { };
554+
statesMap.K.controller = function KController(_K) { };
555+
testGo("J");
556+
expectCounts._J++;
557+
expect(counts).toEqualData(expectCounts);
558+
559+
testGo("K");
560+
561+
expectCounts._K++;
562+
expectCounts._J2++;
563+
expect(counts).toEqualData(expectCounts);
564+
}));
565+
566+
it("should invoke jit resolves during a transition that are injected in a hook like onEnter", inject(function() {
567+
statesMap.J.onEnter = function onEnter(_J) {};
568+
testGo("J", {}, { trace: true });
569+
expectCounts._J++;
570+
expect(counts).toEqualData(expectCounts);
571+
}));
572+
});

0 commit comments

Comments
 (0)