/
choose_what_to_sync.js
282 lines (243 loc) · 8.45 KB
/
choose_what_to_sync.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* 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/. */
import $ from 'jquery';
import Account from 'models/account';
import { assert } from 'chai';
import Backbone from 'backbone';
import Broker from 'models/auth_brokers/base';
import { CHOOSE_WHAT_TO_SYNC } from '../../../../tests/functional/lib/selectors';
import Metrics from 'lib/metrics';
import Notifier from 'lib/channels/notifier';
import sinon from 'sinon';
import SentryMetrics from 'lib/sentry';
import SessionVerificationPoll from 'models/polls/session-verification';
import SyncEngines from 'models/sync-engines';
import TestHelpers from '../../lib/helpers';
import User from 'models/user';
import View from 'views/choose_what_to_sync';
import WindowMock from '../../mocks/window';
const Selectors = CHOOSE_WHAT_TO_SYNC;
describe('views/choose_what_to_sync', () => {
let account;
let broker;
let email;
let model;
let metrics;
let notifier;
let onSubmitComplete;
let sessionVerificationPoll;
let syncEngines;
let user;
let view;
let windowMock;
const DISPLAYED_ENGINE_IDS = [
'tabs',
'bookmarks',
'addons',
'passwords',
'history',
'prefs',
'creditcards'
];
beforeEach(() => {
windowMock = new WindowMock();
syncEngines = new SyncEngines(null, {
engines: DISPLAYED_ENGINE_IDS,
window: windowMock
});
broker = new Broker({
chooseWhatToSyncWebV1Engines: syncEngines
});
sinon.stub(broker, 'persistVerificationData').callsFake(() => Promise.resolve());
email = TestHelpers.createEmail();
model = new Backbone.Model();
notifier = new Notifier();
metrics = new Metrics({ notifier, sentryMetrics: new SentryMetrics() });
onSubmitComplete = sinon.spy();
user = new User({});
account = new Account({
email: email,
sessionToken: 'fake session token',
uid: 'uid'
});
model.set({
account: account,
onSubmitComplete: onSubmitComplete
});
sessionVerificationPoll = new SessionVerificationPoll({}, {
account,
pollIntervalInMS: 2,
window: windowMock
});
});
afterEach(() => {
metrics.destroy();
metrics = null;
view.remove();
view.destroy();
view = null;
});
function initView (options = {}) {
view = new View({
broker,
metrics,
model,
notifier,
sessionVerificationPoll,
user,
viewName: 'choose-what-to-sync'
});
sinon.spy(view, 'navigate');
sinon.stub(view, 'isTrailhead').callsFake(() => options.isTrailhead);
return view.render();
}
it('registers for the expected events', () => {
return initView()
.then(() => {
assert.isFunction(view.events['click a']);
assert.isFunction(view.events['click input']);
assert.isFunction(view.events['input input']);
assert.isFunction(view.events['keyup input']);
assert.isFunction(view.events['submit']);
});
});
describe('renders', () => {
it('coming from sign up, redirects to /signup when email accound data missing', () => {
account.clear('email');
return initView()
.then(() => {
assert.isTrue(view.navigate.calledWith('signup'));
});
});
it('renders email info, adds SCREEN_CLASS to body', () => {
return initView()
.then(() => {
assert.include(
view.$('.success-email-created').text(), email);
const $backEls = view.$('#back');
assert.lengthOf($backEls, 1);
assert.isTrue($('body').hasClass(View.SCREEN_CLASS));
const $rowEls = view.$('.choose-what-to-sync-row');
assert.lengthOf($rowEls, DISPLAYED_ENGINE_IDS.length);
assert.lengthOf(view.$(Selectors.PROGRESS_INDICATOR), 0);
assert.lengthOf(view.$(Selectors.NEWSLETTERS_HEADER), 0);
assert.lengthOf(view.$(Selectors.NEWSLETTERS.FIREFOX_ACCOUNTS_JOURNEY), 0);
assert.lengthOf(view.$(Selectors.NEWSLETTERS.HEALTHY_INTERNET), 0);
assert.lengthOf(view.$(Selectors.NEWSLETTERS.CONSUMER_BETA), 0);
assert.lengthOf(view.$(Selectors.NEWSLETTERS.ONLINE_SAFETY), 0);
});
});
it('renders progress indicator for trailhead', () => {
return initView({ isTrailhead: true })
.then(() => {
assert.lengthOf(view.$(Selectors.PROGRESS_INDICATOR), 1);
});
});
});
describe('afterVisible', () => {
it('persists verification data, starts poll', () => {
return initView()
.then(() => {
sinon.stub(view, 'waitForSessionVerification').callsFake(() => {});
return view.afterVisible();
})
.then(() => {
assert.isTrue(broker.persistVerificationData.calledOnce);
assert.isTrue(broker.persistVerificationData.calledWith(account));
assert.isTrue(view.waitForSessionVerification.calledOnce);
assert.isTrue(view.waitForSessionVerification.calledWith(account));
});
});
});
describe('session verification polling', () => {
it('invokes `validateAndSubmit` on verification', () => {
return initView()
.then(() => {
sinon.spy(view, 'waitForSessionVerification');
sinon.stub(view, 'validateAndSubmit').callsFake(() => {});
sinon.stub(sessionVerificationPoll, 'start').callsFake(() => {});
return view.afterVisible();
})
.then(() => {
assert.isTrue(sessionVerificationPoll.start.calledOnce);
assert.isFalse(view.validateAndSubmit.called);
sessionVerificationPoll.trigger('verified');
assert.isTrue(view.validateAndSubmit.calledOnce);
});
});
});
describe('destroy', () => {
it('removes SCREEN_CLASS from body, calls the parent', () => {
return initView()
.then(() => {
return new Promise((resolve) => {
view.on('destroyed', () => resolve());
view.destroy();
assert.isFalse($('body').hasClass(View.SCREEN_CLASS));
});
});
});
});
describe('_getOfferedEngines', () => {
it('gets a list of offered engines, suitable for displaying', () => {
return initView()
.then(() => {
const offeredEngines = view._getOfferedEngines();
assert.lengthOf(offeredEngines, DISPLAYED_ENGINE_IDS.length);
offeredEngines.forEach((offeredEngine) => {
assert.ok(offeredEngine.tabindex);
assert.ok(offeredEngine.text);
});
});
});
});
describe('_getDeclinedEngineIds', () => {
it('returns an array of declined engines', () => {
return initView()
.then(() => {
$('#container').html(view.el);
//decline the first engine
$('.customize-sync').first().click();
const declined = view._getDeclinedEngineIds();
assert.sameMembers(declined, ['tabs']);
});
});
});
describe('_getOfferedEngineIds', () => {
it('returns an array of offered engine ids', () => {
return initView()
.then(() => {
const offered = view._getOfferedEngineIds();
assert.sameMembers(offered, DISPLAYED_ENGINE_IDS);
});
});
});
describe('submit', () => {
beforeEach(() => {
sinon.stub(user, 'setAccount').callsFake(() => Promise.resolve(account));
sinon.spy(notifier, 'trigger');
});
it('updates and saves the account, logs metrics, calls onSubmitComplete', () => {
return initView()
.then(() => {
view.$('.customize-sync').first().removeAttr('checked');
return view.validateAndSubmit();
})
.then(() => {
const declined = account.get('declinedSyncEngines');
assert.sameMembers(declined, ['tabs']);
const offered = account.get('offeredSyncEngines');
assert.sameMembers(offered, DISPLAYED_ENGINE_IDS);
assert.isTrue(user.setAccount.calledWith(account));
assert.equal(notifier.trigger.callCount, 2);
const args = notifier.trigger.args[1];
assert.equal(args[0], 'set-sync-engines');
assert.deepEqual(args[1], DISPLAYED_ENGINE_IDS);
assert.isTrue(view.onSubmitComplete.calledOnce);
assert.instanceOf(view.onSubmitComplete.args[0][0], Account);
assert.isTrue(TestHelpers.isEventLogged(metrics, 'choose-what-to-sync.engine-unchecked.tabs'));
});
});
});
});