forked from angular-ui/ui-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhookBuilderSpec.ts
212 lines (168 loc) · 7.32 KB
/
hookBuilderSpec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
UIRouter, TransitionService, StateService, State, PathNode, tail, PathFactory
} from "../../src/core";
import {tree2Array} from "../stateHelper.ts";
import "../../src/justjs";
describe('HookBuilder:', function() {
let uiRouter: UIRouter = null;
let $trans: TransitionService = null;
let $state: StateService = null;
let root: State = null;
let log = "";
let hookNames = [ "onBefore", "onStart", "onExit", "onRetain", "onEnter", "onFinish", "onSuccess", "onError" ];
const hook = (name) => () => log += `${name};`;
beforeEach(() => {
log = "";
uiRouter = new UIRouter();
$trans = uiRouter.transitionService;
$state = uiRouter.stateService;
root = uiRouter.stateRegistry.root();
let statetree = {
A: {
B: {
C: {
D: {
}
}
}
}
};
tree2Array(statetree, true).forEach(state => uiRouter.stateRegistry.register(state));
uiRouter.stateRegistry.stateQueue.autoFlush($state);
});
let trans, trans2, hb, hb2, callback;
beforeEach(function() {
// Clean out the default UIRouter onBefore hooks
uiRouter.transitionService.getHooks("onBefore").length = 0;
// Transition from 'A' to 'A.B.C'
let A = $state.target('A', null).$state();
let path = [new PathNode(root), new PathNode(A)];
trans = $trans.create(path, $state.target("A.B.C", null));
hb = trans.hookBuilder();
expect(hb.getOnBeforeHooks().length).toBe(0);
// Transition from 'A.B.C' to 'A'
let A = $state.target('A', null).$state();
let B = $state.target('A.B', null).$state();
let C = $state.target('A.B.C', null).$state();
let fromPath = [new PathNode(root), new PathNode(A), new PathNode(B), new PathNode(C)];
trans2 = $trans.create(fromPath, $state.target("A", null));
hb2 = trans2.hookBuilder();
callback = hook('hook');
expect(typeof callback).toBe('function')
});
const getFn = x => x['eventHook']['callback'];
describe('HookMatchCriteria', function() {
describe('.to', function() {
it("should match a transition with same to state", function() {
trans.onBefore({to: "A.B.C"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
it("should not match a transition with a different to state", function() {
trans.onBefore({to: "A.B"}, callback);
trans.onBefore({to: "A.B.C.D"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([]);
});
it("should match a transition using a glob", function() {
trans.onBefore({to: "A.B.*"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
it("should match a transition using a function", function() {
let deregister = trans.onBefore({to: (state) => state.name === 'A.B'}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([]);
trans.onBefore({to: (state) => state.name === 'A.B.C'}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
});
describe('.from', function() {
it("should match a transition with same from state", function() {
trans.onBefore({from: "A"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
it("should not match a transition with a different from state", function() {
trans.onBefore({from: "A.B"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([]);
});
it("should match a transition using a function", function() {
let deregister = trans.onBefore({from: (state) => state.name === 'A.B'}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([]);
trans.onBefore({from: (state) => state.name === 'A'}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
});
describe('.to and .from', function() {
it("should match a transition with same to and from state", function() {
trans.onBefore({from: "A", to: "A.B.C"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
it("should not match a transition with a different to or from state", function() {
trans.onBefore({from: "A", to: "A.B.C.D"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([]);
});
});
describe('.entering', function() {
it("should match a transition that will enter the 'entering' state", function() {
trans.onBefore({entering: "A.B.C"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
});
describe('.retained', function() {
it("should match a transition where the state is already entered, and will not exit", function() {
trans.onBefore({retained: "A"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
it("should not match a transition that will not retain the state", function() {
trans.onBefore({retained: "A.B"}, callback);
expect(hb.getOnBeforeHooks().map(getFn)).toEqual([]);
});
});
describe('.exiting', function() {
it("should match a transition that will exit the 'exiting' state", function() {
trans2.onBefore({exiting: "A.B.C"}, callback);
expect(hb2.getOnBeforeHooks().map(getFn)).toEqual([callback]);
});
});
});
describe('built TransitionHooks', function() {
beforeEach(function() {
// Deregister all built-in TransitionService hooks for clean slate for these tests
Object.keys($trans._deregisterHookFns).forEach(key => $trans._deregisterHookFns[key]());
});
describe('should have the correct state context', function() {
const context = hook =>
hook.stateContext && hook.stateContext.name;
it('; onBefore should not have a state context', function() {
trans.onBefore({}, callback);
expect(hb.getOnBeforeHooks().map(context)).toEqual([null]);
});
it('; onStart should not have a state context', function() {
trans.onStart({}, callback);
expect(hb.getOnStartHooks().map(context)).toEqual([null]);
});
it('; onEnter should be bound to the entering state(s)', function() {
trans.onEnter({}, callback);
expect(hb.getOnEnterHooks().map(context)).toEqual(["A.B", "A.B.C"]);
});
it('; onRetain should be bound to the retained state(s)', function() {
trans.onRetain({}, callback);
expect(hb.getOnRetainHooks().map(context)).toEqual(["", "A"]);
});
it('; onExit should be bound to the exiting state(s)', function() {
trans2.onExit({}, callback);
expect(hb2.getOnExitHooks().map(context)).toEqual(["A.B.C", "A.B"]);
});
it('; onFinish should not have a state context', function() {
trans.onFinish({}, callback);
expect(hb.getOnFinishHooks().map(context)).toEqual([null]);
});
it('; onSuccess should not have a state context', function() {
trans.onSuccess({}, callback);
expect(hb.getOnSuccessHooks().map(context)).toEqual([null]);
});
it('; onError should not have a state context', function() {
trans.onStart({}, () => { throw new Error('shuckydarn') });
trans.onError({}, callback);
expect(hb.getOnErrorHooks().map(context)).toEqual([null]);
});
});
});
});