@@ -83,4 +83,61 @@ describe('StylesheetLoader', () => {
8383 . toHaveBeenCalledTimes ( 2 ) ;
8484 } ) ;
8585 } ) ;
86+
87+ describe ( 'when preloading stylesheet' , ( ) => {
88+ let preloadedStylesheet : HTMLLinkElement ;
89+
90+ beforeEach ( ( ) => {
91+ preloadedStylesheet = document . createElement ( 'link' ) ;
92+
93+ jest . spyOn ( document , 'createElement' )
94+ . mockImplementation ( ( ) => preloadedStylesheet ) ;
95+
96+ jest . spyOn ( document . head , 'appendChild' )
97+ . mockImplementation ( element =>
98+ setTimeout ( ( ) => element . onload ( new Event ( 'load' ) ) , 0 )
99+ ) ;
100+ } ) ;
101+
102+ it ( 'attaches preload link tag to document' , async ( ) => {
103+ await loader . preloadStylesheet ( 'https://foo.bar/hello-world.css' ) ;
104+
105+ expect ( document . head . appendChild )
106+ . toHaveBeenCalledWith ( preloadedStylesheet ) ;
107+
108+ expect ( preloadedStylesheet . rel )
109+ . toEqual ( 'preload' ) ;
110+
111+ expect ( preloadedStylesheet . as )
112+ . toEqual ( 'style' ) ;
113+
114+ expect ( preloadedStylesheet . href )
115+ . toEqual ( 'https://foo.bar/hello-world.css' ) ;
116+ } ) ;
117+
118+ it ( 'prefetches stylesheet if option is provided' , async ( ) => {
119+ await loader . preloadStylesheet ( 'https://foo.bar/hello-world.css' , {
120+ prefetch : true ,
121+ } ) ;
122+
123+ expect ( document . head . appendChild )
124+ . toHaveBeenCalledWith ( preloadedStylesheet ) ;
125+
126+ expect ( preloadedStylesheet . rel )
127+ . toEqual ( 'prefetch' ) ;
128+
129+ expect ( preloadedStylesheet . as )
130+ . toEqual ( 'style' ) ;
131+
132+ expect ( preloadedStylesheet . href )
133+ . toEqual ( 'https://foo.bar/hello-world.css' ) ;
134+ } ) ;
135+
136+ it ( 'resolves promise if stylesheet is preloaded' , async ( ) => {
137+ const output = await loader . preloadStylesheet ( 'https://foo.bar/hello-world.css' ) ;
138+
139+ expect ( output )
140+ . toBeInstanceOf ( Event ) ;
141+ } ) ;
142+ } ) ;
86143} ) ;
0 commit comments