-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathfeatureClearAllSpec.js
206 lines (170 loc) · 7.08 KB
/
featureClearAllSpec.js
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
describe('clearAll feature', function () {
(function() {
//a simple runtime configurer that uses clearAll to redefine ui-router
//behavior at *runtime* (as opposed to config time).
//
//A note about this provider/service
//if the ui-router services exposed the configruation
//functions that live in their providers
//then this provider/service wrapper would not be necessary.
//However, by latching onto the providers with
//this service, runtime (re)configuration is possible.
//In other words -- the ui-router services *could*
//do this themselves; the features of this "test" provider/service
//may show up as a pull for ui-router seperately :)
//for now, clearAll allows what it does to be possible
angular.module('uiRouterRuntimeConfigurer', ['ui.state']);
UiRouterRuntimeConfigProvider.$inject = ['$stateProvider', '$urlRouterProvider'];
function UiRouterRuntimeConfigProvider($stateProvider, $urlRouterProvider) {
function clearAll() {
//the stateProvider clears states and urlRoutes
//hence this shortcut works
$stateProvider.clearAll();
}
this.clearAll = function() { clearAll(); return this; };
function state(name, definition) {
$stateProvider.state(name, definition);
}
this.state = function(name, definition) { state(name, definition); return this; };
function when(what, handler) {
$urlRouterProvider.when(what, handler);
}
this.when = function(what, handler) { when(what, handler); return this; };
function otherwise(rule) {
$urlRouterProvider.otherwise(rule);
}
this.otherwise = function(rule) { otherwise(rule); return this; };
this.$get = $get;
function $get() {
var uiRouterRuntimeConfig = {};
uiRouterRuntimeConfig.clearAll = function() { clearAll(); return this; };
uiRouterRuntimeConfig.state = function(name, definition) { state(name, definition); return this; };
uiRouterRuntimeConfig.when = function(what, handler) { when(what, handler); return this; };
uiRouterRuntimeConfig.otherwise = function(rule) { otherwise(rule); return this; };
//this service can call the ui-router providers from its provider
return uiRouterRuntimeConfig;
}
}
angular.module('uiRouterRuntimeConfigurer').provider('uiRouterRuntimeConfig', UiRouterRuntimeConfigProvider);
}()); //self-invoking function
var log, logEvents, logEnterExit;
function eventLogger(event, to, toParams, from, fromParams) {
if (logEvents) log += event.name + '(' + to.name + ',' + from.name + ');';
}
function callbackLogger(what) {
return function () {
if (logEnterExit) log += this.name + '.' + what + ';';
};
}
var HOME = {url: '/'},
ABOUT = {url: '/about'},
ADMIN = {url: '/admin'},
LOGIN = {url: '/login'},
THEYWIN = {url: '/theyWin'},
FOUROHFOUR = {url: '/fourOhFour'};
function configureBase(uiRouterRuntimeConfigProviderOrService)
{
return uiRouterRuntimeConfigProviderOrService
.clearAll()
.state('ABOUT', ABOUT)
.state('HOME', HOME)
.state('FOUROHFOUR', FOUROHFOUR)
.state('THEYWIN', THEYWIN)
.when('/northDakota', '/about' )
.when('/someoneGuessesThisUrl', '/theyWin' )
.otherwise('/fourOhFour');
}
function configureAnon(uiRouterRuntimeConfigProviderOrService)
{
return configureBase(uiRouterRuntimeConfigProviderOrService)
.state('LOGIN', LOGIN);
}
function configureAdmin(uiRouterRuntimeConfigProviderOrService)
{
return configureBase(uiRouterRuntimeConfigProviderOrService)
.state('ADMIN', ADMIN);
}
angular.module('test', ['uiRouterRuntimeConfigurer']).config(
['uiRouterRuntimeConfigProvider',
function(uiRouterRuntimeConfigProvider) {
configureAnon(uiRouterRuntimeConfigProvider);
}]
);
beforeEach(module('test'));
function $get(what) {
return jasmine.getEnv().currentSpec.$injector.get(what);
}
function testSet() {
it('should work as always', inject(function ($state, $q) {
var trans = $state.transitionTo(HOME, {});
$q.flush();
expect(resolvedValue(trans)).toBe(HOME);
}));
it('should allow transitions by name', inject(function ($state, $q) {
$state.transitionTo('ABOUT', {});
$q.flush();
expect($state.current).toBe(ABOUT);
}));
it('should always have $current defined', inject(function ($state) {
expect($state.$current).toBeDefined();
}));
it('should have the correct location', inject(function ($state, $q, $location) {
$state.transitionTo('FOUROHFOUR', {});
$q.flush();
expect($location.path()).toBe(FOUROHFOUR.url);
}));
it('should support otherwise', inject(function ($state, $rootScope, $q, $location) {
$location.path("/nonExistent");
$rootScope.$apply();
expect($state.current).toBe(FOUROHFOUR);
}));
it('should support urlRouter/when', inject(function ($state, $rootScope, $q, $location) {
$location.path("/northDakota");
$rootScope.$apply();
expect($state.current).toBe(ABOUT);
}));
it('should support urlRouter/when', inject(function ($state, $rootScope, $q, $location) {
$location.path("/someoneGuessesThisUrl");
$rootScope.$apply();
expect($state.current).toBe(THEYWIN);
}));
}
describe('initially configured states', function() {
testSet();
it('the anonymous user should not have the admin route', inject(function ($state, $rootScope, $q, $location) {
$location.path("/admin");
$rootScope.$apply();
expect($state.current).toBe(FOUROHFOUR);
}));
it('the anonymous user should the login route', inject(function ($state, $rootScope, $q, $location) {
$location.path("/login");
$rootScope.$apply();
expect($state.current).toBe(LOGIN);
}));
});
describe('when the user logs in as admin', function() {
it('the admin user will not have the login route but will have the admin route', inject(function (uiRouterRuntimeConfig, $state, $rootScope, $q, $location) {
configureAdmin(uiRouterRuntimeConfig);
//this is kind of dumb example because the admin should be able to change credentials but for demo purposes
//this will have to do
$location.path("/login");
$rootScope.$apply();
expect($state.current).toBe(FOUROHFOUR);
$location.path("/admin");
$rootScope.$apply();
expect($state.current).toBe(ADMIN);
}));
});
describe('when the admin user creates a blog page in her angular-based CMS system', function() {
it('she will be able to preview it because the route will be added on the fly', inject(function (uiRouterRuntimeConfig, $state, $rootScope, $q, $location) {
configureAdmin(uiRouterRuntimeConfig);
var BLOG = {url: '/blog'};
uiRouterRuntimeConfig.state('BLOG', BLOG);
//this is kind of dumb example because the admin should be able to change credentials but for demo purposes
//this will have to do
$state.transitionTo('BLOG');
$q.flush();
expect($state.current).toBe(BLOG);
}));
});
});