1414 * limitations under the License.
1515 */
1616
17- import { Loader , LoaderOptions } from "." ;
17+ import { DEFAULT_ID , Loader , LoaderOptions } from "." ;
18+
19+ jest . useFakeTimers ( ) ;
1820
1921afterEach ( ( ) => {
2022 document . getElementsByTagName ( "html" ) [ 0 ] . innerHTML = "" ;
@@ -54,6 +56,10 @@ test.each([
5456 expect ( loader . createUrl ( ) ) . toEqual ( expected ) ;
5557} ) ;
5658
59+ test ( "uses default id if empty string" , ( ) => {
60+ expect ( new Loader ( { apiKey : "foo" , id : "" } ) . id ) . toBe ( DEFAULT_ID ) ;
61+ } ) ;
62+
5763test ( "setScript adds a script to head with correct attributes" , ( ) => {
5864 const loader = new Loader ( { apiKey : "foo" } ) ;
5965
@@ -110,7 +116,7 @@ test("loadCallback callback should fire", () => {
110116} ) ;
111117
112118test ( "script onerror should reject promise" , async ( ) => {
113- const loader = new Loader ( { apiKey : "foo" } ) ;
119+ const loader = new Loader ( { apiKey : "foo" , retries : 0 } ) ;
114120
115121 const rejection = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
116122
@@ -119,11 +125,12 @@ test("script onerror should reject promise", async () => {
119125 await rejection ;
120126 expect ( loader [ "done" ] ) . toBeTruthy ( ) ;
121127 expect ( loader [ "loading" ] ) . toBeFalsy ( ) ;
128+ expect ( loader [ "errors" ] . length ) . toBe ( 1 ) ;
122129} ) ;
123130
124131test ( "script onerror should reject promise with multiple loaders" , async ( ) => {
125- const loader = new Loader ( { apiKey : "foo" } ) ;
126- const extraLoader = new Loader ( { apiKey : "foo" } ) ;
132+ const loader = new Loader ( { apiKey : "foo" , retries : 0 } ) ;
133+ const extraLoader = new Loader ( { apiKey : "foo" , retries : 0 } ) ;
127134
128135 let rejection = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
129136 loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
@@ -139,6 +146,25 @@ test("script onerror should reject promise with multiple loaders", async () => {
139146 expect ( extraLoader [ "loading" ] ) . toBeFalsy ( ) ;
140147} ) ;
141148
149+ test ( "script onerror should retry" , async ( ) => {
150+ const loader = new Loader ( { apiKey : "foo" , retries : 1 } ) ;
151+ const deleteScript = jest . spyOn ( loader , "deleteScript" ) ;
152+ const rejection = expect ( loader . load ( ) ) . rejects . toBeInstanceOf ( ErrorEvent ) ;
153+ // eslint-disable-next-line @typescript-eslint/no-empty-function
154+ console . log = jest . fn ( ) ;
155+
156+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
157+ loader [ "loadErrorCallback" ] ( document . createEvent ( "ErrorEvent" ) ) ;
158+ jest . runAllTimers ( ) ;
159+
160+ await rejection ;
161+ expect ( loader [ "done" ] ) . toBeTruthy ( ) ;
162+ expect ( loader [ "loading" ] ) . toBeFalsy ( ) ;
163+ expect ( loader [ "errors" ] . length ) . toBe ( 2 ) ;
164+ expect ( deleteScript ) . toHaveBeenCalledTimes ( 1 ) ;
165+ expect ( console . log ) . toHaveBeenCalledTimes ( loader . retries ) ;
166+ } ) ;
167+
142168test ( "singleton should be used" , ( ) => {
143169 const loader = new Loader ( { apiKey : "foo" } ) ;
144170 const extraLoader = new Loader ( { apiKey : "foo" } ) ;
@@ -195,3 +221,14 @@ test("loader should resolve immediately when google.maps defined", async () => {
195221 delete window . google ;
196222 expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
197223} ) ;
224+
225+ test ( "deleteScript removes script tag from head" , ( ) => {
226+ const loader = new Loader ( { apiKey : "foo" } ) ;
227+ loader [ "setScript" ] ( ) ;
228+ expect ( document . head . childNodes . length ) . toBe ( 1 ) ;
229+ loader . deleteScript ( ) ;
230+ expect ( document . head . childNodes . length ) . toBe ( 0 ) ;
231+ // should work without script existing
232+ loader . deleteScript ( ) ;
233+ expect ( document . head . childNodes . length ) . toBe ( 0 ) ;
234+ } ) ;
0 commit comments