forked from outsideris/angular-summernote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-summernote.test.js
More file actions
660 lines (558 loc) · 20.4 KB
/
angular-summernote.test.js
File metadata and controls
660 lines (558 loc) · 20.4 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/**
* Copyright (c) 2013 JeongHoon Byun aka "Outsider", <http://blog.outsider.ne.kr/>
* Licensed under the MIT license.
* <http://outsider.mit-license.org/>
*/
describe('Summernote directive', function() {
'use strict';
var $rootScope, $compile, $timeout;
beforeEach(module('summernote'));
beforeEach(inject(function(_$compile_, _$rootScope_, _$timeout_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
}));
describe('initialization', function() {
it('has "summernote" class', function () {
var element = $compile('<summernote></summernote>')($rootScope);
$rootScope.$digest();
expect($(element.get(0)).hasClass('summernote')).to.be.true;
});
it('works with "summernote" element', function () {
var element = $compile('<summernote></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().hasClass('note-editor')).to.be.true;
});
it('works with "summernote" attribute', function () {
var element = $compile('<div summernote></div>')($rootScope);
$rootScope.$digest();
expect(element.next().hasClass('note-editor')).to.be.true;
});
it('works with multiple "summernote" elements', function () {
var element = $compile('<summernote></summernote><br><summernote></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next('.note-editor')).to.length(2);
});
});
describe('"height" option', function() {
it('should be 0 unless it specified', function () {
var element = $compile('<summernote></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-editable').outerHeight()).to.be.equal(0);
});
it('should be 400 if it specified', function () {
var element = $compile('<summernote height="400"></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-editable').outerHeight()).to.be.equal(400);
});
it('should set with multiple directives', function () {
var element = $compile('<summernote height="200"></summernote><br><summernote height="400"></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-editable').eq(0).outerHeight()).to.be.equal(200);
expect(element.next().find('.note-editable').eq(1).outerHeight()).to.be.equal(400);
});
});
describe('"min-height" option', function() {
it('should be 300 if it specified', function () {
var element = $compile('<summernote min-height="300"></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-editable').css('min-height')).to.be.equal('300px');
});
});
describe('"max-height" option', function() {
it('should be 500 if it specified', function () {
var element = $compile('<summernote max-height="500"></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-editable').css('max-height')).to.be.equal('500px');
});
});
describe('"placeholder" option', function() {
it('should placeholder', function () {
var element = $compile('<summernote placeholder="This is a placeholder"></summernote>')($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-placeholder')).to.length(1);
expect(element.next().find('.note-placeholder').text()).to.be.equal('This is a placeholder');
});
});
describe('"focus" option', function() {
it('should be focused if it specified', function () {
var el = $('<summernote focus height="400"></summernote>').appendTo(document.body);
var element = $compile(el)($rootScope);
$rootScope.$digest();
expect(element.next().find('.note-editable').get(0)).to.be.equal(document.activeElement);
el.next().remove();
el.remove();
});
});
describe('"airmode" option', function() {
it('should be on', function () {
var element = $compile('<summernote airMode></summernote>')($rootScope);
$rootScope.$digest();
expect(element.data('summernote').options.airMode).to.be.true;
});
it('should be on using config', function () {
var scope = $rootScope.$new();
scope.summernoteConfig = {airMode: true};
var element = $compile('<summernote config="summernoteConfig"></summernote>')(scope);
$rootScope.$digest();
expect(element.data('summernote').options.airMode).to.be.true;
element.next().remove();
element.remove();
});
});
describe('summernoteConfig', function() {
var scope;
beforeEach(function() {
scope = $rootScope.$new();
scope.summernoteConfig = {
height: 300,
minHeight: 200,
maxHeight: 500,
focus: true,
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear']],
['fontsize', ['fontsize']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['height', ['height']]
]
};
});
it('"height" should be 300', function() {
var element = $compile('<summernote config="summernoteConfig"></summernote>')(scope);
$rootScope.$digest();
expect(element.next().find('.note-editable').outerHeight()).to.be.equal(300);
});
it('"minHeight" should be 300', function() {
var element = $compile('<summernote config="summernoteConfig"></summernote>')(scope);
$rootScope.$digest();
expect(element.next().find('.note-editable').css('min-height')).to.be.equal('200px');
});
it('"maxHeight" should be 300', function() {
var element = $compile('<summernote config="summernoteConfig"></summernote>')(scope);
$rootScope.$digest();
expect(element.next().find('.note-editable').css('max-height')).to.be.equal('500px');
});
it('toolbar should be customized', function() {
var element = $compile('<summernote config="summernoteConfig"></summernote>')(scope);
$rootScope.$digest();
expect(element.next().find('.note-toolbar > .note-fontsize')).to.length(1);
expect(element.next().find('.note-toolbar > .note-help')).to.length(0);
});
it('"lang" needs the lang file', function() {
var fn = $compile('<summernote lang="ko-KR"></summernote>');
try {
fn($rootScope);
} catch(e) {
expect(e.message).to.be.exist;
}
});
it('"lang" load the lang file correctly', function() {
var element = $compile('<summernote lang="de-DE"></summernote>')(scope);
$rootScope.$digest();
expect(element.next().find('.note-toolbar > .note-view .btn-fullscreen').attr('data-original-title'))
.to.be.equal('Vollbild');
});
});
describe('destroy', function() {
var scope;
beforeEach(function() {
scope = $rootScope.$new();
});
it('shoud be destroyed when scope is destroyed.', function() {
// given
var element = $compile('<summernote></summernote>')(scope);
scope.$digest();
expect(element.next().hasClass('note-editor')).to.be.true;
// when
scope.$destroy();
// then
expect(element.next().hasClass('note-editor')).to.be.false;
});
it('should clean up summernnote', function () {
// given
scope.summernoteConfig = {height: 300};
scope.test = [];
var element = $compile('<div ng-repeat="t in test"><summernote ng-model="t.c" config="summernoteConfig"></summernote></div>')(scope);
scope.$digest();
scope.test.push({c: ''});
scope.$digest();
expect($(element.next().children().get(0)).hasClass('summernote')).to.be.true;
// when
scope.test.pop();
scope.$digest();
// then
expect($(element.next().children().get(0)).hasClass('summernote')).to.be.false;
});
});
describe('ngModel', function() {
var scope;
beforeEach(function() {
scope = $rootScope.$new();
scope.summernoteConfig = {focus: true};
});
it('text should be synchronized when value are changed in outer scope', function() {
// given
var oldText = 'Hello World!', newText = 'new text';
scope.text = oldText;
var element = $compile('<summernote ng-model="text"></summernote>')(scope);
scope.$digest();
expect(element.summernote('code')).to.be.equal(oldText);
// when
scope.text = newText;
scope.$digest();
// then
expect(element.summernote('code')).to.be.equal(newText);
});
it('text should be synchronized when value are changed in summernote', function() {
var oldText = 'Hello World!', newText = 'new text';
// given
scope.text = oldText;
var el = $('<summernote ng-Model="text"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
expect(element.summernote('code')).to.be.equal(oldText);
// when
element.summernote('code', newText);
$(element.next().find('.note-editable').eq(0)).trigger('input'); // to trigger onChange
scope.$digest();
// then
$timeout.flush();
expect(scope.text).to.be.equal(newText);
el.next().remove();
el.remove();
});
it('element text should be blank in outer scope before digesting', function() {
// given
var blankText = '', helloText = 'Hello World';
var element = $compile('<summernote ng-model="text"></summernote>')(scope);
scope.text = helloText;
expect(element.html()).to.be.equal(blankText);
// when
scope.$digest();
// then
expect(element.html()).to.be.equal(helloText);
});
it('text should be synchronized when text is changed using toolbar', function() {
var selectText = function(element){
var doc = document,
range;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};
var oldText = 'Hello World!';
// given
scope.text = oldText;
var el = $('<summernote ng-Model="text"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
expect(element.summernote('code')).to.be.equal(oldText);
// when
selectText($(element.next().find('.note-editable'))[0]);
$(element.next().find('.note-font').find('button').eq(0)).click();
scope.$digest();
// then
$timeout.flush();
expect(scope.text).to.be.equal(element.summernote('code'));
el.next().remove();
el.remove();
});
it('text chould be synchronized when text is changed in codeview mode', function() {
var oldText = 'Hello World!', newText = 'new text';
// given
scope.text = oldText;
var el = $('<summernote ng-Model="text"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
expect(element.summernote('code')).to.be.equal(oldText);
// when
element.next().find('.note-view').find('button.btn-codeview').click();
scope.text = newText;
scope.$digest();
// then
expect(element.summernote('code')).to.be.equal(newText);
el.next().remove();
el.remove();
});
it('text should be synchronized in use codeview when text is changed in outer scope', function() {
var oldText = 'Hello World!', newText = 'new text';
// given
scope.text = oldText;
var el = $('<summernote ng-Model="text"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
expect(element.summernote('code')).to.be.equal(oldText);
// when
element.next().find('.note-view').find('button.btn-codeview').click();
element.next().find('.note-codable').val(newText);
$(element.next().find('.note-codable').eq(0)).trigger('keyup');
scope.$digest();
// then
$timeout.flush();
expect(scope.text).to.be.equal(newText);
el.next().remove();
el.remove();
});
it('should be synchronized when image inserted', function(done) {
// given
var text = 'Hello World';
scope.text = text;
var el = $('<summernote ng-Model="text"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
var preventBubbling = function(e) { e.stopPropagation(); };
$('.note-toolbar').on('click', preventBubbling);
$(element.next().find('.note-insert').find('button[data-original-title=Picture]')).click(); // image
var imageModal$ = element.next().find('.modal.in .modal-dialog');
expect(imageModal$).to.length(1);
var imgUrl = 'https://www.gravatar.com/avatar/748a6dc8b4eaba0fde62909e39be7987?s=200';
imageModal$.find('.note-image-url').val(imgUrl);
imageModal$.find('.note-image-url').trigger('keyup');
imageModal$.find('.note-image-btn').click();
// then
var timer = setInterval(function() {
if (element.summernote('code') !== text) {
expect(element.summernote('code')).to.match(/gravatar/);
// tear down
$('.note-toolbar').off('click', preventBubbling);
el.next().remove();
el.remove();
clearInterval(timer);
done();
}
}, 200);
});
it('should empty summernote when model is empty', function() {
// given
scope.text = 'Hello World';
var element = $compile($('<summernote ng-Model="text"></summernote>').appendTo(document.body))(scope);
scope.$digest();
// when
scope.text = '';
scope.$digest();
// then
expect(element.summernote('code')).to.not.equal('');
expect(element.summernote('isEmpty')).to.equal(true);
});
});
describe('callbacks', function() {
var scope;
beforeEach(function() {
scope = $rootScope.$new();
scope.summernoteConfig = {focus: false};
});
it('onInit should be invoked', function(done) {
scope.init = function() {
expect(true).to.be.true;
done();
};
$compile('<summernote on-init="init()"></summernote>')(scope);
scope.$digest();
});
it('onEnter should be invoked', function(done) {
scope.enter = function() {
// then
expect(true).to.be.true;
done();
};
// given
var el = $('<summernote on-enter="enter()"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
var e= jQuery.Event('keydown');
e.keyCode = 13; // Enter key
element.next().find('.note-editable').trigger(e);
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
it('onFocus should be invoked', function(done) {
scope.focus = function(e) {
// then
expect(e).to.be.exist;
done();
};
// given
var el = $('<summernote on-focus="focus(evt)"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
// TODO: check the reason why it need triggering focus twice
element.next().find('.note-editable').focus().focus();
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
it('onBlur should be invoked', function(done) {
scope.blur = function(e) {
// then
expect(e).to.be.exist;
done();
};
// given
var el = $('<summernote on-blur="blur(evt)"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
element.next().find('.note-editable').blur();
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
it('onPaste should be invoked', function(done) {
scope.paste = function() {
// then
expect(true).to.be.true;
done();
};
// given
var el = $('<summernote on-paste="paste(evt)"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
var event = jQuery.Event('paste');
event.originalEvent = '';
element.next().find('.note-editable').trigger(event);
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
it('onKeyup should be invoked', function(done) {
scope.keyup = function(e) {
// then
expect(e).to.be.exist;
done();
};
// given
var el = $('<summernote on-keyup="keyup(evt)"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
element.next().find('.note-editable').keyup();
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
it('onKeydown should be invoked', function(done) {
scope.keydown = function(e) {
// then
expect(e).to.be.exist;
done();
};
// given
var el = $('<summernote on-keydown="keydown(evt)"></summernote>').appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
element.next().find('.note-editable').keydown();
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
it('onChange should be invoked', function(done) {
var selectText = function(element){
var doc = document,
range;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};
scope.change = function(contents) {
// then
// FIXME: summernote v0.7.0 has a bug, so `contents` is Event obejct not string of contents
//expect(/Hello World/.test(contents)).to.be.ok;
expect(true).to.be.ok;
done();
};
// given
var oldText = 'Hello World!';
scope.text = oldText;
var el = $('<summernote ng-Model="text" on-change="change(contents)"></summernote>')
.appendTo(document.body);
var element = $compile(el)(scope);
scope.$digest();
// when
selectText($(element.next().find('.note-editable'))[0]);
$(element.next().find('.note-font').find('button').eq(0)).click();
scope.$digest();
// tear down
el.next().remove();
el.remove();
});
// TODO: add tests for onImageUpload
});
describe('"editable" attribute', function() {
var scope;
beforeEach(function() {
scope = $rootScope.$new();
});
it('should be assigned as editable object', function () {
var el = $('<summernote editable="myEditable"></summernote>');
var element = $compile(el)(scope);
scope.$digest();
expect(element.next().find('.note-editable').get(0)).to.be.equal(scope.myEditable.get(0));
el.next().remove();
el.remove();
});
});
describe('"editor" attribute', function() {
var scope;
beforeEach(function() {
scope = $rootScope.$new();
});
it('should be assigned as editor object', function () {
var el = $('<summernote editor="snote"></summernote>');
var element = $compile(el)(scope);
scope.$digest();
expect(element.get(0)).to.be.equal(scope.snote.get(0));
el.next().remove();
el.remove();
});
});
describe('transclude', function() {
it('set initialize text with inner text', function() {
// given
var scope = $rootScope.$new();
var html = '<span style="font-weight: bold;">init text</span>';
// when
var element = $compile('<summernote>'+html+'</summernote>')(scope);
scope.$digest();
// then
expect(element.summernote('code')).to.be.equal(html);
});
it('set blank html if no text in summernote directive', function() {
// given
var scope = $rootScope.$new();
// when
var element = $compile('<summernote></summernote>')(scope);
scope.$digest();
// then
expect(element.summernote('code')).to.be.equal('<p><br></p>');
});
});
});