@@ -95,14 +95,10 @@ describe('Sanitizer API backend', () => {
9595 let setElementContentSanitizer
9696 let setSanitizerConfigSanitizer
9797 let mockSanitizerConstructor
98- let capturedSanitizerConfig
9998 let mockSetHTML
10099
101100 beforeAll ( ( ) => {
102- // Capture the config passed to the Sanitizer constructor
103- capturedSanitizerConfig = null
104- mockSanitizerConstructor = jest . fn ( function ( config ) {
105- capturedSanitizerConfig = config
101+ mockSanitizerConstructor = jest . fn ( function ( ) {
106102 return this
107103 } )
108104 window . Sanitizer = mockSanitizerConstructor
@@ -130,21 +126,20 @@ describe('Sanitizer API backend', () => {
130126 } )
131127
132128 test ( 'instantiates Sanitizer with a form element allowlist' , ( ) => {
133- expect ( mockSanitizerConstructor ) . toHaveBeenCalledTimes ( 1 )
134- const elements = capturedSanitizerConfig . elements
135- expect ( elements ) . toContain ( 'input' )
136- expect ( elements ) . toContain ( 'select' )
137- expect ( elements ) . toContain ( 'textarea' )
138- expect ( elements ) . toContain ( 'label' )
139- expect ( elements ) . toContain ( 'button' )
129+ const config = mockSanitizerConstructor . mock . calls [ 0 ] [ 0 ]
130+ expect ( config . elements ) . toContain ( 'input' )
131+ expect ( config . elements ) . toContain ( 'select' )
132+ expect ( config . elements ) . toContain ( 'textarea' )
133+ expect ( config . elements ) . toContain ( 'label' )
134+ expect ( config . elements ) . toContain ( 'button' )
140135 } )
141136
142137 test ( 'instantiates Sanitizer with a form attribute allowlist' , ( ) => {
143- const attributes = capturedSanitizerConfig . attributes
144- expect ( attributes ) . toContain ( 'type' )
145- expect ( attributes ) . toContain ( 'name' )
146- expect ( attributes ) . toContain ( 'value' )
147- expect ( attributes ) . toContain ( 'checked' )
138+ const config = mockSanitizerConstructor . mock . calls [ 0 ] [ 0 ]
139+ expect ( config . attributes ) . toContain ( 'type' )
140+ expect ( config . attributes ) . toContain ( 'name' )
141+ expect ( config . attributes ) . toContain ( 'value' )
142+ expect ( config . attributes ) . toContain ( 'checked' )
148143 } )
149144
150145 test ( 'uses setHTML when the Sanitizer backend is active' , ( ) => {
@@ -164,35 +159,36 @@ describe('Sanitizer API backend', () => {
164159 expect ( el . innerHTML ) . toContain ( '<label' )
165160 } )
166161
167- test ( 'falls back to false when Sanitizer constructor throws' , ( ) => {
168- // Temporarily replace Sanitizer with one that throws
169- const throwingSanitizer = jest . fn ( ( ) => {
170- throw new Error ( 'Unsupported config' )
162+ describe ( 'when Sanitizer constructor throws' , ( ) => {
163+ let setElementContentLocal
164+
165+ beforeAll ( ( ) => {
166+ window . Sanitizer = jest . fn ( function ( ) {
167+ throw new Error ( 'Unsupported config' )
168+ } )
169+ jest . resetModules ( )
170+ const mod = require ( './../src/js/sanitizer.js' )
171+ setElementContentLocal = mod . setElementContent
172+ mod . setSanitizerConfig ( { backendOrder : [ 'sanitizer' ] } )
171173 } )
172- window . Sanitizer = throwingSanitizer
173- jest . resetModules ( )
174- const mod = require ( './../src/js/sanitizer.js' )
175- const setSanitizerConfigLocal = mod . setSanitizerConfig
176- const setElementContentLocal = mod . setElementContent
177- setSanitizerConfigLocal ( { backendOrder : [ 'sanitizer' ] } )
178174
179- // With sanitizer backend returning false, setHTML should NOT be called
180- const freshSetHTML = jest . fn ( function ( content ) {
181- this . innerHTML = content
175+ afterAll ( ( ) => {
176+ // Restore the outer mock so the outer afterAll clean-up is consistent
177+ window . Sanitizer = mockSanitizerConstructor
178+ jest . resetModules ( )
182179 } )
183- Element . prototype . setHTML = freshSetHTML
184- const el = document . createElement ( 'div' )
185- setElementContentLocal ( el , '<p>test</p>' , false )
186- expect ( freshSetHTML ) . not . toHaveBeenCalled ( )
187180
188- // Restore for subsequent tests and re-require so module state is consistent
189- window . Sanitizer = mockSanitizerConstructor
190- Element . prototype . setHTML = mockSetHTML
191- jest . resetModules ( )
192- const restoredMod = require ( './../src/js/sanitizer.js' )
193- setElementContentSanitizer = restoredMod . setElementContent
194- setSanitizerConfigSanitizer = restoredMod . setSanitizerConfig
195- setSanitizerConfigSanitizer ( { backendOrder : [ 'sanitizer' ] } )
181+ test ( 'falls back to false and does not call setHTML' , ( ) => {
182+ const freshSetHTML = jest . fn ( function ( content ) {
183+ this . innerHTML = content
184+ } )
185+ Element . prototype . setHTML = freshSetHTML
186+ const el = document . createElement ( 'div' )
187+ setElementContentLocal ( el , '<p>test</p>' , false )
188+ expect ( freshSetHTML ) . not . toHaveBeenCalled ( )
189+ // Restore prototype for outer describe clean-up
190+ Element . prototype . setHTML = mockSetHTML
191+ } )
196192 } )
197193} )
198194
0 commit comments