@@ -37,7 +37,14 @@ describe('StylesheetLoader', () => {
3737 beforeEach ( ( ) => {
3838 jest . spyOn ( document . head , 'appendChild' )
3939 . mockImplementation ( element => {
40- setTimeout ( ( ) => ( element as HTMLElement ) . onload ! ( new Event ( 'load' ) ) , 0 ) ;
40+ setTimeout ( ( ) => {
41+ const onload = ( element as HTMLElement & {
42+ onload ?( this : HTMLElement , ev : Event ) : any ;
43+ } ) . onload ;
44+ if ( typeof onload === 'function' ) {
45+ onload . call ( element as HTMLElement , new Event ( 'load' ) ) ;
46+ }
47+ } , 0 ) ;
4148
4249 return element ;
4350 } ) ;
@@ -73,19 +80,26 @@ describe('StylesheetLoader', () => {
7380 'https://foo.bar/hello-world.css' ,
7481 { prepend : true , attributes : { 'data-attribute1' : '1' , 'data-attribute2' : '2' } } ) ;
7582
76- expect ( stylesheet . attributes . getNamedItem ( 'data-attribute1' ) ! . value )
77- . toEqual ( '1' ) ;
83+ const attr1 = stylesheet . attributes . getNamedItem ( 'data-attribute1' ) ;
84+ expect ( attr1 ) . not . toBeNull ( ) ;
85+ expect ( attr1 ?. value ) . toEqual ( '1' ) ;
7886
79- expect ( stylesheet . attributes . getNamedItem ( 'data-attribute2' ) ! . value )
80- . toEqual ( '2' ) ;
87+ const attr2 = stylesheet . attributes . getNamedItem ( 'data-attribute2' ) ;
88+ expect ( attr2 ) . not . toBeNull ( ) ;
89+ expect ( attr2 ?. value ) . toEqual ( '2' ) ;
8190 } ) ;
8291 } ) ;
8392
8493 describe ( 'when stylesheet fails to load' , ( ) => {
8594 beforeEach ( ( ) => {
8695 jest . spyOn ( document . head , 'appendChild' )
8796 . mockImplementation ( element => {
88- setTimeout ( ( ) => ( element as HTMLElement ) . onerror ! ( new Event ( 'error' ) ) , 0 ) ;
97+ setTimeout ( ( ) => {
98+ const onerror = ( element as HTMLElement ) . onerror ;
99+ if ( typeof onerror === 'function' ) {
100+ onerror ( new Event ( 'error' ) ) ;
101+ }
102+ } , 0 ) ;
89103
90104 return element ;
91105 } ) ;
@@ -127,7 +141,9 @@ describe('StylesheetLoader', () => {
127141
128142 jest . spyOn ( document . head , 'appendChild' )
129143 . mockImplementation ( element => {
130- setTimeout ( ( ) => ( element as HTMLElement ) . onload ! ( new Event ( 'load' ) ) , 0 ) ;
144+ setTimeout ( ( ) => {
145+ element . dispatchEvent ( new Event ( 'load' ) ) ;
146+ } , 0 ) ;
131147
132148 return element ;
133149 } ) ;
0 commit comments