@@ -30,58 +30,90 @@ describe('Markdown component - ', () => {
30
30
} ) ;
31
31
} ) ) ;
32
32
33
- it ( 'Should handle Markdown checkboxes correctly.' ,
34
- inject ( [ DomSanitizer ] , ( domSanitizer : DomSanitizer ) => {
35
- // tslint:disable-next-line:max-line-length
36
- comp . inpRawText = '# hello, markdown!\n* [ ] Item 1\n* [x] Item 2\n* [ ] Item 3' ;
37
- let originalHTML = '<h1>hello, markdown!\</h1><ul>' +
38
- // tslint:disable-next-line:max-line-length
39
- '<li><input class="markdown-checkbox" type="checkbox" data-checkbox-index="0"></input> Item 0</li>' +
40
- // tslint:disable-next-line:max-line-length
41
- '<li><input class="markdown-checkbox" type="checkbox" checked="" data-checkbox-index="1"></input> Item 1</li>' +
42
- // tslint:disable-next-line:max-line-length
43
- '<li><input class="markdown-checkbox" type="checkbox" data-checkbox-index="2"></input> Item 2</li></ul>' ;
44
- // in this test, we provide a SaveValue to the component.
45
- comp . inpRenderedText = domSanitizer . bypassSecurityTrustHtml ( originalHTML ) ;
46
- // this first detectChanges() updates the component that one of the @Inputs has changed.
47
- fixture . detectChanges ( ) ;
48
- // because of https://github.com/angular/angular/issues/9866, detectChanges() does not
49
- // call ngOnChanges() on the component (yeah, it it as broken as it sounds). So
50
- // we need to call the component manually to update.
51
- comp . ngOnChanges ( {
52
- inpRawText : { } as SimpleChange ,
53
- inpRenderedText : { } as SimpleChange
54
- } as SimpleChanges ) ;
55
- // and because the test framework is not even able to detect inner changes to a component,
56
- // we need to call detectChanges() again.
57
- fixture . detectChanges ( ) ;
58
- // also, using query() is also not working. Maybe due to the dynamic update of innerHTML.
59
- // So we need to use the nativeElement to get a selector working.
60
- // tslint:disable-next-line:max-line-length
61
- let markdownPreview : Element = fixture . debugElement . nativeElement . querySelector ( '.markdown-rendered' ) ;
62
- expect ( markdownPreview ) . not . toBeNull ( ) ;
63
- // preview render of the template default
64
- let markdownCheckboxElementList = markdownPreview . querySelectorAll ( '.markdown-checkbox' ) ;
65
- expect ( markdownCheckboxElementList ) . not . toBeNull ( ) ;
66
- expect ( markdownCheckboxElementList . length ) . toBe ( 3 ) ;
67
- expect ( markdownCheckboxElementList [ 0 ] . hasAttribute ( 'checked' ) ) . toBeFalsy ( ) ;
68
- expect ( markdownCheckboxElementList [ 1 ] . hasAttribute ( 'checked' ) ) . toBeTruthy ( ) ;
69
- expect ( markdownCheckboxElementList [ 2 ] . hasAttribute ( 'checked' ) ) . toBeFalsy ( ) ;
70
- // tick a checkbox
71
- let checkboxElem = markdownCheckboxElementList [ 0 ] as HTMLElement ;
72
- checkboxElem . click ( ) ;
73
- // see if it ends up in the Markdown
74
- expect ( comp . rawText . indexOf ( '[x] Item 1' ) ) . toBeGreaterThan ( - 1 ) ;
75
- // tick another checkbox
76
- checkboxElem = markdownCheckboxElementList [ 2 ] as HTMLElement ;
77
- checkboxElem . click ( ) ;
78
- // see if it ends up in the Markdown
79
- expect ( comp . rawText . indexOf ( '[x] Item 3' ) ) . toBeGreaterThan ( - 1 ) ;
80
- // untick a checkbox
81
- checkboxElem = markdownCheckboxElementList [ 1 ] as HTMLElement ;
82
- checkboxElem . click ( ) ;
83
- // see if it ends up in the Markdown
84
- expect ( comp . rawText . indexOf ( '[ ] Item 2' ) ) . toBeGreaterThan ( - 1 ) ;
85
- } )
86
- ) ;
33
+ it ( 'Should handle Markdown checkboxes correctly.' ,
34
+ inject ( [ DomSanitizer ] , ( domSanitizer : DomSanitizer ) => {
35
+ // tslint:disable-next-line:max-line-length
36
+ comp . inpRawText = '# hello, markdown!\n* [ ] Item 1\n* [x] Item 2\n* [ ] Item 3' ;
37
+ let originalHTML = '<h1>hello, markdown!\</h1><ul>' +
38
+ // tslint:disable-next-line:max-line-length
39
+ '<li><input class="markdown-checkbox" type="checkbox" data-checkbox-index="0"></input> Item 0</li>' +
40
+ // tslint:disable-next-line:max-line-length
41
+ '<li><input class="markdown-checkbox" type="checkbox" checked="" data-checkbox-index="1"></input> Item 1</li>' +
42
+ // tslint:disable-next-line:max-line-length
43
+ '<li><input class="markdown-checkbox" type="checkbox" data-checkbox-index="2"></input> Item 2</li></ul>' ;
44
+ // in this test, we provide a SaveValue to the component.
45
+ comp . inpRenderedText = domSanitizer . bypassSecurityTrustHtml ( originalHTML ) ;
46
+ // this first detectChanges() updates the component that one of the @Inputs has changed.
47
+ fixture . detectChanges ( ) ;
48
+ // because of https://github.com/angular/angular/issues/9866, detectChanges() does not
49
+ // call ngOnChanges() on the component (yeah, it it as broken as it sounds). So
50
+ // we need to call the component manually to update.
51
+ comp . ngOnChanges ( {
52
+ inpRawText : { } as SimpleChange ,
53
+ inpRenderedText : { } as SimpleChange
54
+ } as SimpleChanges ) ;
55
+ // and because the test framework is not even able to detect inner changes to a component,
56
+ // we need to call detectChanges() again.
57
+ fixture . detectChanges ( ) ;
58
+ // also, using query() is also not working. Maybe due to the dynamic update of innerHTML.
59
+ // So we need to use the nativeElement to get a selector working.
60
+ // tslint:disable-next-line:max-line-length
61
+ let markdownPreview : Element = fixture . debugElement . nativeElement . querySelector ( '.markdown-rendered' ) ;
62
+ expect ( markdownPreview ) . not . toBeNull ( ) ;
63
+ // preview render of the template default
64
+ let markdownCheckboxElementList = markdownPreview . querySelectorAll ( '.markdown-checkbox' ) ;
65
+ expect ( markdownCheckboxElementList ) . not . toBeNull ( ) ;
66
+ expect ( markdownCheckboxElementList . length ) . toBe ( 3 ) ;
67
+ expect ( markdownCheckboxElementList [ 0 ] . hasAttribute ( 'checked' ) ) . toBeFalsy ( ) ;
68
+ expect ( markdownCheckboxElementList [ 1 ] . hasAttribute ( 'checked' ) ) . toBeTruthy ( ) ;
69
+ expect ( markdownCheckboxElementList [ 2 ] . hasAttribute ( 'checked' ) ) . toBeFalsy ( ) ;
70
+ // tick a checkbox
71
+ let checkboxElem = markdownCheckboxElementList [ 0 ] as HTMLElement ;
72
+ checkboxElem . click ( ) ;
73
+ // see if it ends up in the Markdown
74
+ expect ( comp . rawText . indexOf ( '[x] Item 1' ) ) . toBeGreaterThan ( - 1 ) ;
75
+ // tick another checkbox
76
+ checkboxElem = markdownCheckboxElementList [ 2 ] as HTMLElement ;
77
+ checkboxElem . click ( ) ;
78
+ // see if it ends up in the Markdown
79
+ expect ( comp . rawText . indexOf ( '[x] Item 3' ) ) . toBeGreaterThan ( - 1 ) ;
80
+ // untick a checkbox
81
+ checkboxElem = markdownCheckboxElementList [ 1 ] as HTMLElement ;
82
+ checkboxElem . click ( ) ;
83
+ // see if it ends up in the Markdown
84
+ expect ( comp . rawText . indexOf ( '[ ] Item 2' ) ) . toBeGreaterThan ( - 1 ) ;
85
+ } )
86
+ ) ;
87
+
88
+ fit ( 'should emit output onsave empty field when ' +
89
+ '`allowEmptySave` is false and the field is empty' , ( ) => {
90
+ spyOn ( comp . onSaveClick , 'emit' ) ;
91
+ comp . allowEmptySave = false ;
92
+ comp . fieldEmpty = true ;
93
+ comp . previousRawText = 'abc' ;
94
+ comp . rawText = 'xyz' ;
95
+ comp . saveClick ( ) ;
96
+ expect ( comp . onSaveClick . emit ) . not . toHaveBeenCalled ( ) ;
97
+ } ) ;
98
+
99
+ fit ( 'should emit output onsave empty field when `allowEmptySave` is true' , ( ) => {
100
+ spyOn ( comp . onSaveClick , 'emit' ) ;
101
+ comp . allowEmptySave = true ;
102
+ comp . fieldEmpty = true ;
103
+ comp . previousRawText = 'abc' ;
104
+ comp . rawText = 'xyz' ;
105
+ comp . saveClick ( ) ;
106
+ expect ( comp . onSaveClick . emit ) . toHaveBeenCalled ( ) ;
107
+ } ) ;
108
+
109
+ fit ( 'should emit output onsave empty field when ' +
110
+ '`allowEmptySave` is false and the field is not empty' , ( ) => {
111
+ spyOn ( comp . onSaveClick , 'emit' ) ;
112
+ comp . allowEmptySave = false ;
113
+ comp . fieldEmpty = false ;
114
+ comp . previousRawText = 'abc' ;
115
+ comp . rawText = 'xyz' ;
116
+ comp . saveClick ( ) ;
117
+ expect ( comp . onSaveClick . emit ) . toHaveBeenCalled ( ) ;
118
+ } ) ;
87
119
} ) ;
0 commit comments