Skip to content

Commit 792de4a

Browse files
author
Luis Sanchez
committed
fix(core): CHECKOUT-3012 Do not cache failed responses
1 parent 2b6c98b commit 792de4a

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/script-loader.spec.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,61 @@ describe('ScriptLoader', () => {
66

77
beforeEach(() => {
88
script = document.createElement('script');
9-
109
jest.spyOn(document, 'createElement').mockImplementation(() => script);
11-
jest.spyOn(document.body, 'appendChild').mockImplementation((element) =>
12-
element.onreadystatechange(new Event('readystatechange'))
13-
);
14-
1510
loader = new ScriptLoader();
1611
});
1712

1813
afterEach(() => {
1914
jest.restoreAllMocks();
2015
});
2116

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+
});
2423

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');
2826

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+
});
3130

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');
3433

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+
});
3936

40-
try {
37+
it('does not load same script twice', async () => {
4138
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+
});
4543
});
4644

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(() => {});
5062

51-
expect(document.body.appendChild).toHaveBeenCalledTimes(1);
63+
expect(document.body.appendChild).toHaveBeenCalledTimes(2);
64+
});
5265
});
5366
});

src/script-loader.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export default class ScriptLoader {
88

99
script.onload = (event) => resolve(event);
1010
script.onreadystatechange = (event) => resolve(event);
11-
script.onerror = (event) => reject(event);
11+
script.onerror = (event) => {
12+
delete this._scripts[src];
13+
reject(event);
14+
};
1215
script.async = true;
1316
script.src = src;
1417

0 commit comments

Comments
 (0)