• 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

94.58
/cms/tests/frontend/unit/cms.toolbar.test.js
1
'use strict';
2
var CMS = require('../../../static/cms/js/modules/cms.base').default;
1✔
3
var Sideframe = require('../../../static/cms/js/modules/cms.sideframe').default;
1✔
4
var Messages = require('../../../static/cms/js/modules/cms.messages').default;
1✔
5
var $ = require('jquery');
1✔
6
import Toolbar from '../../../static/cms/js/modules/cms.toolbar';
7
import Modal from '../../../static/cms/js/modules/cms.modal';
8

9
CMS.API = CMS.API || {};
1!
10
CMS.API.Helpers = Toolbar.__GetDependency__('Helpers');
1✔
11
CMS.KEYS = Toolbar.__GetDependency__('KEYS');
1✔
12

13
window.CMS = window.CMS || CMS;
1!
14
CMS.Toolbar = Toolbar;
1✔
15
CMS.Modal = Modal;
1✔
16
CMS.Sideframe = Sideframe;
1✔
17
CMS.Messages = Messages;
1✔
18
var showLoader;
19
var hideLoader;
20

21
class Navigation {}
22

23
describe('CMS.Toolbar', function () {
1✔
24
    fixture.setBase('cms/tests/frontend/unit/fixtures');
1✔
25

26
    beforeEach(() => {
1✔
27
        showLoader = jasmine.createSpy();
37✔
28
        hideLoader = jasmine.createSpy();
37✔
29
        Toolbar.__Rewire__('showLoader', showLoader);
37✔
30
        Toolbar.__Rewire__('hideLoader', hideLoader);
37✔
31
        Toolbar.__Rewire__('Navigation', Navigation);
37✔
32
    });
33

34
    afterEach(() => {
1✔
35
        Toolbar.__ResetDependency__('Navigation');
37✔
36
        Toolbar.__ResetDependency__('showLoader');
37✔
37
        Toolbar.__ResetDependency__('hideLoader');
37✔
38
    });
39

40
    it('creates a Toolbar class', function () {
1✔
41
        expect(CMS.Toolbar).toBeDefined();
1✔
42
    });
43

44
    it('has public API', function () {
1✔
45
        expect(CMS.Toolbar.prototype.showLoader).toEqual(jasmine.any(Function));
1✔
46
        expect(CMS.Toolbar.prototype.hideLoader).toEqual(jasmine.any(Function));
1✔
47
        expect(CMS.Toolbar.prototype.openAjax).toEqual(jasmine.any(Function));
1✔
48
    });
49

50
    describe('instance', function () {
1✔
51
        var toolbar;
52
        beforeEach(function (done) {
1✔
53
            fixture.load('toolbar.html');
4✔
54

55
            $(function () {
4✔
56
                spyOn(CMS.Toolbar.prototype, '_initialStates');
4✔
57
                toolbar = new CMS.Toolbar();
4✔
58
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
4✔
59
                    return $.extend(true, CMS.settings, input);
×
60
                });
61
                done();
4✔
62
            });
63
        });
64

65
        afterEach(function () {
1✔
66
            fixture.cleanup();
4✔
67
        });
68

69
        it('has ui', function () {
1✔
70
            expect(toolbar.ui).toEqual(jasmine.any(Object));
1✔
71
            expect(Object.keys(toolbar.ui)).toContain('container');
1✔
72
            expect(Object.keys(toolbar.ui)).toContain('body');
1✔
73
            expect(Object.keys(toolbar.ui)).toContain('window');
1✔
74
            expect(Object.keys(toolbar.ui)).toContain('document');
1✔
75
            expect(Object.keys(toolbar.ui)).toContain('toolbar');
1✔
76
            expect(Object.keys(toolbar.ui)).toContain('navigations');
1✔
77
            expect(Object.keys(toolbar.ui)).toContain('buttons');
1✔
78
            expect(Object.keys(toolbar.ui)).toContain('messages');
1✔
79
            expect(Object.keys(toolbar.ui)).toContain('structureBoard');
1✔
80
            expect(Object.keys(toolbar.ui)).toContain('revert');
1✔
81
            expect(Object.keys(toolbar.ui).length).toEqual(11);
1✔
82
        });
83

84
        it('has options', function () {
1✔
85
            expect(toolbar.options).toEqual({
1✔
86
                toolbarDuration: 200
87
            });
88

89
            var toolbar2 = new CMS.Toolbar({ toolbarDuration: 250, nonExistent: true });
1✔
90
            expect(toolbar2.options).toEqual({
1✔
91
                toolbarDuration: 250,
92
                nonExistent: true
93
            });
94
        });
95

96
        // this spec can be thoroughly expanded, but for the moment just checking for the
97
        // class and event is sufficient
98
        it('initializes the states', function (done) {
1✔
99
            CMS.Toolbar.prototype._initialStates.and.callThrough();
1✔
100
            CMS.Toolbar.prototype._initialStates.calls.reset();
1✔
101
            CMS.settings = { sideframe_enabled: true, sideframe: {}, version: 'fake' };
1✔
102
            CMS.config = { settings: { version: 'fake' }, auth: true };
1✔
103
            toolbar.ui.document.on('cms-ready', function () {
1✔
104
                // expect this to happen
105
                done();
1✔
106
            });
107
            toolbar = new CMS.Toolbar();
1✔
108
            expect(CMS.Toolbar.prototype._initialStates).toHaveBeenCalled();
1✔
109
            expect(toolbar.ui.body).toHaveClass('cms-ready');
1✔
110
        });
111

112
        it('sets the "ready" data on the toolbar ui', function () {
1✔
113
            expect(toolbar.ui.toolbar.data('ready')).toEqual(true);
1✔
114

115
            toolbar.ui.toolbar.data('ready', false);
1✔
116
            new CMS.Toolbar();
1✔
117
            expect(toolbar.ui.toolbar.data('ready')).toEqual(true);
1✔
118
        });
119
    });
120

121
    describe('.openAjax()', function () {
1✔
122
        var toolbar;
123
        beforeEach(function (done) {
1✔
124
            fixture.load('toolbar.html');
12✔
125
            CMS.config = {};
12✔
126
            CMS.settings = $.extend(CMS.settings, {
12✔
127
                toolbar: 'expanded'
128
            });
129
            jasmine.Ajax.install();
12✔
130
            $(function () {
12✔
131
                spyOn(CMS.Toolbar.prototype, '_initialStates');
12✔
132
                toolbar = new CMS.Toolbar();
12✔
133
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
12✔
134
                    return $.extend(true, CMS.settings, input);
×
135
                });
136
                done();
12✔
137
            });
138
        });
139

140
        afterEach(function () {
1✔
141
            jasmine.Ajax.uninstall();
12✔
142
            fixture.cleanup();
12✔
143
        });
144

145
        it('makes the request', function () {
1✔
146
            expect(toolbar.openAjax({ url: '/url' })).toEqual(jasmine.any(Object));
1✔
147
            var request = jasmine.Ajax.requests.mostRecent();
1✔
148
            expect(request.url).toEqual('/url');
1✔
149
        });
150

151
        it('does not make the request if there is a confirmation that is not succeeded', function () {
1✔
152
            spyOn(CMS.API.Helpers, 'secureConfirm').and.callFake(function (question) {
1✔
153
                expect(question).toEqual('Are you sure?');
1✔
154
                return false;
1✔
155
            });
156
            expect(toolbar.openAjax({ url: '/url', text: 'Are you sure?' })).toEqual(false);
1✔
157
        });
158

159
        it('shows the loader before making the request', function () {
1✔
160
            expect(toolbar.openAjax({ url: '/url' })).toEqual(jasmine.any(Object));
1✔
161
            expect(showLoader).toHaveBeenCalled();
1✔
162
        });
163

164
        it('uses custom callback after request succeeds and hides the loader', function () {
1✔
165
            spyOn($, 'ajax').and.callFake(function () {
1✔
166
                return {
1✔
167
                    done: function (callback) {
168
                        callback('response');
1✔
169
                        return { fail: $.noop };
1✔
170
                    }
171
                };
172
            });
173

174
            var callback = jasmine.createSpy();
1✔
175

176
            toolbar.openAjax({
1✔
177
                url: '/url',
178
                callback: callback
179
            });
180

181
            expect(callback).toHaveBeenCalled();
1✔
182
            expect(callback).toHaveBeenCalledWith(toolbar, 'response');
1✔
183
            expect(hideLoader).toHaveBeenCalled();
1✔
184
        });
185

186
        it('does not hide the loader if no callback provided', function () {
1✔
187
            spyOn($, 'ajax').and.callFake(function () {
1✔
188
                return {
1✔
189
                    done: function (callback) {
190
                        callback('response');
1✔
191
                        return { fail: $.noop };
1✔
192
                    }
193
                };
194
            });
195

196
            spyOn(CMS.API.Helpers, 'reloadBrowser');
1✔
197

198
            toolbar.openAjax({
1✔
199
                url: '/url',
200
                onSuccess: '/another-url'
201
            });
202

203
            expect(showLoader).toHaveBeenCalled();
1✔
204
            expect(hideLoader).not.toHaveBeenCalled();
1✔
205
        });
206

207
        it('uses custom onSuccess url from request success', function () {
1✔
208
            spyOn($, 'ajax').and.callFake(function () {
1✔
209
                return {
1✔
210
                    done: function (callback) {
211
                        callback({ url: '/redirect-url' });
1✔
212
                        return { fail: $.noop };
1✔
213
                    }
214
                };
215
            });
216

217
            spyOn(CMS.API.Helpers, 'reloadBrowser');
1✔
218

219
            toolbar.openAjax({
1✔
220
                url: '/url',
221
                onSuccess: 'FOLLOW_REDIRECT'
222
            });
223

224
            expect(CMS.API.Helpers.reloadBrowser).toHaveBeenCalledWith('/redirect-url');
1✔
225
        });
226

227
        it('uses custom onSuccess url after request succeeds', function () {
1✔
228
            spyOn($, 'ajax').and.callFake(function () {
1✔
229
                return {
1✔
230
                    done: function (callback) {
231
                        callback('response');
1✔
232
                        return { fail: $.noop };
1✔
233
                    }
234
                };
235
            });
236

237
            spyOn(CMS.API.Helpers, 'reloadBrowser');
1✔
238

239
            toolbar.openAjax({
1✔
240
                url: '/url',
241
                onSuccess: '/another-url'
242
            });
243

244
            expect(CMS.API.Helpers.reloadBrowser).toHaveBeenCalledWith('/another-url');
1✔
245
        });
246

247
        it('reloads the page if no callback or onSuccess passed', function () {
1✔
248
            spyOn($, 'ajax').and.callFake(function () {
1✔
249
                return {
1✔
250
                    done: function (callback) {
251
                        callback('response');
1✔
252
                        return { fail: $.noop };
1✔
253
                    }
254
                };
255
            });
256

257
            spyOn(CMS.API.Helpers, 'reloadBrowser');
1✔
258

259
            toolbar.openAjax({
1✔
260
                url: '/url'
261
            });
262

263
            expect(CMS.API.Helpers.reloadBrowser).toHaveBeenCalledWith();
1✔
264
        });
265

266
        it('handles parameters', function () {
1✔
267
            spyOn($, 'ajax').and.callFake(function (param) {
1✔
268
                expect(param.data).toEqual({ param1: true, param2: false, param3: 150, param4: 'alala' });
1✔
269
                return {
1✔
270
                    done: function () {
271
                        return { fail: $.noop };
1✔
272
                    }
273
                };
274
            });
275

276
            toolbar.openAjax({
1✔
277
                url: '/whatever',
278
                post: JSON.stringify({ param1: true, param2: false, param3: 150, param4: 'alala' })
279
            });
280
        });
281

282
        it('opens an error message if request failed', function () {
1✔
283
            CMS.API.Messages = new CMS.Messages();
1✔
284
            spyOn(CMS.API.Messages, 'open');
1✔
285

286
            spyOn($, 'ajax').and.callFake(function () {
1✔
287
                return {
1✔
288
                    done: function () {
289
                        return {
1✔
290
                            fail: function (callback) {
291
                                callback({
1✔
292
                                    responseText: 'An error occurred',
293
                                    status: 418,
294
                                    statusText: "I'm a teapot"
295
                                });
296
                            }
297
                        };
298
                    }
299
                };
300
            });
301

302
            toolbar.openAjax({
1✔
303
                url: '/whatever'
304
            });
305

306
            expect(CMS.API.Messages.open).toHaveBeenCalledWith({
1✔
307
                message: "An error occurred | 418 I'm a teapot",
308
                error: true
309
            });
310
        });
311

312
        it('unlocks the toolbar if request succeeds', function () {
1✔
313
            spyOn($, 'ajax').and.callFake(function () {
1✔
314
                return {
3✔
315
                    done: function (callback) {
316
                        callback('response');
3✔
317
                        return { fail: $.noop };
3✔
318
                    }
319
                };
320
            });
321

322
            spyOn(CMS.API.Helpers, 'reloadBrowser');
1✔
323
            CMS.API.locked = true;
1✔
324

325
            toolbar.openAjax({
1✔
326
                url: '/url'
327
            });
328

329
            expect(CMS.API.locked).toEqual(false);
1✔
330

331
            CMS.API.locked = true;
1✔
332

333
            toolbar.openAjax({
1✔
334
                url: '/url',
335
                callback: $.noop
336
            });
337

338
            expect(CMS.API.locked).toEqual(false);
1✔
339

340
            CMS.API.locked = true;
1✔
341

342
            toolbar.openAjax({
1✔
343
                url: '/url',
344
                onSuccess: '/another-url'
345
            });
346

347
            expect(CMS.API.locked).toEqual(false);
1✔
348
        });
349

350
        it('unlocks the toolbar if request fails', function () {
1✔
351
            CMS.API.Messages = new CMS.Messages();
1✔
352
            spyOn(CMS.API.Messages, 'open');
1✔
353

354
            spyOn($, 'ajax').and.callFake(function () {
1✔
355
                return {
1✔
356
                    done: function () {
357
                        return {
1✔
358
                            fail: function (callback) {
359
                                callback({
1✔
360
                                    responseText: 'An error occurred',
361
                                    status: 418,
362
                                    statusText: "I'm a teapot"
363
                                });
364
                            }
365
                        };
366
                    }
367
                };
368
            });
369

370
            CMS.API.locked = true;
1✔
371

372
            toolbar.openAjax({
1✔
373
                url: '/whatever'
374
            });
375

376
            expect(CMS.API.locked).toEqual(false);
1✔
377
        });
378
    });
379

380
    describe('._events()', function () {
1✔
381
        var toolbar;
382
        beforeEach(function (done) {
1✔
383
            fixture.load('toolbar.html');
9✔
384
            CMS.config = {
9✔
385
                lang: {
386
                    publish: 'publish?'
387
                }
388
            };
389
            CMS.settings = $.extend(CMS.settings, {
9✔
390
                toolbar: 'expanded'
391
            });
392
            $(function () {
9✔
393
                spyOn(CMS.Toolbar.prototype, '_initialStates');
9✔
394
                toolbar = new CMS.Toolbar();
9✔
395
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
9✔
396
                    return $.extend(true, CMS.settings, input);
×
397
                });
398
                done();
9✔
399
            });
400
        });
401

402
        afterEach(function () {
1✔
403
            fixture.cleanup();
9✔
404
        });
405

406
        it('attaches event handlers to navigation menu links', function () {
1✔
407
            spyOn(toolbar, '_delegate');
1✔
408

409
            var emptyLink = $(toolbar.ui.navigations[0]).find('a').eq(0);
1✔
410
            var pagesLink = $(toolbar.ui.navigations[0]).find('a').eq(1);
1✔
411

412
            emptyLink.trigger('click');
1✔
413
            expect(toolbar._delegate).not.toHaveBeenCalled();
1✔
414

415
            pagesLink.trigger('click');
1✔
416

417
            expect(toolbar._delegate).toHaveBeenCalledTimes(1);
1✔
418
            expect(toolbar._delegate).toHaveBeenCalledWith(pagesLink);
1✔
419
        });
420

421
        it('attaches event handlers to navigation menu links (to open in new window)', function () {
1✔
422
            var fakeWindow = {
1✔
423
                open: jasmine.createSpy()
424
            };
425
            spyOn(toolbar, '_delegate');
1✔
426
            spyOn(CMS.API.Helpers, '_getWindow').and.callFake(function () {
1✔
427
                return fakeWindow;
1✔
428
            });
429

430
            var emptyLink = $(toolbar.ui.navigations[0]).find('a').eq(0);
1✔
431
            var pagesLink = $(toolbar.ui.navigations[0]).find('a').eq(1);
1✔
432

433
            emptyLink.trigger(new $.Event('keydown', { keyCode: CMS.KEYS.CTRL }));
1✔
434
            emptyLink.trigger('click');
1✔
435
            expect(toolbar._delegate).not.toHaveBeenCalled();
1✔
436
            expect(fakeWindow.open).not.toHaveBeenCalled();
1✔
437

438
            emptyLink.trigger(new $.Event('keydown', { keyCode: CMS.KEYS.CTRL }));
1✔
439
            pagesLink.trigger('click');
1✔
440
            expect(toolbar._delegate).not.toHaveBeenCalledTimes(1);
1✔
441
            expect(fakeWindow.open).toHaveBeenCalledWith(jasmine.stringMatching('cms/page'), '_blank');
1✔
442

443
            pagesLink.trigger('keyup');
1✔
444
        });
445

446
        it('attaches event handlers to navigation menu lists', function () {
1✔
447
            var firstMenuItem = $(toolbar.ui.navigations.find('> li')[0]);
1✔
448
            toolbar.ui.structureBoard = $('<div></div>');
1✔
449

450
            expect(toolbar.ui.document).not.toHandle(toolbar.click);
1✔
451
            expect(toolbar.ui.structureBoard).not.toHandle(toolbar.click);
1✔
452
            expect(toolbar.ui.toolbar).not.toHandle(toolbar.click);
1✔
453
            expect(toolbar.ui.window).not.toHandle(toolbar.resize + '.menu.reset');
1✔
454
            firstMenuItem.trigger('click');
1✔
455
            expect(toolbar.ui.document).toHandle(toolbar.click);
1✔
456
            expect(toolbar.ui.structureBoard).toHandle(toolbar.click);
1✔
457
            expect(toolbar.ui.toolbar).toHandle(toolbar.click);
1✔
458
            expect(toolbar.ui.window).toHandle(toolbar.resize + '.menu.reset');
1✔
459
        });
460

461

462
        it('handles mousemove over top level toolbar items', function () {
1✔
463
            var firstMenuItem = $(toolbar.ui.navigations.find('> li')[0]);
1✔
464
            var secondMenuItem = $(toolbar.ui.navigations.find('> li')[1]);
1✔
465
            var thirdMenuItem = $(toolbar.ui.navigations.find('> li')[2]);
1✔
466

467
            firstMenuItem.trigger('click');
1✔
468

469
            var clickFirstMenuItem = spyOnEvent(firstMenuItem, toolbar.click);
1✔
470
            var clickSecondMenuItem = spyOnEvent(secondMenuItem, toolbar.click);
1✔
471
            var clickThirdMenuItem = spyOnEvent(thirdMenuItem, toolbar.click);
1✔
472

473
            firstMenuItem.trigger('mouseenter');
1✔
474
            expect(clickFirstMenuItem).not.toHaveBeenTriggered();
1✔
475

476
            secondMenuItem.trigger('mouseenter');
1✔
477
            expect(clickSecondMenuItem).toHaveBeenTriggered();
1✔
478

479
            thirdMenuItem.trigger('mouseenter');
1✔
480
            expect(clickThirdMenuItem).toHaveBeenTriggered();
1✔
481
        });
482

483
        it('does not care for mouse over top level items if touch is enabled', function () {
1✔
484
            var firstMenuItem = $(toolbar.ui.navigations.find('> li')[0]);
1✔
485
            var secondMenuItem = $(toolbar.ui.navigations.find('> li')[1]);
1✔
486

487
            firstMenuItem.find('a').trigger('touchstart');
1✔
488
            firstMenuItem.trigger('click');
1✔
489

490
            var clickFirstMenuItem = spyOnEvent(firstMenuItem, toolbar.click);
1✔
491
            var clickSecondMenuItem = spyOnEvent(secondMenuItem, toolbar.click);
1✔
492

493
            firstMenuItem.trigger('mouseenter');
1✔
494
            expect(clickFirstMenuItem).not.toHaveBeenTriggered();
1✔
495
            secondMenuItem.trigger('mouseenter');
1✔
496
            expect(clickSecondMenuItem).not.toHaveBeenTriggered();
1✔
497
        });
498

499
        it('closes the menu item if it is open', function () {
1✔
500
            var firstMenuItem = $(toolbar.ui.navigations.find('> li')[0]);
1✔
501

502
            firstMenuItem.trigger('click');
1✔
503
            expect(firstMenuItem).toHaveClass('cms-toolbar-item-navigation-hover');
1✔
504

505
            firstMenuItem.trigger('click');
1✔
506
            expect(firstMenuItem).not.toHaveClass('cms-toolbar-item-navigation-hover');
1✔
507
        });
508

509
        it('handles mousemove over nested toolbar items', function () {
1✔
510
            var menuItem = $(toolbar.ui.navigations.find('> li')[1]);
1✔
511
            var subMenu = menuItem.find('> ul');
1✔
512
            var childrenSubMenuItem = subMenu.find('> li').eq(0);
1✔
513

514
            spyOn($.fn, 'show').and.callThrough();
1✔
515

516
            expect(childrenSubMenuItem.find('> ul')).not.toBeVisible();
1✔
517
            childrenSubMenuItem.trigger('pointerover');
1✔
518
            expect($.fn.show).toHaveBeenCalledTimes(1);
1✔
519
            expect($.fn.show.calls.mostRecent().object).toEqual(childrenSubMenuItem.find('> ul'));
1✔
520
        });
521

522
        it('attaches event handlers to buttons with data-rel', function () {
1✔
523
            spyOn(toolbar, '_delegate');
1✔
524
            var createButton = toolbar.ui.buttons.eq(0).find('a');
1✔
525
            createButton.trigger(toolbar.click);
1✔
526
            expect(toolbar._delegate).toHaveBeenCalled();
1✔
527
        });
528

529
        it('attaches event handlers to buttons without data-rel', function () {
1✔
530
            var createButton = toolbar.ui.buttons.eq(1).find('a:first');
1✔
531
            spyOn($.Event.prototype, 'stopPropagation');
1✔
532
            createButton.trigger(toolbar.click);
1✔
533
            expect($.Event.prototype.stopPropagation).toHaveBeenCalled();
1✔
534
        });
535

536
    });
537

538
    describe('._screenBlock()', function () {
1✔
539
        var toolbar;
540
        beforeEach(function (done) {
1✔
541
            fixture.load('toolbar.html');
×
542
            CMS.config = {};
×
543
            CMS.settings = $.extend(CMS.settings, {
×
544
                toolbar: 'expanded'
545
            });
546
            $(function () {
×
547
                spyOn(CMS.Toolbar.prototype, '_initialStates');
×
548
                toolbar = new CMS.Toolbar();
×
549
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
×
550
                    return $.extend(true, CMS.settings, input);
×
551
                });
552
                toolbar.ui.window = $('<div></div>');
×
553
                toolbar.ui.screenBlock = $('<div></div>');
×
554
                spyOn($.fn, 'css').and.callThrough();
×
555
                done();
×
556
            });
557
        });
558

559
        afterEach(function () {
1✔
560
            var timeoutId = setInterval(function () {
×
561
            }, 10);
562

563
            for (var i; i <= timeoutId; i++) {
×
564
                clearInterval(i);
×
565
            }
566
            fixture.cleanup();
×
567
        });
568
    });
569

570
    describe('._delegate()', function () {
1✔
571
        var toolbar;
572
        var fakeWindow;
573
        class FakeModal {
574
            constructor() {}
575
        }
576
        class FakeSideframe {
577
            constructor(opts) {
578
                this.constructorOpts = opts;
1✔
579
            }
580
            open() {}
581
        }
582
        beforeEach(function (done) {
1✔
583
            fixture.load('toolbar.html');
7✔
584
            CMS.config = {};
7✔
585
            CMS.settings = $.extend(CMS.settings, {
7✔
586
                toolbar: 'expanded'
587
            });
588
            fakeWindow = {
7✔
589
                location: {
590
                    href: '',
591
                    pathname: '/context.html',
592
                    search: ''
593
                }
594
            };
595
            $(function () {
7✔
596
                spyOn(CMS.Toolbar.prototype, '_initialStates');
7✔
597
                toolbar = new CMS.Toolbar();
7✔
598
                Toolbar.__Rewire__('Modal', FakeModal);
7✔
599
                Toolbar.__Rewire__('Sideframe', FakeSideframe);
7✔
600
                spyOn(toolbar, 'openAjax');
7✔
601
                spyOn(CMS.API.Helpers, '_getWindow').and.returnValue(fakeWindow);
7✔
602
                spyOn(CMS.API.Messages, 'open');
7✔
603
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
7✔
UNCOV
604
                    return $.extend(true, CMS.settings, input);
×
605
                });
606
                done();
7✔
607
            });
608
        });
609

610
        afterEach(function (done) {
1✔
611
            Toolbar.__ResetDependency__('Modal');
7✔
612
            Toolbar.__ResetDependency__('Sideframe');
7✔
613
            fixture.cleanup();
7✔
614
            setTimeout(function () {
7✔
615
                done();
7✔
616
            }, 200);
617
        });
618

619
        it('return false if item is disabled', function () {
1✔
620
            expect(toolbar._delegate($('<div class="cms-btn-disabled"></div>'))).toEqual(false);
1✔
621
        });
622
        it('opens modal if item is "modal"', function () {
1✔
623
            var modalOpen = jasmine.createSpy();
1✔
624

625
            FakeModal.prototype.open = modalOpen;
1✔
626

627
            toolbar._delegate($('<div href="href" data-name="modal" data-rel="modal" data-on-close="test"></div>'));
1✔
628

629
            expect(modalOpen).toHaveBeenCalledWith({
1✔
630
                url: 'href?cms_path=%2Fcontext.html',
631
                title: 'modal'
632
            });
633
        });
634

635
        it('opens sideframe if item is "sideframe"', function () {
1✔
636
            var sideframeOpen = jasmine.createSpy();
1✔
637
            spyOn(FakeSideframe.prototype, 'open').and.callFake(sideframeOpen);
1✔
638

639
            toolbar._delegate(
1✔
640
                $('<div href="href2" data-name="modal" data-rel="sideframe" data-on-close="test2"></div>')
641
            );
642

643
            // Check that a Sideframe was constructed with onClose option
644
            // (we can't easily check this with the spy, but we can verify open was called)
645
            expect(sideframeOpen).toHaveBeenCalledWith({
1✔
646
                url: 'href2',
647
                animate: true
648
            });
649
        });
650

651
        it('sideframe disabled defaults to redirecting to the url', function () {
1✔
652

653
            // Set the switch to disable the sideframe
654
            CMS.settings = $.extend(true, CMS.settings, {
1✔
655
                sideframe_enabled: false
656
            });
657
            expect(fakeWindow.location.href).toEqual('');
1✔
658
            toolbar._delegate(
1✔
659
                $('<div href="disabled-sideframe-href" data-rel="sideframe" ></div>')
660
            );
661
            expect(fakeWindow.location.href).toEqual('disabled-sideframe-href');
1✔
662

663
            // Reset the switch to enable the sideframe for other tests
664
            CMS.settings = $.extend(true, CMS.settings, {
1✔
665
                sideframe_enabled: true
666
            });
667
        });
668

669
        it('opens message if item is "message"', function () {
1✔
670
            toolbar._delegate($('<div data-rel="message" data-text="message!"></div>'));
1✔
671

672
            expect(CMS.API.Messages.open).toHaveBeenCalledWith({
1✔
673
                message: 'message!'
674
            });
675
        });
676

677
        it('opens ajax request if item is "ajax"', function () {
1✔
678
            toolbar._delegate(
1✔
679
                $('<div href="href" data-rel="ajax" ' +
680
                  'data-post=\'{ "test": "shmest" }\' data-text="text" data-on-success="REFRESH"></div>')
681
            );
682

683
            expect(toolbar.openAjax).toHaveBeenCalledWith({
1✔
684
                url: 'href',
685
                post: '{"test":"shmest"}',
686
                text: 'text',
687
                method: undefined,
688
                onSuccess: 'REFRESH'
689
            });
690
        });
691

692
        it('just redirects to the page if nothing else', function () {
1✔
693
            expect(fakeWindow.location.href).toEqual('');
1✔
694
            toolbar._delegate(
1✔
695
                $('<div href="href"></div>')
696
            );
697
            expect(fakeWindow.location.href).toEqual('href');
1✔
698
        });
699
    });
700

701
    describe('._debug()', function () {
1✔
702
        var toolbar;
703
        beforeEach(function (done) {
1✔
704
            fixture.load('toolbar.html');
2✔
705
            CMS.config = {
2✔
706
                lang: {
707
                    debug: 'DEBUG!'
708
                }
709
            };
710
            CMS.settings = $.extend(CMS.settings, {
2✔
711
                toolbar: 'expanded'
712
            });
713
            $(function () {
2✔
714
                spyOn(CMS.Toolbar.prototype, '_initialStates');
2✔
715
                toolbar = new CMS.Toolbar();
2✔
716
                toolbar.ui.container.append('<div class="cms-debug-bar"></div>');
2✔
717
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
2✔
UNCOV
718
                    return $.extend(true, CMS.settings, input);
×
719
                });
720

721
                spyOn(CMS.API.Messages, 'open');
2✔
722
                jasmine.clock().install();
2✔
723
                done();
2✔
724
            });
725
        });
726

727
        afterEach(function (done) {
1✔
728
            jasmine.clock().uninstall();
2✔
729
            fixture.cleanup();
2✔
730
            setTimeout(function () {
2✔
731
                done();
2✔
732
            }, 200);
733
        });
734

735
        it('shows debug information after timeout', function () {
1✔
736
            toolbar._debug();
1✔
737

738
            toolbar.ui.container.find('.cms-debug-bar').trigger(toolbar.mouseEnter);
1✔
739
            jasmine.clock().tick(10000);
1✔
740

741
            expect(CMS.API.Messages.open).toHaveBeenCalledWith({
1✔
742
                message: 'DEBUG!'
743
            });
744
        });
745

746
        it('does not show debug information if not hovering after timeout', function () {
1✔
747
            toolbar._debug();
1✔
748

749
            toolbar.ui.container.find('.cms-debug-bar').trigger(toolbar.mouseEnter);
1✔
750
            jasmine.clock().tick(500);
1✔
751
            toolbar.ui.container.find('.cms-debug-bar').trigger(toolbar.mouseLeave);
1✔
752
            jasmine.clock().tick(10000);
1✔
753

754
            expect(CMS.API.Messages.open).not.toHaveBeenCalled();
1✔
755
        });
756
    });
757

758
    describe('_refreshMarkup', () => {
1✔
759
        const diffDOMConstructor = jasmine.createSpy();
1✔
760
        const newToolbar = $('<div></div>');
1✔
761
        let toolbar;
762

763
        class DiffDOM {
764
            constructor() {
UNCOV
765
                diffDOMConstructor();
×
766
            }
767
        }
768
        DiffDOM.prototype.diff = jasmine.createSpy();
1✔
769
        DiffDOM.prototype.apply = jasmine.createSpy();
1✔
770

771
        const trigger = jasmine.createSpy();
1✔
772
        class FakeNavigation {
773
            constructor() {
774
                this.ui = {
2✔
775
                    window: {
776
                        trigger
777
                    }
778
                };
779
            }
780
        }
781

782
        beforeEach(function (done) {
1✔
783
            fixture.load('toolbar.html');
1✔
784
            CMS.config = {
1✔
785
                lang: {
786
                    debug: 'DEBUG!'
787
                }
788
            };
789
            CMS.settings = $.extend(CMS.settings, {
1✔
790
                toolbar: 'expanded'
791
            });
792
            $(function () {
1✔
793
                Toolbar.__Rewire__('Navigation', FakeNavigation);
1✔
794
                Toolbar.__Rewire__('DiffDOM', DiffDOM);
1✔
795
                spyOn(CMS.Toolbar.prototype, '_initialStates');
1✔
796
                toolbar = new CMS.Toolbar();
1✔
797
                spyOn(toolbar, 'setSettings').and.callFake(function (input) {
1✔
UNCOV
798
                    return $.extend(true, CMS.settings, input);
×
799
                });
800
                spyOn(toolbar, '_setupUI');
1✔
801
                spyOn(toolbar, '_events');
1✔
802
                CMS.API.Clipboard = {
1✔
803
                    ui: {},
804
                    _toolbarEvents: jasmine.createSpy()
805
                };
806

807
                jasmine.clock().install();
1✔
808
                done();
1✔
809
            });
810
        });
811

812
        afterEach(function (done) {
1✔
813
            jasmine.clock().uninstall();
1✔
814
            Toolbar.__ResetDependency__('Navigation');
1✔
815
            Toolbar.__ResetDependency__('DiffDOM');
1✔
816
            fixture.cleanup();
1✔
817
            setTimeout(function () {
1✔
818
                done();
1✔
819
            }, 200);
820
        });
821

822
        it('refreshes markup', () => {
1✔
823
            toolbar.navigation = null;
1✔
824
            toolbar._refreshMarkup(newToolbar);
1✔
825
            expect(toolbar._setupUI).toHaveBeenCalledTimes(1);
1✔
826
            expect(toolbar._events).toHaveBeenCalledTimes(1);
1✔
827
            expect(toolbar.navigation instanceof FakeNavigation).toEqual(true);
1✔
828
            expect(trigger).toHaveBeenCalledWith('resize');
1✔
829
            expect(CMS.API.Clipboard._toolbarEvents).toHaveBeenCalledTimes(1);
1✔
830
        });
831
    });
832
});
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