@@ -3,16 +3,22 @@ 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' ;
6
7
7
8
describe ( 'ConfiguratorShowMoreComponent' , ( ) => {
8
9
let component : ConfiguratorShowMoreComponent ;
9
10
let fixture : ComponentFixture < ConfiguratorShowMoreComponent > ;
10
11
let htmlElem : HTMLElement ;
12
+ let sanitizerSpy : jasmine . SpyObj < DomSanitizer > ;
11
13
12
14
beforeEach ( waitForAsync ( ( ) => {
15
+ sanitizerSpy = jasmine . createSpyObj < DomSanitizer > ( 'DomSanitizer' , [
16
+ 'bypassSecurityTrustHtml' ,
17
+ ] ) ;
13
18
TestBed . configureTestingModule ( {
14
19
imports : [ I18nTestingModule ] ,
15
20
declarations : [ ConfiguratorShowMoreComponent ] ,
21
+ providers : [ { provide : DomSanitizer , useValue : sanitizerSpy } ] ,
16
22
} )
17
23
. overrideComponent ( ConfiguratorShowMoreComponent , {
18
24
set : {
@@ -35,79 +41,59 @@ describe('ConfiguratorShowMoreComponent', () => {
35
41
expect ( component ) . toBeTruthy ( ) ;
36
42
} ) ;
37
43
38
- it ( 'should render component' , ( ) => {
44
+ it ( 'should render component' , async ( ) => {
39
45
fixture . detectChanges ( ) ;
46
+ await fixture . whenStable ( ) ;
40
47
CommonConfiguratorTestUtilsService . expectElementPresent (
41
48
expect ,
42
49
htmlElem ,
43
50
'span'
44
51
) ;
45
- CommonConfiguratorTestUtilsService . expectElementPresent (
46
- expect ,
47
- htmlElem ,
48
- 'button'
49
- ) ;
50
- } ) ;
51
-
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 ) ) ;
57
52
} ) ;
58
53
59
- it ( 'should not set showMore after view init' , ( ) => {
60
- component . text = 'short text' ;
61
-
62
- component . ngAfterViewInit ( ) ;
63
- fixture . detectChanges ( ) ;
64
- CommonConfiguratorTestUtilsService . expectElementNotPresent (
65
- expect ,
66
- htmlElem ,
67
- 'button'
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>'
68
61
) ;
69
- expect ( component . showMore ) . toBe ( false ) ;
70
- expect ( component . textToShow ) . toBe ( component . text ) ;
62
+ expect ( result ) . toEqual ( 'Sanitized Text' ) ;
71
63
} ) ;
72
64
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 ) ;
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 ( '' ) ;
80
69
} ) ;
81
70
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' ;
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
+ } ) ;
88
76
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
- } ) ;
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' ) ;
81
+ } ) ;
98
82
99
- it ( 'should happen on view' , ( ) => {
100
- component . text = suspiciousTextWithFormatting ;
101
- component . ngAfterViewInit ( ) ;
102
- fixture . detectChanges ( ) ;
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' ) ;
89
+ } ) ;
103
90
104
- CommonConfiguratorTestUtilsService . expectElementToContainText (
105
- expect ,
106
- htmlElem ,
107
- 'span' ,
108
- sanitizedText
109
- ) ;
110
- } ) ;
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 ©' ) ;
111
97
} ) ;
112
98
113
99
describe ( 'Accessibility' , ( ) => {
0 commit comments