• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divio / django-cms / #30120

14 Nov 2025 01:00AM UTC coverage: 89.921%. Remained the same
#30120

push

travis-ci

web-flow
Merge 73415788f into c38b75715

1333 of 2144 branches covered (62.17%)

416 of 567 new or added lines in 27 files covered. (73.37%)

421 existing lines in 12 files now uncovered.

9207 of 10239 relevant lines covered (89.92%)

11.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.32
/cms/tests/frontend/unit/cms.base.test.js
1
'use strict';
2

3
import CMS, { Helpers, KEYS, uid } from '../../../static/cms/js/modules/cms.base';
4
var jQuery = require('jquery');
1✔
5
var $ = jQuery;
6
var showLoader;
7
var hideLoader;
8

9
CMS.API.Helpers = Helpers;
1✔
10
CMS.KEYS = KEYS;
1✔
11

12
window.CMS = window.CMS || CMS;
1✔
13

14
describe('cms.base.js', function() {
1✔
15
    fixture.setBase('cms/tests/frontend/unit/fixtures');
1✔
16

17
    // same implementation as in CMS.API.Helpers._isStorageSupported
18
    var _isLocalStorageSupported = (function() {
1✔
19
        var mod = 'modernizr';
1✔
20
        try {
1✔
21
            localStorage.setItem(mod, mod);
1✔
22
            localStorage.removeItem(mod);
1✔
23
            return true;
1✔
24
        } catch {
25
            // istanbul ignore next
26
            return false;
27
        }
28
    })();
29

30
    beforeEach(() => {
1✔
31
        showLoader = jasmine.createSpy();
49✔
32
        hideLoader = jasmine.createSpy();
49✔
33
        CMS.__Rewire__('showLoader', showLoader);
49✔
34
        CMS.__Rewire__('hideLoader', hideLoader);
49✔
35
    });
36

37
    afterEach(() => {
1✔
38
        CMS.__ResetDependency__('showLoader');
49✔
39
        CMS.__ResetDependency__('hideLoader');
49✔
40
    });
41

42
    it('creates CMS namespace', function() {
1✔
43
        expect(CMS).toBeDefined();
1✔
44
        expect(CMS).toEqual(jasmine.any(Object));
1✔
45
        expect(CMS.API).toEqual(jasmine.any(Object));
1✔
46
        expect(KEYS).toEqual(jasmine.any(Object));
1✔
47
    });
48

49
    describe('CMS.API', function() {
1✔
50
        it('exists', function() {
1✔
51
            expect(CMS.API.Helpers).toEqual(jasmine.any(Object));
1✔
52
            // this expectation is here so no one ever forgets to add a test
53
            expect(Object.keys(CMS.API.Helpers).length).toEqual(27);
1✔
54
        });
55

56
        describe('.reloadBrowser()', function() {
1✔
57
            /**
58
             * @function createFakeWindow
59
             * @returns {Object}
60
             */
61
            function createFakeWindow() {
62
                return {
3✔
63
                    parent: {
64
                        location: {
65
                            href: '',
66
                            reload: jasmine.createSpy()
67
                        },
68
                        CMS: {
69
                            config: {
70
                                request: {
71
                                    model: 'model',
72
                                    pk: 'pk'
73
                                }
74
                            },
75
                            API: {}
76
                        },
77
                        setTimeout: jasmine.createSpy().and.callFake(function(cb, timeout) {
78
                            expect(timeout).toEqual(0);
2✔
79
                            cb();
2✔
80
                        })
81
                    }
82
                };
83
            }
84
            /**
85
             * @function createWindowSpy
86
             * @param {Object} win
87
             */
88
            function createWindowSpy(win) {
89
                spyOn(CMS.API.Helpers, '_getWindow').and.callFake(function() {
3✔
90
                    return win;
3✔
91
                });
92
            }
93

94
            it('reloads the browser if no arguments passed', function() {
1✔
95
                var win = createFakeWindow();
1✔
96
                createWindowSpy(win);
1✔
97
                CMS.API.Helpers.reloadBrowser();
1✔
98

99
                expect(win.parent.location.reload).toHaveBeenCalled();
1✔
100
            });
101

102
            it('redirects the browser to an url if `url` was passed', function() {
1✔
103
                var win = createFakeWindow();
1✔
104
                createWindowSpy(win);
1✔
105
                CMS.API.Helpers.reloadBrowser('/url');
1✔
106

107
                expect(win.parent.location.href).toEqual('/url');
1✔
108
                expect(win.parent.location.reload).not.toHaveBeenCalled();
1✔
109
            });
110

111
            it('uses correct timeout', function() {
1✔
112
                var win = createFakeWindow();
1✔
113
                createWindowSpy(win);
1✔
114
                win.parent.CMS.config.request.url = '/my-url';
1✔
115
                spyOn(CMS.API.Helpers, 'reloadBrowser').and.callThrough();
1✔
116
                win.parent.setTimeout = jasmine.createSpy().and.callFake(function(cb, timeout) {
1✔
117
                    expect(timeout).toEqual(50);
1✔
118
                    cb();
1✔
119
                });
120
                // for some reason jasmine.Ajax.install() didn't work here
121
                // presumably because of window.parent shenanigans.
122
                spyOn($, 'ajax').and.callFake(function(opts) {
1✔
UNCOV
123
                    opts.success('');
×
124
                });
125

126
                expect(CMS.API.Helpers.reloadBrowser('/new-url', 50, false)).toEqual(undefined);
1✔
127
                expect($.ajax).not.toHaveBeenCalled();
1✔
128
                expect(win.parent.location.href).toEqual('/new-url');
1✔
129
            });
130
        });
131

132
        describe('onPluginSave()', function() {
1✔
133
            beforeEach(() => {
1✔
134
                CMS.API.StructureBoard = {
4✔
135
                    invalidateState: jasmine.createSpy()
136
                };
137
            });
138
            afterEach(() => {
1✔
139
                CMS.API.Helpers.dataBridge = null;
4✔
140
            });
141

142
            it('invalidates state if the plugin was edited', () => {
1✔
143
                CMS._instances = [{ options: { plugin_id: 1, type: 'plugin' } }];
1✔
144

145
                CMS.API.Helpers.dataBridge = { plugin_id: '1', action: 'edit' };
1✔
146
                CMS.API.Helpers.onPluginSave();
1✔
147
                expect(CMS.API.StructureBoard.invalidateState).toHaveBeenCalledWith(
1✔
148
                    'EDIT', { plugin_id: '1', action: 'edit' }
149
                );
150
            });
151

152
            it('invalidates state if the plugin was added', () => {
1✔
153
                CMS._instances = [];
1✔
154

155
                CMS.API.Helpers.dataBridge = { plugin_id: '1', action: 'add' };
1✔
156
                CMS.API.Helpers.onPluginSave();
1✔
157
                expect(CMS.API.StructureBoard.invalidateState).toHaveBeenCalledWith(
1✔
158
                    'ADD', { plugin_id: '1', action: 'add' }
159
                );
160
            });
161

162
            it('invalidates state if the plugin was added', () => {
1✔
163
                CMS._instances = [{ options: { plugin_id: 1, type: 'generic' } }];
1✔
164

165
                // FrontendEditableMixin issues "change" action, databridge processes it as an
166
                // add action
167
                CMS.API.Helpers.dataBridge = { plugin_id: '1', action: 'change' };
1✔
168
                CMS.API.Helpers.onPluginSave();
1✔
169
                expect(CMS.API.StructureBoard.invalidateState).toHaveBeenCalledWith(
1✔
170
                    'ADD', { plugin_id: '1', action: 'change' }
171
                );
172
            });
173

174
            it('proxies to reloadBrowser', function() {
1✔
175
                spyOn(CMS.API.Helpers, 'reloadBrowser');
1✔
176

177
                CMS.API.Helpers._isReloading = false;
1✔
178

179
                CMS.API.Helpers.onPluginSave();
1✔
180
                expect(CMS.API.Helpers.reloadBrowser).toHaveBeenCalledTimes(1);
1✔
181
                expect(CMS.API.Helpers.reloadBrowser).toHaveBeenCalledWith(null, 300);
1✔
182
            });
183
        });
184

185
        describe('.preventSubmit()', function() {
1✔
186
            beforeEach(function(done) {
1✔
187
                fixture.load('toolbar_form.html');
1✔
188
                $(function() {
1✔
189
                    done();
1✔
190
                });
191
            });
192

193
            afterEach(function() {
1✔
194
                fixture.cleanup();
1✔
195
            });
196

197
            it('should prevent forms from being submitted when one form is submitted', function() {
1✔
198
                var submitCallback = jasmine.createSpy().and.returnValue(false);
1✔
199

200
                CMS.API.Helpers.preventSubmit();
1✔
201
                var form = $('.cms-toolbar #form1');
1✔
202
                var input1 = $('input[type=submit]').eq(0);
1✔
203
                var input2 = $('input[type=submit]').eq(1);
1✔
204
                form.submit(submitCallback);
1✔
205
                form.find('input').trigger('click');
1✔
206

207
                expect(input1).toHaveCss({ opacity: '0.5' });
1✔
208
                expect(input2).toHaveCss({ opacity: '0.5' });
1✔
209

210
                spyOnEvent(input1, 'click');
1✔
211
                spyOnEvent(input2, 'click');
1✔
212

213
                input1.trigger('click');
1✔
214
                input2.trigger('click');
1✔
215

216
                expect('click').toHaveBeenPreventedOn(input1);
1✔
217
                expect('click').toHaveBeenPreventedOn(input2);
1✔
218
                expect(showLoader).toHaveBeenCalled();
1✔
219
                expect(submitCallback).toHaveBeenCalled();
1✔
220
            });
221
        });
222

223
        describe('.csrf()', function() {
1✔
224
            it('should set csrf token on ajax requests', function() {
1✔
225
                var token = 'csrf';
1✔
226
                var request;
227

228
                jasmine.Ajax.install();
1✔
229
                $.ajax('/test');
1✔
230
                request = jasmine.Ajax.requests.mostRecent();
1✔
231
                expect(request.requestHeaders['X-CSRFToken']).toEqual(undefined);
1✔
232

233
                spyOn($, 'ajaxSetup').and.callThrough();
1✔
234
                CMS.API.Helpers.csrf(token);
1✔
235
                expect($.ajaxSetup).toHaveBeenCalled();
1✔
236
                expect($.ajaxSetup.calls.count()).toEqual(1);
1✔
237

238
                $.ajax('/test');
1✔
239
                request = jasmine.Ajax.requests.mostRecent();
1✔
240
                expect(request.requestHeaders['X-CSRFToken']).toEqual(token);
1✔
241
                jasmine.Ajax.uninstall();
1✔
242
            });
243
        });
244

245
        describe('.setSettings()', function() {
1✔
246
            beforeEach(function() {
1✔
247
                CMS.API.Helpers._isStorageSupported = true;
4✔
248
                if (_isLocalStorageSupported) {
4!
249
                    localStorage.clear();
4✔
250
                }
251
                jasmine.Ajax.install();
4✔
252

253
                jasmine.Ajax.stubRequest('/my-settings-url').andReturn({
4✔
254
                    status: 200,
255
                    contentType: 'text/plain',
256
                    responseText: '{"serverSetting":true,"version":"same"}'
257
                });
258

259
                jasmine.Ajax.stubRequest('/my-settings-url-with-empty-response').andReturn({
4✔
260
                    status: 200,
261
                    contentType: 'text/plain',
262
                    responseText: ''
263
                });
264

265
                jasmine.Ajax.stubRequest('/my-settings-url').andReturn({
4✔
266
                    status: 200,
267
                    contentType: 'text/plain',
268
                    responseText: '{"serverSetting":true}'
269
                });
270

271
                jasmine.Ajax.stubRequest('/my-broken-settings-url').andReturn({
4✔
272
                    status: 500,
273
                    responseText: 'Fail'
274
                });
275
            });
276

277
            afterEach(function() {
1✔
278
                jasmine.Ajax.uninstall();
4✔
279
            });
280

281
            it('should put settings in localStorage if it is available', function() {
1✔
282
                if (!_isLocalStorageSupported) {
1!
UNCOV
283
                    pending('Localstorage is not supported, skipping');
×
284
                }
285

286
                CMS.config = {
1✔
287
                    settings: {}
288
                };
289

290
                expect(CMS.API.Helpers.setSettings({ mySetting: true })).toEqual({ mySetting: true });
1✔
291
                expect(localStorage.getItem('cms_cookie')).toEqual('{"mySetting":true}');
1✔
292

293
                CMS.config.settings = { mySetting: false };
1✔
294
                expect(CMS.API.Helpers.setSettings({ anotherSetting: true })).toEqual({
1✔
295
                    mySetting: false,
296
                    anotherSetting: true
297
                });
298
                expect(localStorage.getItem('cms_cookie')).toEqual('{"mySetting":false,"anotherSetting":true}');
1✔
299

300
                expect(CMS.API.Helpers.setSettings({ mySetting: true })).toEqual({
1✔
301
                    mySetting: true
302
                });
303
                expect(localStorage.getItem('cms_cookie')).toEqual('{"mySetting":true}');
1✔
304
            });
305

306
            it('makes a synchronous request to the session url if localStorage is not available', function() {
1✔
307
                CMS.API.Helpers._isStorageSupported = false;
1✔
308
                CMS.config = {
1✔
309
                    urls: {
310
                        settings: '/my-settings-url'
311
                    }
312
                };
313
                expect(CMS.API.Helpers.setSettings({ mySetting: true })).toEqual({ serverSetting: true });
1✔
314

315
                expect(showLoader.calls.count()).toEqual(1);
1✔
316
                expect(hideLoader.calls.count()).toEqual(1);
1✔
317
            });
318

319
            it('uses default settings if response is empty', function() {
1✔
320
                CMS.API.Helpers._isStorageSupported = false;
1✔
321
                CMS.config = {
1✔
322
                    settings: {
323
                        defaultSetting: true
324
                    },
325
                    urls: {
326
                        settings: '/my-settings-url-with-empty-response'
327
                    }
328
                };
329

330
                expect(CMS.API.Helpers.setSettings({ mySetting: true })).toEqual({ defaultSetting: true });
1✔
331

332
                expect(showLoader.calls.count()).toEqual(1);
1✔
333
                expect(hideLoader.calls.count()).toEqual(1);
1✔
334
            });
335

336
            it('makes a synchronous request which can fail', function() {
1✔
337
                CMS.API.Helpers._isStorageSupported = false;
1✔
338
                CMS.config = {
1✔
339
                    urls: {
340
                        settings: '/my-broken-settings-url'
341
                    }
342
                };
343
                CMS.API.Messages = {
1✔
344
                    open: jasmine.createSpy()
345
                };
346

347
                expect(CMS.API.Helpers.setSettings({ mySetting: true })).toEqual({ mySetting: true });
1✔
348

349
                expect(showLoader).toHaveBeenCalled();
1✔
350
                expect(hideLoader).not.toHaveBeenCalled();
1✔
351
                expect(CMS.API.Messages.open).toHaveBeenCalledWith({
1✔
352
                    message: 'Fail | 500 error',
353
                    error: true
354
                });
355
            });
356
        });
357

358
        describe('.getSettings()', function() {
1✔
359
            beforeEach(function() {
1✔
360
                CMS.API.Helpers._isStorageSupported = true;
7✔
361
                if (_isLocalStorageSupported) {
7!
362
                    localStorage.clear();
7✔
363
                }
364
                jasmine.Ajax.install();
7✔
365

366
                jasmine.Ajax.stubRequest('/my-settings-url-with-empty-response').andReturn({
7✔
367
                    status: 200,
368
                    contentType: 'text/plain',
369
                    responseText: ''
370
                });
371

372
                jasmine.Ajax.stubRequest('/my-settings-url').andReturn({
7✔
373
                    status: 200,
374
                    contentType: 'text/plain',
375
                    responseText: `{"serverSetting":true,"version":"${__CMS_VERSION__}","edit_off":1}`
376
                });
377

378
                jasmine.Ajax.stubRequest('/my-broken-settings-url').andReturn({
7✔
379
                    status: 500,
380
                    responseText: 'Fail'
381
                });
382
            });
383

384
            afterEach(function() {
1✔
385
                jasmine.Ajax.uninstall();
7✔
386
            });
387

388
            it('should get settings from localStorage', function() {
1✔
389
                if (!_isLocalStorageSupported) {
1!
UNCOV
390
                    pending('Localstorage is not supported, skipping');
×
391
                }
392

393
                localStorage.setItem(
1✔
394
                    'cms_cookie',
395
                    JSON.stringify({ version: __CMS_VERSION__, presetSetting: true, edit_off: 1 })
396
                );
397
                CMS.settings = {};
1✔
398
                CMS.config = {
1✔
399
                    settings: {
400
                        version: __CMS_VERSION__
401
                    }
402
                };
403

404
                expect(CMS.API.Helpers.getSettings()).toEqual({
1✔
405
                    presetSetting: true,
406
                    version: __CMS_VERSION__,
407
                    edit_off: 1
408
                });
409

410
                expect(showLoader.calls.count()).toEqual(0);
1✔
411
                expect(hideLoader.calls.count()).toEqual(0);
1✔
412
            });
413

414
            it('should get settings from config if there is a version mismatch', function() {
1✔
415
                if (!_isLocalStorageSupported) {
1!
UNCOV
416
                    pending('Localstorage is not supported, skipping');
×
417
                }
418

419
                localStorage.setItem(
1✔
420
                    'cms_cookie',
421
                    JSON.stringify({ version: 'old', presetSetting: true, edit_off: 1 })
422
                );
423
                CMS.settings = {};
1✔
424
                CMS.config = {
1✔
425
                    settings: {
426
                        version: 'new',
427
                        other_stuff: true
428
                    }
429
                };
430

431
                expect(CMS.API.Helpers.getSettings()).toEqual({ version: 'new', other_stuff: true });
1✔
432

433
                expect(showLoader.calls.count()).toEqual(0);
1✔
434
                expect(hideLoader.calls.count()).toEqual(0);
1✔
435
            });
436

437
            it('should get settings from localStorage and cms.config if required settings are not there', function() {
1✔
438
                if (!_isLocalStorageSupported) {
1!
UNCOV
439
                    pending('Localstorage is not supported, skipping');
×
440
                }
441

442
                localStorage.setItem('cms_cookie', JSON.stringify({ presetSetting: true }));
1✔
443
                CMS.settings = {};
1✔
444
                CMS.config = {
1✔
445
                    settings: {
446
                        fromConfig: true
447
                    }
448
                };
449

450
                expect(CMS.API.Helpers.getSettings()).toEqual({ fromConfig: true });
1✔
451

452
                expect(showLoader.calls.count()).toEqual(0);
1✔
453
                expect(hideLoader.calls.count()).toEqual(0);
1✔
454
            });
455

456
            it('should first set settings from CMS.config is there are no settings in localstorage', function() {
1✔
457
                if (!_isLocalStorageSupported) {
1!
UNCOV
458
                    pending('Localstorage is not supported, skipping');
×
459
                }
460

461
                CMS.settings = {};
1✔
462
                CMS.config = {
1✔
463
                    settings: {
464
                        configSetting: true
465
                    }
466
                };
467
                spyOn(CMS.API.Helpers, 'setSettings').and.callThrough();
1✔
468

469
                expect(CMS.API.Helpers.getSettings()).toEqual({ configSetting: true });
1✔
470

471
                expect(showLoader.calls.count()).toEqual(0);
1✔
472
                expect(hideLoader.calls.count()).toEqual(0);
1✔
473
                expect(CMS.API.Helpers.setSettings).toHaveBeenCalled();
1✔
474
            });
475

476
            it('makes a synchronous request to the session url if localStorage is not available', function() {
1✔
477
                CMS.API.Helpers._isStorageSupported = false;
1✔
478
                CMS.config = {
1✔
479
                    settings: {
480
                        version: __CMS_VERSION__
481
                    },
482
                    urls: {
483
                        settings: '/my-settings-url'
484
                    }
485
                };
486

487
                expect(CMS.API.Helpers.getSettings()).toEqual({
1✔
488
                    version: __CMS_VERSION__,
489
                    serverSetting: true,
490
                    edit_off: 1
491
                });
492

493
                expect(showLoader.calls.count()).toEqual(1);
1✔
494
                expect(hideLoader.calls.count()).toEqual(1);
1✔
495
            });
496

497
            it('uses default settings if response is empty', function() {
1✔
498
                CMS.API.Helpers._isStorageSupported = false;
1✔
499
                CMS.config = {
1✔
500
                    settings: {
501
                        defaultSetting: true
502
                    },
503
                    urls: {
504
                        settings: '/my-settings-url-with-empty-response'
505
                    }
506
                };
507

508
                expect(CMS.API.Helpers.getSettings()).toEqual({ defaultSetting: true });
1✔
509

510
                expect(showLoader.calls.count()).toEqual(2);
1✔
511
                expect(hideLoader.calls.count()).toEqual(2);
1✔
512
            });
513

514
            it('makes a synchronous request which can fail', function() {
1✔
515
                CMS.API.Helpers._isStorageSupported = false;
1✔
516
                CMS.config = {
1✔
517
                    settings: { test: false },
518
                    urls: {
519
                        settings: '/my-broken-settings-url'
520
                    }
521
                };
522
                CMS.API.Messages = {
1✔
523
                    open: jasmine.createSpy()
524
                };
525

526
                expect(CMS.API.Helpers.getSettings()).toEqual({ test: false });
1✔
527

528
                expect(showLoader).toHaveBeenCalled();
1✔
529
                expect(hideLoader).not.toHaveBeenCalled();
1✔
530
                expect(CMS.API.Messages.open).toHaveBeenCalledWith({
1✔
531
                    message: 'Fail | 500 error',
532
                    error: true
533
                });
534
            });
535
        });
536

537
        describe('.makeURL()', function() {
1✔
538
            it('outputs the same url when no additional params passed', function() {
1✔
539
                var url;
540
                url = CMS.API.Helpers.makeURL('test');
1✔
541
                expect(url).toEqual('test');
1✔
542

543
                url = CMS.API.Helpers.makeURL('https://google.com/');
1✔
544
                expect(url).toEqual('https://google.com/');
1✔
545
            });
546

547
            it('outputs new url when additional params passed', function() {
1✔
548
                var url;
549

550
                url = CMS.API.Helpers.makeURL('test', [['param', '1']]);
1✔
551
                expect(url).toEqual('test?param=1');
1✔
552
            });
553

554
            it('outputs new url when there are multiple additional params', function() {
1✔
555
                var url;
556

557
                url = CMS.API.Helpers.makeURL('test', [['param', '1'], ['another', '2']]);
1✔
558
                expect(url).toEqual('test?param=1&another=2');
1✔
559

560
                url = CMS.API.Helpers.makeURL('test?param=1', [['another', '2']]);
1✔
561
                expect(url).toEqual('test?param=1&another=2');
1✔
562

563
                url = CMS.API.Helpers.makeURL('test?param=1&another=2', [['different', '3']]);
1✔
564
                expect(url).toEqual('test?param=1&another=2&different=3');
1✔
565

566
                url = CMS.API.Helpers.makeURL('test?param=1&another=2', [['different', '3']]);
1✔
567
                expect(url).toEqual('test?param=1&another=2&different=3');
1✔
568

569
                url = CMS.API.Helpers.makeURL('test?param=1&another=2&again=3', [['different', '3']]);
1✔
570
                expect(url).toEqual('test?param=1&another=2&again=3&different=3');
1✔
571
            });
572

573
            it('replaces param values with new ones if they match', function() {
1✔
574
                var url;
575

576
                url = CMS.API.Helpers.makeURL('test?param=1&another=2', [['another', '3']]);
1✔
577
                expect(url).toEqual('test?param=1&another=3');
1✔
578

579
                url = CMS.API.Helpers.makeURL('test?param=1&another=2', [['another', '3'], ['param', '4']]);
1✔
580
                expect(url).toEqual('test?another=3&param=4');
1✔
581
            });
582

583
            it('understands hashes in the url', function() {
1✔
584
                var url;
585

586
                url = CMS.API.Helpers.makeURL('test#hash', [['param', '1'], ['another', '2']]);
1✔
587
                expect(url).toEqual('test?param=1&another=2#hash');
1✔
588

589
                url = CMS.API.Helpers.makeURL('test#hash#with#hash', [['param', '1'], ['another', '2']]);
1✔
590
                expect(url).toEqual('test?param=1&another=2#hash#with#hash');
1✔
591

592
                url = CMS.API.Helpers.makeURL('test#', [['param', '1'], ['another', '2']]);
1✔
593
                expect(url).toEqual('test?param=1&another=2');
1✔
594

595
                url = CMS.API.Helpers.makeURL('test#hash&stuff', [['param', '1'], ['another', '2']]);
1✔
596
                expect(url).toEqual('test?param=1&another=2#hash&stuff');
1✔
597

598
                url = CMS.API.Helpers.makeURL('test#hash&stuff', []);
1✔
599
                expect(url).toEqual('test#hash&stuff');
1✔
600
            });
601
        });
602

603
        describe('.secureConfirm()', function() {
1✔
604
            it('returns true if confirm is prevented', function() {
1✔
605
                spyOn(window, 'confirm').and.callFake(function(message) {
1✔
606
                    expect(message).toEqual('message');
1✔
607
                    return false;
1✔
608
                });
609
                expect(CMS.API.Helpers.secureConfirm('message')).toEqual(true);
1✔
610
            });
611

612
            it('returns actual value if confirm is not prevented', function() {
1✔
613
                jasmine.clock().install();
1✔
614
                jasmine.clock().mockDate();
1✔
615
                spyOn(window, 'confirm').and.callFake(function() {
1✔
616
                    jasmine.clock().tick(15);
1✔
617
                    return false;
1✔
618
                });
619

620
                expect(CMS.API.Helpers.secureConfirm('cms')).toEqual(false);
1✔
621

622
                window.confirm.and.callFake(function() {
1✔
623
                    jasmine.clock().tick(15);
1✔
624
                    return true;
1✔
625
                });
626

627
                expect(CMS.API.Helpers.secureConfirm('cms')).toEqual(true);
1✔
628

629
                jasmine.clock().uninstall();
1✔
630
            });
631
        });
632

633
        describe('.addEventListener()', function() {
1✔
634
            beforeEach(function(done) {
1✔
635
                fixture.load('cms_root.html');
2✔
636
                $(function() {
2✔
637
                    done();
2✔
638
                });
639
            });
640

641
            afterEach(function() {
1✔
642
                fixture.cleanup();
2✔
643
            });
644

645
            it('adds an event', function() {
1✔
646
                CMS._eventRoot = $('#cms-top');
1✔
647
                CMS.API.Helpers.addEventListener('my-event', $.noop);
1✔
648

649
                expect($('#cms-top')).toHandle('cms-my-event');
1✔
650
            });
651
            it('adds multiple events', function() {
1✔
652
                CMS._eventRoot = $('#cms-top');
1✔
653
                CMS.API.Helpers.addEventListener('my-event my-other-event', $.noop);
1✔
654

655
                expect($('#cms-top')).toHandle('cms-my-event');
1✔
656
                expect($('#cms-top')).toHandle('cms-my-other-event');
1✔
657
            });
658
        });
659

660
        describe('.removeEventListener()', function() {
1✔
661
            beforeEach(function(done) {
1✔
662
                fixture.load('cms_root.html');
3✔
663
                $(function() {
3✔
664
                    done();
3✔
665
                });
666
            });
667

668
            afterEach(function() {
1✔
669
                fixture.cleanup();
3✔
670
            });
671
            it('removes an event', function() {
1✔
672
                CMS._eventRoot = $('#cms-top');
1✔
673

674
                CMS.API.Helpers.addEventListener('my-event', $.noop);
1✔
675
                CMS.API.Helpers.removeEventListener('my-event');
1✔
676

677
                expect($('#cms-top')).not.toHandle('cms-my-event');
1✔
678
            });
679

680
            it('removes an event with correct handler', function() {
1✔
681
                CMS._eventRoot = $('#cms-top');
1✔
682
                var fn = function() {
1✔
UNCOV
683
                    expect(true).toEqual(true);
×
684
                };
685

686
                CMS.API.Helpers.addEventListener('my-event', $.noop);
1✔
687
                CMS.API.Helpers.addEventListener('my-event', fn);
1✔
688
                CMS.API.Helpers.removeEventListener('my-event', $.noop);
1✔
689

690
                expect($('#cms-top')).toHandleWith('cms-my-event', fn);
1✔
691
                expect($('#cms-top')).not.toHandleWith('cms-my-event', $.noop);
1✔
692
            });
693

694
            it('removes multiple events', function() {
1✔
695
                CMS._eventRoot = $('#cms-top');
1✔
696

697
                CMS.API.Helpers.addEventListener('my-event my-other-event', $.noop);
1✔
698
                CMS.API.Helpers.removeEventListener('my-event my-other-event');
1✔
699

700
                expect($('#cms-top')).not.toHandle('cms-my-event');
1✔
701
                expect($('#cms-top')).not.toHandle('cms-my-other-event');
1✔
702
            });
703
        });
704

705
        describe('.dispatchEvent()', function() {
1✔
706
            beforeEach(function(done) {
1✔
707
                fixture.load('cms_root.html');
6✔
708
                $(function() {
6✔
709
                    done();
6✔
710
                });
711
            });
712

713
            afterEach(function() {
1✔
714
                fixture.cleanup();
6✔
715
            });
716
            it('dispatches an event', function() {
1✔
717
                CMS._eventRoot = $('#cms-top');
1✔
718
                var fn = jasmine.createSpy();
1✔
719
                CMS.API.Helpers.addEventListener('my-event', fn);
1✔
720
                CMS.API.Helpers.dispatchEvent('my-event');
1✔
721
                expect(fn).toHaveBeenCalled();
1✔
722
            });
723

724
            it('does not dispatch multiple events', function() {
1✔
725
                CMS._eventRoot = $('#cms-top');
1✔
726
                var fn1 = jasmine.createSpy();
1✔
727
                var fn2 = jasmine.createSpy();
1✔
728

729
                CMS.API.Helpers.addEventListener('my-event', fn1);
1✔
730
                CMS.API.Helpers.addEventListener('my-another-event', fn2);
1✔
731
                CMS.API.Helpers.dispatchEvent('my-event my-another-event');
1✔
732
                expect(fn1).not.toHaveBeenCalled();
1✔
733
                expect(fn2).not.toHaveBeenCalled();
1✔
734
            });
735

736
            it('can attach payload to event', function() {
1✔
737
                CMS._eventRoot = $('#cms-top');
1✔
738
                var fn = jasmine.createSpy();
1✔
739

740
                CMS.API.Helpers.addEventListener('my-event', fn);
1✔
741
                CMS.API.Helpers.dispatchEvent('my-event', {
1✔
742
                    payload: 'djangoCMS'
743
                });
744
                expect(fn).toHaveBeenCalledWith(jasmine.any(Object), {
1✔
745
                    payload: 'djangoCMS'
746
                });
747
            });
748

749
            it('returns dispatched event', function() {
1✔
750
                CMS._eventRoot = $('#cms-top');
1✔
751
                var fn = jasmine.createSpy();
1✔
752

753
                CMS.API.Helpers.addEventListener('my-event', fn);
1✔
754
                expect(CMS.API.Helpers.dispatchEvent('my-event') instanceof $.Event).toEqual(true);
1✔
755
            });
756

757
            it('can has namespaces', function() {
1✔
758
                CMS._eventRoot = $('#cms-top');
1✔
759
                var fn = jasmine.createSpy();
1✔
760

761
                CMS.API.Helpers.addEventListener('my-event.namespace', fn);
1✔
762
                CMS.API.Helpers.dispatchEvent('my-event', {
1✔
763
                    payload: 'djangoCMS'
764
                });
765
                expect(fn).toHaveBeenCalledWith(jasmine.any(Object), {
1✔
766
                    payload: 'djangoCMS'
767
                });
768
            });
769

770
            it('can has namespaces the other way', function() {
1✔
771
                CMS._eventRoot = $('#cms-top');
1✔
772
                var fn = jasmine.createSpy();
1✔
773

774
                CMS.API.Helpers.addEventListener('my-event', fn);
1✔
775
                CMS.API.Helpers.dispatchEvent('my-event.namespace', {
1✔
776
                    payload: 'djangoCMS'
777
                });
778
                expect(fn).not.toHaveBeenCalled();
1✔
779
            });
780
        });
781

782
        describe('.preventTouchScrolling()', function() {
1✔
783
            it('prevents touch move on an element', function() {
1✔
784
                CMS.API.Helpers.preventTouchScrolling($(document), 'tests');
1✔
785
                expect($(document)).toHandle('touchmove');
1✔
786
                expect($(document)).toHandle('touchmove.cms.preventscroll.tests');
1✔
787
                var event = spyOnEvent(document, 'touchmove');
1✔
788
                $(document).trigger('touchmove');
1✔
789
                expect(event).toHaveBeenPrevented();
1✔
790
            });
791
        });
792

793
        // depends on the previous one
794
        describe('.allowTouchScrolling()', function() {
1✔
795
            it('allows touch move on an element', function() {
1✔
796
                expect($(document)).toHandle('touchmove');
1✔
797
                expect($(document)).toHandle('touchmove.cms.preventscroll.tests');
1✔
798
                CMS.API.Helpers.allowTouchScrolling($(document), 'tests');
1✔
799
                var event = spyOnEvent(document, 'touchmove');
1✔
800
                $(document).trigger('touchmove');
1✔
801
                expect(event).not.toHaveBeenPrevented();
1✔
802
            });
803
        });
804

805
        describe('._getWindow()', function() {
1✔
806
            it('returns window', function() {
1✔
807
                expect(CMS.API.Helpers._getWindow()).toEqual(window);
1✔
808
            });
809
        });
810

811
        describe('.uid()', function() {
1✔
812
            it('returns a number', function() {
1✔
813
                expect(uid()).toEqual(jasmine.any(Number));
1✔
814
            });
815

816
            it('returns always different ids', function() {
1✔
817
                expect(uid()).not.toBe(uid());
1✔
818
            });
819
        });
820

821
        describe('.updateUrlWithPath()', function() {
1✔
822
            it('supports query strings', function() {
1✔
823
                spyOn(CMS.API.Helpers, '_getWindow').and.returnValue({
1✔
824
                    location: {
825
                        pathname: '/de/',
826
                        search: '?language=en'
827
                    }
828
                });
829

830
                expect(CMS.API.Helpers.updateUrlWithPath('/')).toEqual('/?cms_path=%2Fde%2F%3Flanguage%3Den');
1✔
831
            });
832
        });
833

834
        describe('.setColorScheme() and .getColorScheme()', function() {
1✔
835
            it('allows setting of dark color scheme', function() {
1✔
836
                CMS.API.Helpers.setColorScheme('dark');
1✔
837
                expect(CMS.API.Helpers.getColorScheme()).toEqual('dark');
1✔
838
            });
839
            it('allows setting of light color scheme', function() {
1✔
840
                CMS.API.Helpers.setColorScheme('light');
1✔
841
                expect(CMS.API.Helpers.getColorScheme()).toEqual('light');
1✔
842
            });
843
            it('allows setting of system color scheme', function() {
1✔
844
                CMS.API.Helpers.setColorScheme('auto');
1✔
845
                expect(CMS.API.Helpers.getColorScheme()).toEqual('auto');
1✔
846
            });
847
        });
848
    });
849
});
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc