-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
ResourceFetchHandler.test.ts
69 lines (55 loc) · 1.92 KB
/
ResourceFetchHandler.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Window from '../../src/window/Window';
import IWindow from '../../src/window/IWindow';
import IDocument from '../../src/nodes/document/IDocument';
import ResourceFetchHandler from '../../src/fetch/ResourceFetchHandler';
import IResponse from '../../src/fetch/IResponse';
const MOCKED_SYNC_REQUEST = global['mockedModules']['sync-request'];
describe('ResourceFetchHandler', () => {
let window: IWindow;
let document: IDocument;
beforeEach(() => {
MOCKED_SYNC_REQUEST.options = null;
MOCKED_SYNC_REQUEST.statusCode = 200;
MOCKED_SYNC_REQUEST.body = 'test';
window = new Window();
document = window.document;
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('fetch()', () => {
it('Returns resource data asynchrounously.', async () => {
let fetchedURL = null;
jest.spyOn(window, 'fetch').mockImplementation((url: string) => {
fetchedURL = url;
return Promise.resolve(<IResponse>{
text: () => Promise.resolve('test'),
ok: true
});
});
const test = await ResourceFetchHandler.fetch(document, 'path/to/script/');
expect(fetchedURL).toBe('path/to/script/');
expect(test).toBe('test');
});
});
describe('fetchSync()', () => {
it('Returns resource data synchrounously.', () => {
window.location.href = 'https://localhost:8080/base/';
const test = ResourceFetchHandler.fetchSync(document, 'path/to/script/');
expect(MOCKED_SYNC_REQUEST.options).toEqual({
method: 'GET',
url: 'https://localhost:8080/base/path/to/script/'
});
expect(test).toBe('test');
});
it('Handles error when resource is fetched synchrounously.', () => {
window.location.href = 'https://localhost:8080/base/';
MOCKED_SYNC_REQUEST.statusCode = 404;
expect(() => {
ResourceFetchHandler.fetchSync(document, 'path/to/script/');
}).toThrowError(
'Failed to perform request to "https://localhost:8080/base/path/to/script/". Status code: 404'
);
});
});
});