@@ -6,48 +6,61 @@ describe('ScriptLoader', () => {
6
6
7
7
beforeEach ( ( ) => {
8
8
script = document . createElement ( 'script' ) ;
9
-
10
9
jest . spyOn ( document , 'createElement' ) . mockImplementation ( ( ) => script ) ;
11
- jest . spyOn ( document . body , 'appendChild' ) . mockImplementation ( ( element ) =>
12
- element . onreadystatechange ( new Event ( 'readystatechange' ) )
13
- ) ;
14
-
15
10
loader = new ScriptLoader ( ) ;
16
11
} ) ;
17
12
18
13
afterEach ( ( ) => {
19
14
jest . restoreAllMocks ( ) ;
20
15
} ) ;
21
16
22
- it ( 'attaches script tag to document' , async ( ) => {
23
- await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
17
+ describe ( 'when script succeeds to load' , ( ) => {
18
+ beforeEach ( ( ) => {
19
+ jest . spyOn ( document . body , 'appendChild' ) . mockImplementation ( ( element ) =>
20
+ setTimeout ( ( ) => element . onreadystatechange ( new Event ( 'readystatechange' ) ) , 0 )
21
+ ) ;
22
+ } ) ;
24
23
25
- expect ( document . body . appendChild ) . toHaveBeenCalledWith ( script ) ;
26
- expect ( script . src ) . toEqual ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
27
- } ) ;
24
+ it ( 'attaches script tag to document' , async ( ) => {
25
+ await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
28
26
29
- it ( 'resolves promise if script is loaded' , async ( ) => {
30
- const output = await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
27
+ expect ( document . body . appendChild ) . toHaveBeenCalledWith ( script ) ;
28
+ expect ( script . src ) . toEqual ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
29
+ } ) ;
31
30
32
- expect ( output ) . toBeInstanceOf ( Event ) ;
33
- } ) ;
31
+ it ( 'resolves promise if script is loaded' , async ( ) => {
32
+ const output = await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
34
33
35
- it ( 'rejects promise if script is not loaded' , async ( ) => {
36
- jest . spyOn ( document . body , 'appendChild' ) . mockImplementation (
37
- ( element ) => element . onerror ( new Event ( 'error' ) )
38
- ) ;
34
+ expect ( output ) . toBeInstanceOf ( Event ) ;
35
+ } ) ;
39
36
40
- try {
37
+ it ( 'does not load same script twice' , async ( ) => {
41
38
await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
42
- } catch ( output ) {
43
- expect ( output ) . toBeInstanceOf ( Event ) ;
44
- }
39
+ await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
40
+
41
+ expect ( document . body . appendChild ) . toHaveBeenCalledTimes ( 1 ) ;
42
+ } ) ;
45
43
} ) ;
46
44
47
- it ( 'does not load same script twice' , async ( ) => {
48
- await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
49
- await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' ) ;
45
+ describe ( 'when script fails to load' , ( ) => {
46
+ beforeEach ( ( ) => {
47
+ jest . spyOn ( document . body , 'appendChild' ) . mockImplementation ( element =>
48
+ setTimeout ( ( ) => element . onerror ( new Event ( 'error' ) ) , 0 )
49
+ ) ;
50
+ } ) ;
51
+
52
+ it ( 'rejects promise if script is not loaded' , async ( ) => {
53
+ await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' )
54
+ . catch ( error => expect ( error ) . toBeTruthy ( ) ) ;
55
+ } ) ;
56
+
57
+ it ( 'loads the script again' , async ( ) => {
58
+ await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' )
59
+ . catch ( ( ) => { } ) ;
60
+ await loader . loadScript ( 'https://code.jquery.com/jquery-3.2.1.min.js' )
61
+ . catch ( ( ) => { } ) ;
50
62
51
- expect ( document . body . appendChild ) . toHaveBeenCalledTimes ( 1 ) ;
63
+ expect ( document . body . appendChild ) . toHaveBeenCalledTimes ( 2 ) ;
64
+ } ) ;
52
65
} ) ;
53
66
} ) ;
0 commit comments