/
textAngular.spec.js
1249 lines (1157 loc) · 50.5 KB
/
textAngular.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
describe('textAngular', function(){
/*
Display Tests
*/
describe('Minimal Initiation', function(){
'use strict';
var $rootScope, element;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular><p>Test Content</p></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('Adds Correct Classes and Elements', function () {
it('add .ta-root to base element', function(){
expect(jQuery(element).hasClass('.ta-root'));
});
it('adds 2 .ta-editor elements', function(){
expect(jQuery('.ta-editor', element).length).toBe(2);
});
it('adds the WYSIWYG div', function(){
expect(jQuery('div.ta-text.ta-editor', element).length).toBe(1);
});
it('adds the textarea', function(){
expect(jQuery('textarea.ta-html.ta-editor', element).length).toBe(1);
});
it('no hidden form submission element', function(){
expect(jQuery('input[type=hidden][name=test]', element).length).toBe(0);
});
});
});
describe('Basic Initiation without ng-model', function(){
'use strict';
var $rootScope, element;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test"><p>Test Content</p></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('Adds Correct Classes and Elements', function () {
it('add .ta-root to base element', function(){
expect(jQuery(element).hasClass('.ta-root'));
});
it('adds 2 .ta-editor elements', function(){
expect(jQuery('.ta-editor', element).length).toBe(2);
});
it('adds the WYSIWYG div', function(){
expect(jQuery('div.ta-text.ta-editor', element).length).toBe(1);
});
it('adds the textarea', function(){
expect(jQuery('textarea.ta-html.ta-editor', element).length).toBe(1);
});
it('adds one hidden form submission element', function(){
expect(jQuery('input[type=hidden][name=test]', element).length).toBe(1);
});
});
});
describe('Add classes via attributes', function(){
'use strict';
var $rootScope, element, textAngularManager;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, _textAngularManager_) {
$rootScope = _$rootScope_;
textAngularManager = _textAngularManager_;
element = _$compile_('<text-angular name="test" ta-focussed-class="test-focus-class" ta-text-editor-class="test-text-class" ta-html-editor-class="test-html-class"></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('Adds Correct Classes', function () {
it('initially has no focus class', function(){
expect(!jQuery(element).hasClass('.test-focus-class'));
});
it('adds focus class when ta-text is focussed', function(){
textAngularManager.retrieveEditor('test').scope.displayElements.text.triggerHandler('focus');
expect(jQuery(element).hasClass('.test-focus-class'));
});
it('adds focus class when ta-html is focussed', function(){
jQuery('.ta-html', element).triggerHandler('focus');
expect(jQuery(element).hasClass('.test-focus-class'));
});
it('adds text editor class', function(){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.hasClass('.test-text-class'));
});
it('adds html editor class', function(){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.html.hasClass('.test-html-class'));
});
});
});
describe('Change classes via decorator', function(){
'use strict';
var $rootScope, element, textAngularManager;
beforeEach(module('textAngular', function($provide){
// change all the classes at once
$provide.decorator('taOptions', ['$delegate', function(taOptions){
taOptions.classes.focussed = "test-focus-class";
taOptions.classes.disabled = "disabled-test";
taOptions.classes.textEditor = 'test-text-class';
taOptions.classes.htmlEditor = 'test-html-class';
return taOptions;
}]);
}));
beforeEach(inject(function (_$compile_, _$rootScope_, _textAngularManager_) {
$rootScope = _$rootScope_;
textAngularManager = _textAngularManager_;
element = _$compile_('<text-angular name="test"></text-angular>')($rootScope);
$rootScope.$digest();
}));
it('should add .test-focus-class instead of default .focussed', function(){
textAngularManager.retrieveEditor('test').scope.displayElements.text.triggerHandler('focus');
expect(jQuery(element).hasClass('.test-focus-class'));
});
it('adds text editor class', function(){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.hasClass('.test-text-class'));
});
it('adds html editor class', function(){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.html.hasClass('.test-html-class'));
});
it('adds disabled class', function(){
expect(jQuery(element).hasClass('.disabled-test'));
});
});
describe('Add tabindex attribute', function(){
'use strict';
var $rootScope, element, textAngularManager;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, _textAngularManager_) {
$rootScope = _$rootScope_;
textAngularManager = _textAngularManager_;
element = _$compile_('<text-angular name="test" tabindex="42"></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('Check moved across', function () {
it('to textEditor', function(){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.attr('tabindex')).toBe('42');
});
it('to htmlEditor', function(){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.html.attr('tabindex')).toBe('42');
});
it('removed from .ta-root', function(){
expect(element.attr('tabindex')).toBeUndefined();
});
});
});
describe('Use serial attribute', function(){
'use strict';
var $rootScope, element, textAngularManager;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, _textAngularManager_) {
$rootScope = _$rootScope_;
textAngularManager = _textAngularManager_;
element = _$compile_('<text-angular serial="test"></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('generates id\'s and name attributes', function () {
it('name of editor', function(){
expect(textAngularManager.retrieveEditor('textAngularEditortest')).not.toBeUndefined();
});
it('to textEditor', function(){
expect(textAngularManager.retrieveEditor('textAngularEditortest').scope.displayElements.text.attr('id')).toBe('taTextElementtest');
});
it('to htmlEditor', function(){
expect(textAngularManager.retrieveEditor('textAngularEditortest').scope.displayElements.html.attr('id')).toBe('taHtmlElementtest');
});
});
});
describe('Disable the editor', function(){
'use strict';
var $rootScope, element;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$rootScope = _$rootScope_;
$rootScope.disabled = true;
element = _$compile_('<text-angular name="test" ta-disabled="disabled"></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('check disabled class', function () {
it('is added initially', function(){
expect(jQuery(element).hasClass('disaled'));
});
it('is removed on change to false', function(){
$rootScope.disabled = false;
$rootScope.$digest();
expect(!jQuery(element).hasClass('disabled'));
});
it('is added on change to true', function(){
$rootScope.disabled = false;
$rootScope.$digest();
$rootScope.disabled = true;
$rootScope.$digest();
expect(jQuery(element).hasClass('disabled'));
});
});
});
describe('Check view change', function(){
'use strict';
var $rootScope, element, editorScope;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager, $document) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test"></text-angular>')($rootScope);
$document.find('body').append(element);
editorScope = textAngularManager.retrieveEditor('test').scope;$document.find('body').append(element);
$rootScope.$digest();
}));
afterEach(function(){
element.remove();
});
it('initially should hide .ta-html and show .ta-text', function(){
expect(jQuery('.ta-text', element[0]).is(':visible')).toBe(true);
expect(jQuery('.ta-html', element[0]).is(':visible')).toBe(false);
});
describe('from WYSIWYG text to RAW HTML view', function () {
it('should hide .ta-text and show .ta-html', function(){
editorScope.switchView();
editorScope.$parent.$digest();
expect(jQuery('.ta-text', element[0]).is(':visible')).toBe(false);
expect(jQuery('.ta-html', element[0]).is(':visible')).toBe(true);
});
});
describe('from RAW HTML to WYSIWYG text view', function () {
it('should hide .ta-html and show .ta-text', function(){
editorScope.switchView();
editorScope.$parent.$digest();
editorScope.switchView();
editorScope.$parent.$digest();
expect(jQuery('.ta-text', element[0]).is(':visible')).toBe(true);
expect(jQuery('.ta-html', element[0]).is(':visible')).toBe(false);
});
});
});
describe('Check focussed class adding', function(){
'use strict';
var $rootScope, element, editorScope;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test"></text-angular>')($rootScope);
$rootScope.$digest();
editorScope = textAngularManager.retrieveEditor('test').scope;
}));
describe('should have added .focussed', function(){
it('on trigger focus on ta-text', function(){
editorScope.displayElements.text.triggerHandler('focus');
expect(element.hasClass('focussed'));
});
it('on trigger focus on ta-html', function(){
editorScope.displayElements.html.triggerHandler('focus');
expect(element.hasClass('focussed'));
});
});
describe('should have removed .focussed', function(){
it('on ta-text trigger blur', function(){
editorScope.displayElements.text.triggerHandler('focus');
$rootScope.$digest();
editorScope.displayElements.text.triggerHandler('blur');
expect(!element.hasClass('focussed'));
});
it('on ta-html trigger blur', function(){
editorScope.displayElements.html.triggerHandler('focus');
$rootScope.$digest();
editorScope.displayElements.html.triggerHandler('blur');
expect(!element.hasClass('focussed'));
});
});
});
describe('Check text and html editor setup functions', function(){
'use strict';
var $rootScope, element;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$rootScope = _$rootScope_;
$rootScope.attrSetup = function($element){
$element.attr('testattr', 'trueish');
};
element = _$compile_('<text-angular ta-text-editor-setup="attrSetup" ta-html-editor-setup="attrSetup" name="test"></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('should have added attribute to', function(){
it('ta-text', inject(function(textAngularManager){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.attr('testattr')).toBe('trueish');
}));
it('ta-html', inject(function(textAngularManager){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.html.attr('testattr')).toBe('trueish');
}));
});
});
describe('Check placeholder passthrough', function(){
'use strict';
var $rootScope, element;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test" placeholder="Test Placeholder"></text-angular>')($rootScope);
$rootScope.$digest();
}));
describe('should have added placeholder to', function(){
it('ta-text', inject(function(textAngularManager){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.attr('placeholder')).toBe('Test Placeholder');
}));
it('ta-html', inject(function(textAngularManager){
expect(textAngularManager.retrieveEditor('test').scope.displayElements.html.attr('placeholder')).toBe('Test Placeholder');
}));
});
});
describe('registration', function(){
beforeEach(module('textAngular'));
it('should add itself to the textAngularManager', inject(function($rootScope, $compile, textAngularManager){
$compile('<text-angular name="test"></text-angular>')($rootScope);
expect(textAngularManager.retrieveEditor('test')).not.toBeUndefined();
}));
it('should register toolbars to itself', inject(function($rootScope, $compile, textAngularManager){
var toolbar1 = {name: 'test-toolbar1'};
var toolbar2 = {name: 'test-toolbar2'};
textAngularManager.registerToolbar(toolbar1);
textAngularManager.registerToolbar(toolbar2);
$compile('<text-angular name="test" ta-target-toolbars="test-toolbar1,test-toolbar2"></text-angular>')($rootScope);
var _toolbars = textAngularManager.retrieveToolbarsViaEditor('test');
expect(_toolbars[0]).toBe(toolbar1);
expect(_toolbars[1]).toBe(toolbar2);
}));
});
describe('unregistration', function(){
beforeEach(module('textAngular'));
it('should remove itself from the textAngularManager on $destroy', inject(function($rootScope, $compile, textAngularManager){
var element = $compile('<text-angular name="test"></text-angular>')($rootScope);
$rootScope.$digest();
textAngularManager.retrieveEditor('test').scope.$destroy();
expect(textAngularManager.retrieveEditor('test')).toBeUndefined();
}));
});
/*
Form validation tests
*/
describe('form validation', function(){
'use strict';
var $rootScope, element;
beforeEach(module('textAngular'));
describe('basic', function(){
beforeEach(inject(function (_$compile_, _$rootScope_, _$window_, $document, textAngularManager) {
$window = _$window_;
$rootScope = _$rootScope_;
$rootScope.html = '';
var _form = angular.element('<form name="form"></form>');
element = angular.element('<text-angular name="test" ng-model="html"></text-angular>');
_form.append(element);
$document.find('body').append(_$compile_(_form)($rootScope));
element = textAngularManager.retrieveEditor('test').scope.displayElements.text;
$rootScope.$digest();
}));
describe('should start with', function () {
it('pristine', function(){
expect($rootScope.form.$pristine).toBe(true);
});
it('field pristine', function(){
expect($rootScope.form.test.$pristine).toBe(true);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
describe('should NOT change on direct model change', function () {
beforeEach(function(){
$rootScope.htmlcontent = '<div>Test Change Content</div>';
$rootScope.$digest();
});
it('pristine', function(){
expect($rootScope.form.$pristine).toBe(true);
});
it('field pristine', function(){
expect($rootScope.form.test.$pristine).toBe(true);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
describe('should change on input update', function () {
beforeEach(inject(function(textAngularManager){
element.html('<div>Test Change Content</div>');
element.triggerHandler('keyup');
$rootScope.$digest();
}));
it('not pristine', function(){
expect($rootScope.form.$pristine).toBe(false);
});
it('field not pristine', function(){
expect($rootScope.form.test.$pristine).toBe(false);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
describe('should change on blur', function () {
beforeEach(inject(function(textAngularManager){
element.html('<div>Test Change Content</div>');
element.triggerHandler('blur');
$rootScope.$digest();
}));
it('not pristine', function(){
expect($rootScope.form.$pristine).toBe(false);
});
it('field not pristine', function(){
expect($rootScope.form.test.$pristine).toBe(false);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
});
describe('with errors', function(){
beforeEach(inject(function (_$compile_, _$rootScope_, _$window_, $document, textAngularManager) {
$window = _$window_;
$rootScope = _$rootScope_;
$rootScope.html = '';
var _form = angular.element('<form name="form"></form>');
element = angular.element('<text-angular name="test" ng-model="html" required></text-angular>');
_form.append(element);
$document.find('body').append(_$compile_(_form)($rootScope));
element = textAngularManager.retrieveEditor('test').scope.displayElements.text;
$rootScope.$digest();
}));
describe('should start with', function () {
it('ng-required', function(){
expect($rootScope.form.test.$error.required).toBe(true);
});
it('valid', function(){
expect($rootScope.form.$invalid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$invalid).toBe(true);
});
});
describe('should change on direct model change', function () {
beforeEach(function(){
$rootScope.html = '<div>Test Change Content</div>';
$rootScope.$digest();
});
it('ng-required', function(){
expect($rootScope.form.test.$error.required).toBe(false);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
describe('should change on input update', function () {
beforeEach(inject(function(textAngularManager){
element.html('<div>Test Change Content</div>');
element.triggerHandler('keyup');
$rootScope.$digest();
}));
it('ng-required', function(){
expect($rootScope.form.test.$error.required).toBe(false);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
describe('should change on blur', function () {
beforeEach(inject(function(textAngularManager){
element.html('<div>Test Change Content</div>');
element.triggerHandler('blur');
$rootScope.$digest();
}));
it('ng-required', function(){
expect($rootScope.form.test.$error.required).toBe(false);
});
it('valid', function(){
expect($rootScope.form.$valid).toBe(true);
});
it('field valid', function(){
expect($rootScope.form.test.$valid).toBe(true);
});
});
});
});
/*
Data Tests
*/
describe('Basic Initiation without ng-model', function(){
'use strict';
var $rootScope, element, displayElements;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test"><p>Test Content</p></text-angular>')($rootScope);
$rootScope.$digest();
displayElements = textAngularManager.retrieveEditor('test').scope.displayElements;
}));
describe('Inserts the current information into the fields', function(){
it('populates the WYSIWYG area', function(){
expect(displayElements.text.find('p').length).toBe(1);
});
it('populates the textarea', function(){
expect(displayElements.html.val()).toBe('<p>Test Content</p>');
});
it('populates the hidden input value', function(){
expect(displayElements.forminput.val()).toBe('<p>Test Content</p>');
});
});
});
describe('Basic Initiation with ng-model', function(){
'use strict';
var $rootScope, element, displayElements;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
$rootScope.html = '<p>Test Content</p>';
element = _$compile_('<text-angular name="test" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
displayElements = textAngularManager.retrieveEditor('test').scope.displayElements;
}));
describe('Inserts the current information into the fields', function(){
it('populates the WYSIWYG area', function(){
expect(displayElements.text.find('p').length).toBe(1);
});
it('populates the textarea', function(){
expect(displayElements.html.val()).toBe('<p>Test Content</p>');
});
it('populates the hidden input value', function(){
expect(displayElements.forminput.val()).toBe('<p>Test Content</p>');
});
});
});
describe('Basic Initiation with ng-model and originalContents', function(){
'use strict';
var $rootScope, element, displayElements;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
$rootScope.html = '<p>Test Content</p>';
element = _$compile_('<text-angular name="test" ng-model="html"><p>Original Content</p></text-angular>')($rootScope);
$rootScope.$digest();
displayElements = textAngularManager.retrieveEditor('test').scope.displayElements;
}));
describe('Inserts the current information into the fields', function(){
it('populates the WYSIWYG area', function(){
expect(displayElements.text.find('p').length).toBe(1);
});
it('populates the textarea', function(){
expect(displayElements.html.val()).toBe('<p>Test Content</p>');
});
it('populates the hidden input value', function(){
expect(displayElements.forminput.val()).toBe('<p>Test Content</p>');
});
});
});
describe('should respect the ta-default-wrap value', function(){
beforeEach(module('textAngular'));
it('with ng-model', inject(function($rootScope, $compile, textAngularManager){
$rootScope.html = '';
$compile('<text-angular ta-default-wrap="div" name="test" ng-model="html"></text-angular>')($rootScope);
element = textAngularManager.retrieveEditor('test').scope.displayElements.text;
$rootScope.$digest();
element.triggerHandler('focus');
$rootScope.$digest();
expect(element.html()).toBe('<div><br></div>');
}));
it('without ng-model', inject(function($rootScope, $compile, textAngularManager){
$compile('<text-angular ta-default-wrap="div" name="test"></text-angular>')($rootScope);
element = textAngularManager.retrieveEditor('test').scope.displayElements.text;
$rootScope.$digest();
element.triggerHandler('focus');
$rootScope.$digest();
expect(element.html()).toBe('<div><br></div>');
}));
});
describe('should respect taUnsafeSanitizer attribute', function () {
'use strict';
var $rootScope, element, element2, editorScope, displayElements;
beforeEach(module('textAngular'));
describe('without ng-model', function(){
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test" ta-unsafe-sanitizer="true"><p>Test Contents</p></text-angular>')($rootScope);
$rootScope.$digest();
editorScope = textAngularManager.retrieveEditor('test').scope;
element = textAngularManager.retrieveEditor('test').scope.displayElements.text;
element2 = textAngularManager.retrieveEditor('test').scope.displayElements.html;
}));
it('allow bad tags', function () {
element.append('<bad-tag>Test 2 Content</bad-tag>');
element.triggerHandler('keyup');
$rootScope.$digest();
expect(element2.val()).toBe('<p>Test Contents</p><bad-tag>Test 2 Content</bad-tag>');
});
it('not allow malformed html', function () {
element.append('<bad-tag Test 2 Content</bad-tag>');
element.triggerHandler('keyup');
$rootScope.$digest();
expect(element2.val()).toBe('<p>Test Contents</p>');
});
});
describe('with ng-model', function(){
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
$rootScope.html = '<p>Test Contents</p>';
element = _$compile_('<text-angular name="test" ta-unsafe-sanitizer="true" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
editorScope = textAngularManager.retrieveEditor('test').scope;
element = editorScope.displayElements.text;
}));
it('allow bad tags', function () {
element.append('<bad-tag>Test 2 Content</bad-tag>');
element.triggerHandler('keyup');
$rootScope.$digest();
expect($rootScope.html).toBe('<p>Test Contents</p><bad-tag>Test 2 Content</bad-tag>');
});
it('not allow malformed html', function () {
element.append('<bad-tag Test 2 Content</bad-tag>');
element.triggerHandler('keyup');
$rootScope.$digest();
expect($rootScope.html).toBe('<p>Test Contents</p>');
});
});
});
describe('Updates without ng-model', function(){
'use strict';
var $rootScope, element, displayElements;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
element = _$compile_('<text-angular name="test"><p>Test Content</p></text-angular>')($rootScope);
$rootScope.$digest();
displayElements = textAngularManager.retrieveEditor('test').scope.displayElements;
displayElements.text.html('<div>Test Change Content</div>');
displayElements.text.triggerHandler('keyup');
$rootScope.$digest();
}));
describe('updates from .ta-text', function(){
it('should update .ta-html', function(){
expect(displayElements.html.val()).toBe('<div>Test Change Content</div>');
});
it('should update input[type=hidden]', function(){
expect(displayElements.forminput.val()).toBe('<div>Test Change Content</div>');
});
});
describe('updates from .ta-html', function(){
it('should update .ta-text', function(){
expect(displayElements.text.html()).toBe('<div>Test Change Content</div>');
});
it('should update input[type=hidden]', function(){
expect(displayElements.forminput.val()).toBe('<div>Test Change Content</div>');
});
});
});
describe('Updates with ng-model', function(){
'use strict';
var $rootScope, element, displayElements;
beforeEach(module('textAngular'));
beforeEach(inject(function (_$compile_, _$rootScope_, textAngularManager) {
$rootScope = _$rootScope_;
$rootScope.html = '<p>Test Content</p>';
element = _$compile_('<text-angular name="test" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
$rootScope.html = '<div>Test Change Content</div>';
$rootScope.$digest();
displayElements = textAngularManager.retrieveEditor('test').scope.displayElements;
}));
describe('updates from model to display', function(){
it('should update .ta-html', function(){
expect(displayElements.html.val()).toBe('<div>Test Change Content</div>');
});
it('should update .ta-text', function(){
expect(displayElements.text.html()).toBe('<div>Test Change Content</div>');
});
it('should update input[type=hidden]', function(){
expect(displayElements.forminput.val()).toBe('<div>Test Change Content</div>');
});
});
});
describe('ng-model should handle undefined and null', function(){
'use strict';
beforeEach(module('textAngular'));
it('should handle initial undefined to empty-string', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = undefined;
var element = $compile('<text-angular name="test" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p><br></p>');
}));
it('should handle initial null to empty-string', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = null;
var element = $compile('<text-angular name="test" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p><br></p>');
}));
it('should handle initial undefined to originalContents', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = undefined;
var element = $compile('<text-angular name="test" ng-model="html">Test Contents</text-angular>')($rootScope);
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p>Test Contents</p>');
}));
it('should handle initial null to originalContents', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = null;
var element = $compile('<text-angular name="test" ng-model="html">Test Contents</text-angular>')($rootScope);
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p>Test Contents</p>');
}));
describe('should reset', function(){
it('from undefined to empty-string', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = 'Test Content';
var element = $compile('<text-angular name="test" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
$rootScope.html = undefined;
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p><br></p>');
}));
it('from null to empty-string', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = 'Test Content';
var element = $compile('<text-angular name="test" ng-model="html"></text-angular>')($rootScope);
$rootScope.$digest();
$rootScope.html = null;
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p><br></p>');
}));
it('from undefined to blank/emptystring WITH originalContents', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = 'Test Content1';
var element = $compile('<text-angular name="test" ng-model="html">Test Contents2</text-angular>')($rootScope);
$rootScope.$digest();
$rootScope.html = undefined;
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p><br></p>');
}));
it('from null to blank/emptystring WITH originalContents', inject(function ($compile, $rootScope, textAngularManager) {
$rootScope.html = 'Test Content1';
var element = $compile('<text-angular name="test" ng-model="html">Test Contents2</text-angular>')($rootScope);
$rootScope.$digest();
$rootScope.html = null;
$rootScope.$digest();
expect(textAngularManager.retrieveEditor('test').scope.displayElements.text.html()).toBe('<p><br></p>');
}));
});
});
describe('should have correct startAction and endAction functions', function(){
'use strict';
beforeEach(module('textAngular'));
it('should have rangy loaded with save-restore module', function(){
expect(window.rangy).toBeDefined();
expect(window.rangy.saveSelection).toBeDefined();
});
var editorScope, element, sel, range;
beforeEach(inject(function($compile, $rootScope, textAngularManager, $document){
$rootScope.html = '<p>Lorem ipsum dolor sit amet, <i>consectetur adipisicing</i> elit, <strong>sed do eiusmod tempor incididunt</strong> ut labore et dolore magna aliqua.</p>';
element = $compile('<text-angular name="test" ng-model="html">Test Contents2</text-angular>')($rootScope);
$document.find('body').append(element);
$rootScope.$digest();
editorScope = textAngularManager.retrieveEditor('test').scope;
// setup selection
sel = window.rangy.getSelection();
range = window.rangy.createRangyRange();
range.selectNodeContents(editorScope.displayElements.text.find('p').find('strong')[0]);
sel.setSingleRange(range);
}));
afterEach(function(){
element.remove();
});
describe('startAction should return a function that will restore a selection', function(){
it('should start with the correct selection', function(){
expect(sel.getRangeAt(0).toHtml()).toBe('sed do eiusmod tempor incididunt');
});
it('should set _actionRunning to true', function(){
editorScope.startAction();
expect(editorScope._actionRunning);
});
it('should return a function that resets the selection', function(){
var resetFunc = editorScope.startAction();
expect(sel.toHtml()).toBe('sed do eiusmod tempor incididunt');
// change selection
var range = window.rangy.createRangyRange();
range.selectNodeContents(editorScope.displayElements.text.find('p').find('i')[0]);
sel.setSingleRange(range);
sel.refresh();
expect(sel.toHtml()).toBe('consectetur adipisicing');
// reset selection
resetFunc();
sel.refresh();
expect(sel.toHtml()).toBe('sed do eiusmod tempor incididunt');
});
});
describe('endAction should remove the ability to restore selection', function(){
it('shouldn\'t affect the selection', function(){
editorScope.startAction();
editorScope.endAction();
expect(sel.toHtml()).toBe('sed do eiusmod tempor incididunt');
});
it('shouldn\'t restore the selection', function(){
var resetFunc = editorScope.startAction();
editorScope.endAction();
var range = window.rangy.createRangyRange();
range.selectNodeContents(editorScope.displayElements.text.find('p').find('i')[0]);
sel.setSingleRange(range);
sel.refresh();
expect(sel.toHtml()).toBe('consectetur adipisicing');
// reset selection - should do nothing now
resetFunc();
sel.refresh();
expect(sel.toHtml()).toBe('consectetur adipisicing');
});
});
});
describe('Toolbar interaction functions work', function(){
beforeEach(module('textAngular'));
var editorScope, element, sel, range;
beforeEach(inject(function(taRegisterTool, taOptions, taSelectableElements){
taSelectableElements.push('i');
taRegisterTool('testbutton', {
buttontext: 'reactive action',
action: function(){
return this.$element.attr('hit-this', 'true');
},
commandKeyCode: 21,
activeState: function(){ return true; },
onElementSelect: {
element: 'i',
action: function(event, element, editorScope){
element.attr('hit-via-select', 'true');
this.$element.attr('hit-via-select', 'true');
}
}
});
taOptions.toolbar = [
['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'pre', 'quote', 'testbutton'],
['bold', 'italics', 'underline', 'strikeThrough', 'ul', 'ol', 'redo', 'undo', 'clear'],
['justifyLeft','justifyCenter','justifyRight','indent','outdent'],
['html', 'insertImage', 'insertLink', 'unlink']
];
}));
beforeEach(inject(function($compile, $rootScope, textAngularManager, $document){
$rootScope.html = '<p>Lorem ipsum <u>dolor sit amet<u>, consectetur <i>adipisicing elit, sed do eiusmod tempor incididunt</i> ut labore et <a>dolore</a> magna aliqua.</p>';
element = $compile('<text-angular name="test" ng-model="html">Test Contents2</text-angular>')($rootScope);
$document.find('body').append(element);
editorScope = textAngularManager.retrieveEditor('test').scope;
textAngularManager.retrieveEditor('test').editorFunctions.focus();
$rootScope.$digest();
// setup selection
sel = window.rangy.getSelection();
range = window.rangy.createRangyRange();
range.selectNodeContents(editorScope.displayElements.text.find('p').find('i')[0]);
sel.setSingleRange(range);
}));
afterEach(inject(function(taSelectableElements){
taSelectableElements.pop();
element.remove();
}));
it('should not trigger focus out while an action is processing', inject(function($timeout){
editorScope.displayElements.text.triggerHandler('focus');
editorScope.$parent.$digest();
editorScope.startAction();
editorScope.displayElements.text.triggerHandler('blur');
editorScope.$parent.$digest();
$timeout.flush();
expect(jQuery(element[0]).find('button:disabled').length).toBe(0);
}));
it('keypress should call sendKeyCommand', function(){
editorScope.displayElements.text.triggerHandler('keypress', {metaKey: true, which: 21});
editorScope.$parent.$digest();
expect(jQuery(element[0]).find('.ta-toolbar button[name=testbutton]').attr('hit-this')).toBe('true');
});
describe('wrapSelection', function(){
it('should wrap the selected text in tags', function(){
editorScope.wrapSelection('bold');
expect(editorScope.displayElements.text.find('p').find('b').length).toBe(1);
});
it('should unwrap the selected text in tags on re-call', function(){
editorScope.wrapSelection('bold');
editorScope.wrapSelection('bold');
expect(editorScope.displayElements.text.find('p').find('b').length).toBe(0);
});
});
describe('queryFormatBlockState', function(){
it('should return true if formatted', function(){
editorScope.wrapSelection("formatBlock", "<PRE>");
expect(editorScope.queryFormatBlockState('PRE')).toBe(true);
});
it('should return false if un-formatted', function(){
expect(editorScope.queryFormatBlockState('PRE')).toBe(false);
});
});
describe('queryCommandState', function(){
it('should return value if not showHtml', function(){
editorScope.showHtml = false;
editorScope.$parent.$digest();
expect(editorScope.queryCommandState('bold')).not.toBe('');
});
it('should return empty if showHtml', function(){
editorScope.showHtml = true;
editorScope.$parent.$digest();
expect(editorScope.queryCommandState('bold')).toBe('');
});
});
describe('ta-element-select event', function(){
it('fires correctly on element selector', function(){
var triggerElement = editorScope.displayElements.text.find('i');
triggerElement.triggerHandler('click');
expect(triggerElement.attr('hit-via-select')).toBe('true');
});
it('fires correctly when filter returns true', inject(function(taTools){
taTools.testbutton.onElementSelect.filter = function(){ return true; };
var triggerElement = editorScope.displayElements.text.find('i');
triggerElement.triggerHandler('click');
expect(triggerElement.attr('hit-via-select')).toBe('true');
}));
it('does not fire when filter returns false', inject(function(taTools){
taTools.testbutton.onElementSelect.filter = function(){ return false; };
var triggerElement = editorScope.displayElements.text.find('i');