@@ -130,6 +130,47 @@ describe('dynamicCSS', () => {
130130 expect ( document . querySelector ( 'style' ) ) . toBeFalsy ( ) ;
131131 } ) ;
132132
133+ it ( 'can remove styles while the default container is unavailable' , ( ) => {
134+ const { head, body } = document ;
135+ const style = updateCSS ( TEST_STYLE , 'detached' ) ;
136+ head . remove ( ) ;
137+ body . remove ( ) ;
138+ try {
139+ expect ( ( ) => removeCSS ( 'detached' ) ) . not . toThrow ( ) ;
140+ expect ( ( ) => removeCSS ( 'detached' , { styles : [ style ] } ) ) . not . toThrow ( ) ;
141+ expect ( style . parentNode ) . toBe ( head ) ;
142+ } finally {
143+ document . documentElement . append ( head , body ) ;
144+ removeCSS ( 'detached' ) ;
145+ clearContainerCache ( ) ;
146+ }
147+ } ) ;
148+
149+ it ( 'returns null when the document containers disappear during style creation' , ( ) => {
150+ clearContainerCache ( ) ;
151+ const { head, body } = document ;
152+ const createElement = document . createElement . bind ( document ) ;
153+ const spy = jest
154+ . spyOn ( document , 'createElement' )
155+ . mockImplementationOnce ( tagName => {
156+ head . remove ( ) ;
157+ body . remove ( ) ;
158+ return createElement ( tagName ) ;
159+ } ) ;
160+ try {
161+ expect ( updateCSS ( TEST_STYLE , 'transient' ) ) . toBeNull ( ) ;
162+ expect ( head . querySelector ( 'style' ) ) . toBeNull ( ) ;
163+ } finally {
164+ spy . mockRestore ( ) ;
165+ document . documentElement . append ( head , body ) ;
166+ clearContainerCache ( ) ;
167+ }
168+ const restored = updateCSS ( TEST_STYLE , 'transient' ) ;
169+ expect ( restored . parentNode ) . toBe ( head ) ;
170+ removeCSS ( 'transient' ) ;
171+ clearContainerCache ( ) ;
172+ } ) ;
173+
133174 describe ( 'updateCSS' , ( ) => {
134175 beforeEach ( ( ) => {
135176 updateCSS ( TEST_STYLE , 'unique' ) ;
0 commit comments