UNPKG

9.82 kBJavaScriptView Raw
1'use strict';
2
3var sinon = require('sinon');
4var cookie = require('cookie');
5var Backbone = require('backbone');
6
7var ClientScenario = require('../scenarios/clientScenario');
8var endpoint = require('../../src/js/endpoint');
9
10var SK_STORAGE = 'sk_deviceid';
11
12describe('Main', function() {
13 var scenario;
14 var sandbox;
15 var SupportKit;
16
17 before(function() {
18 scenario = new ClientScenario();
19 scenario.build();
20 });
21
22 after(function() {
23 scenario.clean();
24 });
25
26 beforeEach(function(done) {
27 sandbox = sinon.sandbox.create();
28 SupportKit = require('../../src/js/main.js');
29 SupportKit.once('ready', function() {
30 sandbox.stub(SupportKit.user, 'save', function(attributes, options) {
31 return this._save(attributes, options);
32 });
33 done();
34 });
35 SupportKit.init({
36 appToken: 'thisisanapptoken'
37 });
38
39 });
40
41 afterEach(function() {
42 SupportKit.destroy();
43 delete global.SupportKit;
44 sandbox.restore();
45 });
46
47 describe('Global bindings', function() {
48 // those tests are using the expect form since undefined
49 // cannot be tested with the should syntax
50 it('should publish a global', function() {
51 global.SupportKit.should.exist;
52 });
53
54 it('should not publish dependencies in global context', function() {
55 expect(global.Backbone).to.not.exist;
56 expect(global._).to.not.exist;
57 });
58 });
59
60 describe('#init', function() {
61 var userId = 'thisisauserid';
62 var appToken = 'thisisanapptoken';
63 var jwt = 'thisisajwt';
64 var endpointSpy;
65 var trackSpy;
66 var initSpy;
67
68 beforeEach(function() {
69 trackSpy = sandbox.spy(SupportKit, 'track');
70 endpointSpy = sandbox.spy(endpoint, 'post');
71 initSpy = sandbox.spy();
72 });
73
74 it('should trigger ready, track appboot and resolve the promise', function(done) {
75 SupportKit.destroy();
76
77 SupportKit.once('ready', function() {
78 trackSpy.should.have.been.calledWith('skt-appboot');
79 initSpy.should.have.been.calledOnce;
80 done();
81 });
82
83 var initPromise = SupportKit.init({
84 appToken: appToken
85 });
86
87 initPromise.then(initSpy);
88 });
89
90 it('if supplied a userId should store the deviceId in local storage', function(done) {
91 SupportKit.destroy();
92
93 SupportKit.once('ready', function() {
94 localStorage.getItem(SK_STORAGE + '_' + userId).should.exist;
95 done();
96 });
97
98 SupportKit.init({
99 appToken: appToken,
100 userId: userId
101 });
102 });
103
104 it('should populate endpoint with supplied appToken and jwt', function(done) {
105 SupportKit.destroy();
106
107 SupportKit.once('ready', function() {
108 endpoint.jwt.should.eql(jwt);
109 endpoint.appToken.should.eql(appToken);
110 done();
111 });
112
113 SupportKit.init({
114 appToken: appToken,
115 jwt: jwt
116 });
117 });
118
119 it('should not populate endpoint jwt if unspecified', function(done) {
120 SupportKit.destroy();
121
122 SupportKit.once('ready', function() {
123 expect(endpoint.jwt).to.not.exist;
124 done();
125 });
126
127 SupportKit.init({
128 appToken: appToken
129 });
130 });
131
132 it('should post platform device info to appboot', function(done) {
133 SupportKit.destroy();
134
135 SupportKit.once('ready', function() {
136 expect(endpointSpy.args[0][1].deviceInfo.platform).to.equal('web');
137 done();
138 });
139
140 SupportKit.init({
141 appToken: appToken
142 });
143 });
144 });
145
146 describe('#logout', function() {
147 beforeEach(function() {
148 document.cookie = SK_STORAGE + '=' + 'test';
149 SupportKit.logout();
150 });
151
152 it('should remove the device id from cookies', function() {
153 expect(cookie.parse(document.cookie)[SK_STORAGE]).to.not.exist;
154 });
155
156 it('should clear the endpoint of all variables', function() {
157 expect(endpoint.appToken).to.not.exist;
158 expect(endpoint.jwt).to.not.exist;
159 expect(endpoint.appUserId).to.not.exist;
160 });
161 });
162
163 describe('#updateUser', function() {
164
165 // check if `save` actually calls the server or not
166 var syncSpy;
167 beforeEach(function() {
168 syncSpy = sandbox.spy(Backbone, 'sync');
169 });
170
171 it('should fail the promise if called with bad parameters (empty, in this case)', function(done) {
172 SupportKit.updateUser().fail(function() {
173 done();
174 });
175 });
176
177 it('should not call save if the user has not changed', function(done) {
178
179 SupportKit.updateUser({
180 givenName: 'GIVEN_NAME',
181 surname: 'SURNAME',
182 properties: {
183 'TEST': true
184 }
185 }).then(function() {
186 syncSpy.should.be.calledOnce;
187
188 return SupportKit.updateUser({
189 givenName: 'GIVEN_NAME',
190 surname: 'SURNAME',
191 properties: {
192 'TEST': true
193 }
194 });
195 }).then(function() {
196 syncSpy.should.be.calledOnce;
197 }).always(function() {
198 done();
199 });
200
201
202 });
203 });
204
205 describe('#_rulesContainEvent', function() {
206 it('should contain "in-rule" event', function() {
207 SupportKit._rulesContainEvent('in-rule-in-event').should.be.true;
208 SupportKit._rulesContainEvent('in-rule-not-event').should.be.true;
209 });
210
211 it('should not contain "not-in-rule" event', function() {
212 SupportKit._rulesContainEvent('not-rule-in-event').should.be.false;
213 });
214 });
215
216 describe('#_hasEvent', function() {
217 it('should contain "in-rule" and "not-in-rule" events', function() {
218 SupportKit._hasEvent('in-rule-in-event').should.be.true;
219 SupportKit._hasEvent('not-rule-in-event').should.be.true;
220 });
221
222 it('should not contain "not-in-event" event', function() {
223 SupportKit._hasEvent('in-rule-not-event').should.be.false;
224 });
225 });
226
227 describe('#track', function() {
228 var endpoint = require('../../src/js/endpoint');
229 var eventCreateSpy;
230 var endpointSpy;
231
232 beforeEach(function() {
233 eventCreateSpy = sandbox.spy(SupportKit._eventCollection, 'create');
234 endpointSpy = sandbox.spy(endpoint, 'put');
235 });
236
237 describe('tracking a new event', function() {
238
239 it('should call /api/event', function() {
240 SupportKit._hasEvent('new-event').should.be.false;
241 SupportKit._rulesContainEvent('new-event').should.be.false;
242
243 SupportKit.track('new-event');
244
245 endpointSpy.should.have.been.calledWith('api/event');
246 });
247 });
248
249 describe('tracking an existing event in rules', function() {
250
251 it('should create an event through the collection', function() {
252 SupportKit._rulesContainEvent('in-rule-not-event').should.be.true;
253 SupportKit._hasEvent('in-rule-not-event').should.be.false;
254
255 SupportKit.track('in-rule-not-event');
256
257
258 SupportKit._rulesContainEvent('in-rule-in-event').should.be.true;
259 SupportKit._hasEvent('in-rule-in-event').should.be.true;
260
261 SupportKit.track('in-rule-in-event');
262
263 eventCreateSpy.should.have.been.calledTwice;
264 });
265 });
266
267 describe('tracking an existing event not in rules', function() {
268 it('should do nothing if already in events and not in rules', function() {
269 SupportKit._rulesContainEvent('not-rule-in-event').should.be.false;
270 SupportKit._hasEvent('not-rule-in-event').should.be.true;
271
272 SupportKit.track('not-rule-in-event');
273
274 eventCreateSpy.should.not.have.been.called;
275 endpointSpy.should.not.have.been.called;
276 });
277 });
278
279
280 describe('skt-appboot', function() {
281
282 it('should do nothing if not in rules', function() {
283 SupportKit._rulesContainEvent('skt-appboot').should.be.false;
284 SupportKit._hasEvent('skt-appboot').should.be.true;
285
286 SupportKit.track('skt-appboot');
287
288 eventCreateSpy.should.not.have.been.called;
289 endpointSpy.should.not.have.been.called;
290 });
291
292
293 describe('in rules', function() {
294 beforeEach(function() {
295 SupportKit._ruleCollection.add({
296 '_id': '558c455fa2d213d0581f0a0b',
297 'events': ['skt-appboot']
298 }, {
299 parse: true
300 });
301 });
302
303 it('should create an event through the collection', function() {
304 SupportKit._rulesContainEvent('skt-appboot').should.be.true;
305 SupportKit._hasEvent('skt-appboot').should.be.true;
306
307 SupportKit.track('skt-appboot');
308
309 eventCreateSpy.should.have.been.calledOnce;
310 });
311 });
312 });
313 });
314});