This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
router.js
250 lines (210 loc) · 7.36 KB
/
router.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
define([
'chai',
'underscore',
'backbone',
'router',
'views/sign_in',
'views/sign_up',
'views/ready',
'lib/session',
'lib/constants',
'lib/metrics',
'lib/ephemeral-messages',
'models/reliers/relier',
'models/auth_brokers/base',
'../../mocks/window',
'../../lib/helpers'
],
function (chai, _, Backbone, Router, SignInView, SignUpView, ReadyView,
Session, Constants, Metrics, EphemeralMessages, Relier,
NullBroker, WindowMock, TestHelpers) {
/*global describe, beforeEach, afterEach, it*/
var assert = chai.assert;
describe('lib/router', function () {
var router;
var windowMock;
var origNavigate;
var navigateUrl;
var navigateOptions;
var metrics;
var relier;
var broker;
var profileClientMock;
beforeEach(function () {
navigateUrl = navigateOptions = null;
$('#container').html('<div id="stage"></div>');
profileClientMock = TestHelpers.stubbedProfileClient();
windowMock = new WindowMock();
metrics = new Metrics();
relier = new Relier({
window: windowMock
});
broker = new NullBroker();
router = new Router({
window: windowMock,
metrics: metrics,
relier: relier,
broker: broker
});
origNavigate = Backbone.Router.prototype.navigate;
Backbone.Router.prototype.navigate = function (url, options) {
navigateUrl = url;
navigateOptions = options;
};
});
afterEach(function () {
metrics.destroy();
windowMock = router = navigateUrl = navigateOptions = metrics = null;
Backbone.Router.prototype.navigate = origNavigate;
$('#container').empty();
});
describe('navigate', function () {
it('Tells the router to navigate to a page', function () {
windowMock.location.search = '';
router.navigate('/signin');
assert.equal(navigateUrl, '/signin');
assert.deepEqual(navigateOptions, { trigger: true });
});
it('preserves window search parameters across screen transition',
function () {
windowMock.location.search = '?context=' + Constants.FX_DESKTOP_CONTEXT;
router.navigate('/forgot');
assert.equal(navigateUrl, '/forgot?context=' + Constants.FX_DESKTOP_CONTEXT);
assert.deepEqual(navigateOptions, { trigger: true });
});
});
describe('redirectToSignupOrSettings', function () {
it('replaces current page with the signup page if there is no sessionToken', function () {
windowMock.location.search = '';
Session.set('sessionToken', null);
router.redirectToSignupOrSettings();
assert.equal(navigateUrl, '/signup');
assert.deepEqual(navigateOptions, { trigger: true, replace: true });
});
it('replaces the current page with the settings page if there is a sessionToken', function () {
windowMock.location.search = '';
Session.set('sessionToken', 'abc123');
router.redirectToSignupOrSettings();
assert.equal(navigateUrl, '/settings');
assert.deepEqual(navigateOptions, { trigger: true, replace: true });
});
});
describe('showView, then another showView', function () {
var signInView, signUpView;
beforeEach(function () {
signInView = new SignInView({
metrics: metrics,
window: windowMock,
profileClient: profileClientMock,
relier: relier
});
signUpView = new SignUpView({
metrics: metrics,
window: windowMock,
profileClient: profileClientMock,
relier: relier
});
});
afterEach(function () {
signInView = signUpView = null;
});
it('shows a view, then shows the new view', function () {
windowMock.location.pathname = '/signin';
return router.showView(signInView)
.then(function () {
assert.ok($('#fxa-signin-header').length);
windowMock.location.pathname = '/signup';
return router.showView(signUpView);
})
.then(function () {
assert.ok($('#fxa-signup-header').length);
assert.isTrue(TestHelpers.isEventLogged(metrics, 'screen.signin'));
assert.isTrue(TestHelpers.isEventLogged(metrics, 'screen.signup'));
});
});
});
describe('showView', function () {
var view;
beforeEach(function () {
view = new SignUpView({
metrics: metrics,
window: windowMock,
router: router,
// ensure there is no cross talk with other tests.
ephemeralMessages: new EphemeralMessages(),
relier: relier
});
});
afterEach(function () {
view = null;
});
it('navigates to unexpected error view on beforeRender errors', function () {
windowMock.location.pathname = '/signup';
view.beforeRender = function () {
throw new Error('boom');
};
var navigate = view.navigate;
view.navigate = function (url, options) {
assert.equal(options.error.message, 'boom');
return navigate.call(this, url, options);
};
return router.showView(view)
.then(function () {
assert.include(navigateUrl, 'unexpected_error');
});
});
it('navigates to unexpected error view on context errors', function () {
windowMock.location.pathname = '/signup';
view.context = function () {
throw new Error('boom');
};
var navigate = view.navigate;
view.navigate = function (url, options) {
assert.equal(options.error.message, 'boom');
return navigate.call(this, url, options);
};
return router.showView(view)
.then(function () {
assert.include(navigateUrl, 'unexpected_error');
});
});
it('navigates to unexpected error view on afterRender errors', function () {
windowMock.location.pathname = '/signup';
view.afterRender = function () {
throw new Error('boom');
};
var navigate = view.navigate;
view.navigate = function (url, options) {
assert.equal(options.error.message, 'boom');
return navigate.call(this, url, options);
};
return router.showView(view)
.then(function () {
assert.include(navigateUrl, 'unexpected_error');
});
});
it('only logs a screen that has children once', function () {
windowMock.location.pathname = '/signup_complete';
view = new ReadyView({
metrics: metrics,
window: windowMock,
router: router,
// ensure there is no cross talk with other tests.
ephemeralMessages: new EphemeralMessages(),
relier: relier,
broker: broker,
type: 'sign_up'
});
return router.showView(view)
.then(function () {
assert.equal(metrics.getFilteredData().events.length, 1);
assert.isTrue(TestHelpers.isEventLogged(metrics, 'screen.signup_complete'));
});
});
});
});
});