1
1
import { ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
2
2
3
+ import * as UtilsFunction from '../../../../utils/util' ;
3
4
import { configureTestSuite } from './../../../../util-test/util-expect.spec' ;
4
5
5
6
import { PoRichTextBodyComponent } from './po-rich-text-body.component' ;
@@ -49,7 +50,7 @@ describe('PoRichTextBodyComponent:', () => {
49
50
describe ( 'executeCommand:' , ( ) => {
50
51
51
52
it ( 'should call `focus`' , ( ) => {
52
- const spyFocus = spyOn ( component . bodyElement . nativeElement , < any > 'focus' ) ;
53
+ const spyFocus = spyOn ( component . bodyElement . nativeElement , < any > 'focus' ) ;
53
54
const fakeValue = 'p' ;
54
55
55
56
component . executeCommand ( fakeValue ) ;
@@ -58,7 +59,7 @@ describe('PoRichTextBodyComponent:', () => {
58
59
} ) ;
59
60
60
61
it ( 'should call `execCommand` with string as parameter.' , ( ) => {
61
- const spyExecCommand = spyOn ( document , < any > 'execCommand' ) ;
62
+ const spyExecCommand = spyOn ( document , < any > 'execCommand' ) ;
62
63
const fakeValue = 'p' ;
63
64
64
65
component . executeCommand ( fakeValue ) ;
@@ -69,12 +70,29 @@ describe('PoRichTextBodyComponent:', () => {
69
70
it ( 'should call `execCommand` with object as parameter.' , ( ) => {
70
71
const command = 'foreColor' ;
71
72
const value = '#000000' ;
72
- const spyExecCommand = spyOn ( document , < any > 'execCommand' ) ;
73
- const fakeValue = { command, value } ;
73
+ const spyExecCommand = spyOn ( document , < any > 'execCommand' ) ;
74
+ const fakeValue = { command, value } ;
75
+
76
+ spyOn ( component , < any > 'handleCommandLink' ) ;
74
77
75
78
component . executeCommand ( fakeValue ) ;
76
79
77
80
expect ( spyExecCommand ) . toHaveBeenCalledWith ( fakeValue . command , false , fakeValue . value ) ;
81
+ expect ( component [ 'handleCommandLink' ] ) . not . toHaveBeenCalled ( ) ;
82
+ } ) ;
83
+
84
+ it ( 'should call `handleCommandLink` with an object as parameter if command value is `InsertHTML`.' , ( ) => {
85
+ const command = 'InsertHTML' ;
86
+ const value = { urlLink : 'link' , urlLinkText : 'link text' } ;
87
+ const spyExecCommand = spyOn ( document , < any > 'execCommand' ) ;
88
+ const fakeValue = { command, value } ;
89
+
90
+ spyOn ( component , < any > 'handleCommandLink' ) ;
91
+
92
+ component . executeCommand ( fakeValue ) ;
93
+
94
+ expect ( component [ 'handleCommandLink' ] ) . toHaveBeenCalledWith ( command , value . urlLink , value . urlLinkText ) ;
95
+ expect ( spyExecCommand ) . not . toHaveBeenCalled ( ) ;
78
96
} ) ;
79
97
80
98
it ( 'should call `updateModel`' , ( ) => {
@@ -152,6 +170,90 @@ describe('PoRichTextBodyComponent:', () => {
152
170
expect ( component [ 'emitSelectionCommands' ] ) . toHaveBeenCalled ( ) ;
153
171
} ) ;
154
172
173
+ it ( 'onKeyDown: should call `event.preventDefault` and `shortcutCommand.emit` if keyCode is `76` and ctrlKey is `true`' , ( ) => {
174
+ const fakeEvent = {
175
+ keyCode : 76 ,
176
+ ctrlKey : true ,
177
+ preventDefault : ( ) => { } ,
178
+ } ;
179
+
180
+ spyOn ( component . shortcutCommand , 'emit' ) ;
181
+ spyOn ( fakeEvent , 'preventDefault' ) ;
182
+
183
+ component . onKeyDown ( fakeEvent ) ;
184
+
185
+ expect ( fakeEvent . preventDefault ) . toHaveBeenCalled ( ) ;
186
+ expect ( component . shortcutCommand . emit ) . toHaveBeenCalled ( ) ;
187
+ } ) ;
188
+
189
+ it ( 'onKeyDown: should call `event.preventDefault` and `shortcutCommand.emit` if keyCode is `76` and metaKey is `true`' , ( ) => {
190
+ const fakeEvent = {
191
+ keyCode : 76 ,
192
+ metaKey : true ,
193
+ preventDefault : ( ) => { } ,
194
+ } ;
195
+
196
+ spyOn ( component . shortcutCommand , 'emit' ) ;
197
+ spyOn ( fakeEvent , 'preventDefault' ) ;
198
+
199
+ component . onKeyDown ( fakeEvent ) ;
200
+
201
+ expect ( fakeEvent . preventDefault ) . toHaveBeenCalled ( ) ;
202
+ expect ( component . shortcutCommand . emit ) . toHaveBeenCalled ( ) ;
203
+ } ) ;
204
+
205
+ it ( 'onKeyDown: shouldn`t call `event.preventDefault` and `shortcutCommand.emit` if keyCode isn`t `76`' , ( ) => {
206
+ const fakeEvent = {
207
+ keyCode : 18 ,
208
+ cmdKey : true ,
209
+ preventDefault : ( ) => { } ,
210
+ } ;
211
+
212
+ spyOn ( component . shortcutCommand , 'emit' ) ;
213
+ spyOn ( fakeEvent , 'preventDefault' ) ;
214
+
215
+ component . onKeyDown ( fakeEvent ) ;
216
+
217
+ expect ( fakeEvent . preventDefault ) . not . toHaveBeenCalled ( ) ;
218
+ expect ( component . shortcutCommand . emit ) . not . toHaveBeenCalled ( ) ;
219
+ } ) ;
220
+
221
+ it ( 'onKeyDown: shouldn`t call `event.preventDefault` and `shortcutCommand.emit` if ctrlKey isn`t true' , ( ) => {
222
+ const fakeEvent = {
223
+ keyCode : 76 ,
224
+ ctrlKey : false ,
225
+ preventDefault : ( ) => { } ,
226
+ } ;
227
+
228
+ spyOn ( component . shortcutCommand , 'emit' ) ;
229
+ spyOn ( fakeEvent , 'preventDefault' ) ;
230
+
231
+ component . onKeyDown ( fakeEvent ) ;
232
+
233
+ expect ( fakeEvent . preventDefault ) . not . toHaveBeenCalled ( ) ;
234
+ expect ( component . shortcutCommand . emit ) . not . toHaveBeenCalled ( ) ;
235
+ } ) ;
236
+
237
+ it ( 'cursorPositionedInALink: should return true if tag element is a link' , ( ) => {
238
+ const fakeSelection = { focusNode : { parentElement : { tagName : 'A' } } } ;
239
+
240
+ spyOn ( document , 'getSelection' ) . and . returnValue ( < any > fakeSelection ) ;
241
+
242
+ const expectedValue = component [ 'cursorPositionedInALink' ] ( ) ;
243
+
244
+ expect ( expectedValue ) . toBe ( true ) ;
245
+ } ) ;
246
+
247
+ it ( 'cursorPositionedInALink: should return false if tag element isn`t a link' , ( ) => {
248
+ const fakeSelection = { focusNode : { parentElement : { tagName : 'B' } } } ;
249
+
250
+ spyOn ( document , 'getSelection' ) . and . returnValue ( < any > fakeSelection ) ;
251
+
252
+ const expectedValue = component [ 'cursorPositionedInALink' ] ( ) ;
253
+
254
+ expect ( expectedValue ) . toBe ( false ) ;
255
+ } ) ;
256
+
155
257
it ( 'update: should call `updateModel`' , fakeAsync ( ( ) => {
156
258
spyOn ( component , < any > 'updateModel' ) ;
157
259
@@ -177,6 +279,86 @@ describe('PoRichTextBodyComponent:', () => {
177
279
expect ( component . commands . emit ) . toHaveBeenCalled ( ) ;
178
280
} ) ;
179
281
282
+ it ( `emitSelectionCommands: the object property 'commands'
283
+ should contain 'Createlink' if 'cursorPositionedInALink' returns 'true'` , ( ) => {
284
+
285
+ spyOn ( component , < any > 'cursorPositionedInALink' ) . and . returnValue ( true ) ;
286
+ spyOn ( document , 'queryCommandState' ) . and . returnValue ( false ) ;
287
+ spyOn ( document , 'queryCommandValue' ) . and . returnValue ( 'rgb' ) ;
288
+ spyOn ( component , < any > 'rgbToHex' ) . and . returnValue ( 'hex' ) ;
289
+ spyOn ( component . commands , 'emit' ) ;
290
+
291
+ component [ 'emitSelectionCommands' ] ( ) ;
292
+
293
+ expect ( component . commands . emit ) . toHaveBeenCalledWith ( { commands : [ 'Createlink' ] , hexColor : 'hex' } ) ;
294
+ } ) ;
295
+
296
+ it ( `emitSelectionCommands: the object property 'commands'
297
+ shouldn't contain 'Createlink' if 'cursorPositionedInALink' returns 'false'` , ( ) => {
298
+
299
+ spyOn ( component , < any > 'cursorPositionedInALink' ) . and . returnValue ( false ) ;
300
+ spyOn ( document , 'queryCommandState' ) . and . returnValue ( false ) ;
301
+ spyOn ( document , 'queryCommandValue' ) . and . returnValue ( 'rgb' ) ;
302
+ spyOn ( component , < any > 'rgbToHex' ) . and . returnValue ( 'hex' ) ;
303
+ spyOn ( component . commands , 'emit' ) ;
304
+
305
+ component [ 'emitSelectionCommands' ] ( ) ;
306
+
307
+ expect ( component . commands . emit ) . toHaveBeenCalledWith ( { commands : [ ] , hexColor : 'hex' } ) ;
308
+ } ) ;
309
+
310
+ it ( 'handleCommandLink: should call `insertHtmlLinkElement` if isIE returns `true`' , ( ) => {
311
+ const fakeValue = {
312
+ command : 'InsertHTML' ,
313
+ urlLink : 'urlLink' ,
314
+ urlLinkText : 'url link text'
315
+ } ;
316
+
317
+ spyOn ( UtilsFunction , 'isIE' ) . and . returnValue ( true ) ;
318
+ spyOn ( component , < any > 'insertHtmlLinkElement' ) ;
319
+ spyOn ( document , < any > 'execCommand' ) ;
320
+
321
+ component [ 'handleCommandLink' ] ( fakeValue . command , fakeValue . urlLink , fakeValue . urlLinkText ) ;
322
+
323
+ expect ( document . execCommand ) . not . toHaveBeenCalled ( ) ;
324
+ expect ( UtilsFunction . isIE ) . toHaveBeenCalled ( ) ;
325
+ expect ( component [ 'insertHtmlLinkElement' ] ) . toHaveBeenCalledWith ( fakeValue . urlLink , fakeValue . urlLinkText ) ;
326
+ } ) ;
327
+
328
+ it ( 'handleCommandLink: should call `document.execCommand` with `command`, `false` and linkValue as params if isIE is `false`' , ( ) => {
329
+ const linkValue = `<a class="po-rich-text-link" href="urlLink" target="_blank">url link text</a>` ;
330
+ const fakeValue = {
331
+ command : 'InsertHTML' ,
332
+ urlLink : 'urlLink' ,
333
+ urlLinkText : 'url link text'
334
+ } ;
335
+
336
+ spyOn ( UtilsFunction , 'isIE' ) . and . returnValue ( false ) ;
337
+ spyOn ( component , < any > 'insertHtmlLinkElement' ) ;
338
+ spyOn ( document , < any > 'execCommand' ) ;
339
+
340
+ component [ 'handleCommandLink' ] ( fakeValue . command , fakeValue . urlLink , fakeValue . urlLinkText ) ;
341
+
342
+ expect ( document . execCommand ) . toHaveBeenCalledWith ( fakeValue . command , false , linkValue ) ;
343
+ expect ( component [ 'insertHtmlLinkElement' ] ) . not . toHaveBeenCalled ( ) ;
344
+ } ) ;
345
+
346
+ it ( `handleCommandLink: the parameter 'linkvalue' should be concatenated with 'urlLink' if 'urlLinkText' is undefined` , ( ) => {
347
+ const linkValue = `<a class="po-rich-text-link" href="urlLink" target="_blank">urlLink</a>` ;
348
+ const fakeValue = {
349
+ command : 'InsertHTML' ,
350
+ urlLink : 'urlLink' ,
351
+ urlLinkText : undefined
352
+ } ;
353
+
354
+ spyOn ( UtilsFunction , 'isIE' ) . and . returnValue ( false ) ;
355
+ spyOn ( document , < any > 'execCommand' ) ;
356
+
357
+ component [ 'handleCommandLink' ] ( fakeValue . command , fakeValue . urlLink , fakeValue . urlLinkText ) ;
358
+
359
+ expect ( document . execCommand ) . toHaveBeenCalledWith ( fakeValue . command , false , linkValue ) ;
360
+ } ) ;
361
+
180
362
it ( 'updateModel: should update `modelValue`' , ( ) => {
181
363
component . bodyElement . nativeElement . innerHTML = 'teste' ;
182
364
component [ 'updateModel' ] ( ) ;
@@ -258,14 +440,27 @@ describe('PoRichTextBodyComponent:', () => {
258
440
expect ( fakeThis . change . emit ) . not . toHaveBeenCalled ( ) ;
259
441
} ) ) ;
260
442
443
+ it ( 'insertHtmlLinkElement: should contain `po-rich-text-link`' , ( ) => {
444
+ const urlLink = 'urlLink' ;
445
+ const urlLinkText = 'url link text' ;
446
+
447
+ component . focus ( ) ;
448
+
449
+ component [ 'insertHtmlLinkElement' ] ( urlLink , urlLinkText ) ;
450
+
451
+ fixture . detectChanges ( ) ;
452
+
453
+ expect ( nativeElement . querySelector ( '.po-rich-text-link' ) ) . toBeTruthy ( ) ;
454
+ } ) ;
455
+
261
456
} ) ;
262
457
263
458
describe ( 'Templates:' , ( ) => {
264
459
265
- it ( 'should contain `po-rich-text-body`' , ( ) => {
460
+ it ( 'should contain `po-rich-text-body`' , ( ) => {
266
461
267
- expect ( nativeElement . querySelector ( '.po-rich-text-body' ) ) . toBeTruthy ( ) ;
268
- } ) ;
462
+ expect ( nativeElement . querySelector ( '.po-rich-text-body' ) ) . toBeTruthy ( ) ;
463
+ } ) ;
269
464
270
465
} ) ;
271
466
0 commit comments