@@ -3,22 +3,16 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
3
3
import { I18nTestingModule } from '@spartacus/core' ;
4
4
import { CommonConfiguratorTestUtilsService } from '../../../common/testing/common-configurator-test-utils.service' ;
5
5
import { ConfiguratorShowMoreComponent } from './configurator-show-more.component' ;
6
- import { DomSanitizer } from '@angular/platform-browser' ;
7
6
8
7
describe ( 'ConfiguratorShowMoreComponent' , ( ) => {
9
8
let component : ConfiguratorShowMoreComponent ;
10
9
let fixture : ComponentFixture < ConfiguratorShowMoreComponent > ;
11
10
let htmlElem : HTMLElement ;
12
- let sanitizerSpy : jasmine . SpyObj < DomSanitizer > ;
13
11
14
12
beforeEach ( waitForAsync ( ( ) => {
15
- sanitizerSpy = jasmine . createSpyObj < DomSanitizer > ( 'DomSanitizer' , [
16
- 'bypassSecurityTrustHtml' ,
17
- ] ) ;
18
13
TestBed . configureTestingModule ( {
19
14
imports : [ I18nTestingModule ] ,
20
15
declarations : [ ConfiguratorShowMoreComponent ] ,
21
- providers : [ { provide : DomSanitizer , useValue : sanitizerSpy } ] ,
22
16
} )
23
17
. overrideComponent ( ConfiguratorShowMoreComponent , {
24
18
set : {
@@ -41,59 +35,79 @@ describe('ConfiguratorShowMoreComponent', () => {
41
35
expect ( component ) . toBeTruthy ( ) ;
42
36
} ) ;
43
37
44
- it ( 'should render component' , async ( ) => {
38
+ it ( 'should render component' , ( ) => {
45
39
fixture . detectChanges ( ) ;
46
- await fixture . whenStable ( ) ;
47
40
CommonConfiguratorTestUtilsService . expectElementPresent (
48
41
expect ,
49
42
htmlElem ,
50
43
'span'
51
44
) ;
52
- } ) ;
53
-
54
- it ( 'should remove HTML tags from input text' , ( ) => {
55
- sanitizerSpy . bypassSecurityTrustHtml . and . returnValue (
56
- 'Sanitized Text' as any
57
- ) ; // Fake SafeHtml
58
- const result = component . normalize ( '<b>Sanitized Text</b>' ) ;
59
- expect ( sanitizerSpy . bypassSecurityTrustHtml ) . toHaveBeenCalledWith (
60
- '<b>Sanitized Text</b>'
45
+ CommonConfiguratorTestUtilsService . expectElementPresent (
46
+ expect ,
47
+ htmlElem ,
48
+ 'button'
61
49
) ;
62
- expect ( result ) . toEqual ( 'Sanitized Text' ) ;
63
50
} ) ;
64
51
65
- it ( 'should return an empty string when input is null' , ( ) => {
66
- sanitizerSpy . bypassSecurityTrustHtml . and . returnValue ( null ) ;
67
- const result = component . normalize ( null as unknown as string ) ;
68
- expect ( result ) . toEqual ( '' ) ;
52
+ it ( 'should set showMore after view init' , ( ) => {
53
+ component . ngAfterViewInit ( ) ;
54
+ fixture . detectChanges ( ) ;
55
+ expect ( component . showMore ) . toBe ( true ) ;
56
+ expect ( component . textToShow ) . toBe ( component . text . substring ( 0 , 60 ) ) ;
69
57
} ) ;
70
58
71
- it ( 'should return an empty string when input is undefined' , ( ) => {
72
- sanitizerSpy . bypassSecurityTrustHtml . and . returnValue ( undefined ) ;
73
- const result = component . normalize ( undefined as unknown as string ) ;
74
- expect ( result ) . toEqual ( '' ) ;
75
- } ) ;
59
+ it ( 'should not set showMore after view init' , ( ) => {
60
+ component . text = 'short text' ;
76
61
77
- it ( 'should return the same text if there are no HTML elements' , ( ) => {
78
- sanitizerSpy . bypassSecurityTrustHtml . and . returnValue ( 'Plain Text' as any ) ;
79
- const result = component . normalize ( 'Plain Text' ) ;
80
- expect ( result ) . toEqual ( 'Plain Text' ) ;
62
+ component . ngAfterViewInit ( ) ;
63
+ fixture . detectChanges ( ) ;
64
+ CommonConfiguratorTestUtilsService . expectElementNotPresent (
65
+ expect ,
66
+ htmlElem ,
67
+ 'button'
68
+ ) ;
69
+ expect ( component . showMore ) . toBe ( false ) ;
70
+ expect ( component . textToShow ) . toBe ( component . text ) ;
81
71
} ) ;
82
72
83
- it ( 'should remove script tags to prevent XSS' , ( ) => {
84
- sanitizerSpy . bypassSecurityTrustHtml . and . returnValue ( 'Safe Content' as any ) ;
85
- const result = component . normalize (
86
- '<script>alert("XSS")</script>Safe Content'
87
- ) ;
88
- expect ( result ) . toEqual ( 'Safe Content' ) ;
73
+ it ( 'should set showHiddenText after toggleShowMore action' , ( ) => {
74
+ fixture . detectChanges ( ) ;
75
+ component . ngAfterViewInit ( ) ;
76
+ component . toggleShowMore ( ) ;
77
+ fixture . detectChanges ( ) ;
78
+ expect ( component . showHiddenText ) . toBe ( true ) ;
79
+ expect ( component . textToShow ) . toBe ( component . text ) ;
89
80
} ) ;
90
81
91
- it ( 'should handle special characters properly' , ( ) => {
92
- sanitizerSpy . bypassSecurityTrustHtml . and . returnValue (
93
- 'Text & Special Chars ©' as any
94
- ) ;
95
- const result = component . normalize ( 'Text & Special Chars ©' ) ;
96
- expect ( result ) . toEqual ( 'Text & Special Chars ©' ) ;
82
+ describe ( 'Sanitization of suspicious input' , ( ) => {
83
+ const suspiciousTextWithFormatting =
84
+ '<h1>Digital camera</h1> is a great product <p> <script' ;
85
+ const suspiciousTextWithoutFormatting =
86
+ 'Digital camera is a great product <script' ;
87
+ const sanitizedText = 'Digital camera is a great product' ;
88
+
89
+ it ( 'does not happen through method normalize because that is meant for removing HTML tags for better readibility' , ( ) => {
90
+ component . text = suspiciousTextWithFormatting ;
91
+ component . ngAfterViewInit ( ) ;
92
+ fixture . detectChanges ( ) ;
93
+ expect ( component . textNormalized ) . toBe ( suspiciousTextWithoutFormatting ) ;
94
+ expect ( component [ 'normalize' ] ( suspiciousTextWithFormatting ) ) . toBe (
95
+ suspiciousTextWithoutFormatting
96
+ ) ;
97
+ } ) ;
98
+
99
+ it ( 'should happen on view' , ( ) => {
100
+ component . text = suspiciousTextWithFormatting ;
101
+ component . ngAfterViewInit ( ) ;
102
+ fixture . detectChanges ( ) ;
103
+
104
+ CommonConfiguratorTestUtilsService . expectElementToContainText (
105
+ expect ,
106
+ htmlElem ,
107
+ 'span' ,
108
+ sanitizedText
109
+ ) ;
110
+ } ) ;
97
111
} ) ;
98
112
99
113
describe ( 'Accessibility' , ( ) => {
0 commit comments