@@ -27,6 +27,33 @@ describe('useScriptTag', () => {
27
27
expect ( scriptTagElement ( ) ) . toBeInstanceOf ( HTMLScriptElement )
28
28
} )
29
29
30
+ it ( 'should re-use the same src for multiple loads' , async ( ) => {
31
+ const addChildListener = vitest . spyOn ( document . head , 'appendChild' )
32
+
33
+ expect ( addChildListener ) . not . toBeCalled ( )
34
+
35
+ expect ( scriptTagElement ( ) ) . toBeNull ( )
36
+
37
+ const vm = useSetup ( ( ) => {
38
+ const script1 = useScriptTag ( src , ( ) => { } , { immediate : false , manual : true } )
39
+ const script2 = useScriptTag ( src , ( ) => { } , { immediate : false , manual : true } )
40
+
41
+ return {
42
+ script1,
43
+ script2,
44
+ }
45
+ } )
46
+
47
+ await vm . script1 . load ( false )
48
+ await vm . script2 . load ( false )
49
+
50
+ expect ( vm . script1 . scriptTag . value ) . not . toBeNull ( )
51
+ expect ( vm . script2 . scriptTag . value ) . not . toBeNull ( )
52
+
53
+ expect ( addChildListener ) . toBeCalledTimes ( 1 )
54
+ expect ( scriptTagElement ( ) ) . toBeInstanceOf ( HTMLScriptElement )
55
+ } )
56
+
30
57
it ( 'should remove script tag on unmount' , async ( ) => {
31
58
const removeChildListener = vitest . spyOn ( document . head , 'removeChild' )
32
59
@@ -90,4 +117,36 @@ describe('useScriptTag', () => {
90
117
91
118
expect ( vm . scriptTag ) . toBeNull ( )
92
119
} )
120
+
121
+ it ( 'should remove script tag on unload call after multiple loads' , async ( ) => {
122
+ const removeChildListener = vitest . spyOn ( document . head , 'removeChild' )
123
+
124
+ expect ( removeChildListener ) . not . toBeCalled ( )
125
+
126
+ expect ( scriptTagElement ( ) ) . toBeNull ( )
127
+
128
+ const vm = useSetup ( ( ) => {
129
+ const script1 = useScriptTag ( src , ( ) => { } , { immediate : false , manual : true } )
130
+ const script2 = useScriptTag ( src , ( ) => { } , { immediate : false , manual : true } )
131
+
132
+ return {
133
+ script1,
134
+ script2,
135
+ }
136
+ } )
137
+
138
+ // Multiple Loads
139
+ await vm . script1 . load ( false )
140
+ await vm . script2 . load ( false )
141
+
142
+ expect ( scriptTagElement ( ) ) . toBeInstanceOf ( HTMLScriptElement )
143
+
144
+ vm . script1 . unload ( )
145
+ vm . script2 . unload ( )
146
+
147
+ expect ( vm . script1 . scriptTag . value ) . toBeNull ( )
148
+ expect ( vm . script2 . scriptTag . value ) . toBeNull ( )
149
+ expect ( removeChildListener ) . toBeCalledTimes ( 1 )
150
+ expect ( scriptTagElement ( ) ) . toBeNull ( )
151
+ } )
93
152
} )
0 commit comments