Skip to content

Commit 142659e

Browse files
Alexandre Kirszenbergfacebook-github-bot
authored andcommitted
ServiceWorker-based Delta and HMR client
Reviewed By: mjesun Differential Revision: D10359356 fbshipit-source-id: 79346e488041fa1d14144c0897d6391c07c4d13f
1 parent 95441e8 commit 142659e

File tree

12 files changed

+1177
-0
lines changed

12 files changed

+1177
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+js_foundation
8+
* @format
9+
*/
10+
11+
/* eslint-env worker */
12+
13+
'use strict';
14+
15+
const stringToBundle = require('../stringToBundle');
16+
17+
const {getBundle, setBundle} = require('../bundleCache');
18+
const {Request, Response, Headers} = require('node-fetch');
19+
const {URL} = require('url');
20+
21+
jest.mock('../stringToBundle');
22+
23+
describe('bundleCache', () => {
24+
let putMock;
25+
let matchMock;
26+
beforeEach(() => {
27+
global.fetch = jest.fn();
28+
putMock = jest.fn();
29+
matchMock = jest.fn();
30+
31+
global.URL = URL;
32+
global.Response = Response;
33+
global.Request = Request;
34+
global.Headers = Headers;
35+
global.caches = {
36+
open: jest.fn().mockResolvedValue({
37+
put: putMock,
38+
match: matchMock,
39+
}),
40+
};
41+
});
42+
43+
describe('getBundle', () => {
44+
it('retrieves a bundle from the bundle cache', async () => {
45+
const bundle = {
46+
base: true,
47+
revisionId: 'revId',
48+
pre: 'pre',
49+
post: 'post',
50+
modules: [[0, '0'], [100, '100']],
51+
};
52+
const bundleReq = new Request('http://localhost/bundles/cool-bundle');
53+
matchMock.mockResolvedValue(new Response(JSON.stringify(bundle)));
54+
expect(await getBundle(bundleReq)).toEqual(bundle);
55+
expect(fetch).not.toHaveBeenCalled();
56+
expect(matchMock).toHaveBeenCalledWith(bundleReq);
57+
});
58+
59+
it('retrieves a bundle from the browser cache', async () => {
60+
const stringBundle = 'stringBundle';
61+
const bundle = {
62+
base: true,
63+
revisionId: 'revId',
64+
pre: 'pre',
65+
post: 'post',
66+
modules: [[0, '0'], [100, '100']],
67+
};
68+
const bundleReq = new Request('http://localhost/bundles/cool-bundle');
69+
matchMock.mockResolvedValue(null);
70+
fetch.mockResolvedValue(new Response(stringBundle));
71+
stringToBundle.mockReturnValue(bundle);
72+
expect(await getBundle(bundleReq)).toEqual(bundle);
73+
expect(fetch).toHaveBeenCalledWith(bundleReq, {cache: 'force-cache'});
74+
expect(stringToBundle).toHaveBeenCalledWith(stringBundle);
75+
});
76+
77+
it('returns null when a bundle cannot be found', async () => {
78+
matchMock.mockResolvedValue(null);
79+
fetch.mockResolvedValue(null);
80+
expect(await getBundle({})).toEqual(null);
81+
});
82+
});
83+
84+
describe('setBundle', () => {
85+
it('stores a bundle in the bundle cache', async () => {
86+
const bundle = {
87+
base: true,
88+
revisionId: 'revId',
89+
pre: 'pre',
90+
post: 'post',
91+
modules: [[0, '0'], [100, '100']],
92+
};
93+
const bundleReq = new Request('http://localhost/bundles/cool-bundle');
94+
await setBundle(bundleReq, bundle);
95+
const putCall = putMock.mock.calls[0];
96+
expect(putCall[0]).toBe(bundleReq);
97+
expect(await putCall[1].json()).toEqual(bundle);
98+
});
99+
});
100+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails oncall+js_foundation
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
const bundleToString = require('../bundleToString');
14+
15+
describe('bundleToString', () => {
16+
it('serializes a bundle into a plain JS bundle', () => {
17+
expect(
18+
bundleToString({
19+
base: true,
20+
revisionId: 'revisionId',
21+
pre: 'console.log("Hello World!");',
22+
post: 'console.log("That\'s all folks!");',
23+
modules: [[0, 'console.log("Best module.");']],
24+
}),
25+
).toMatchInlineSnapshot(`
26+
"console.log(\\"Hello World!\\");
27+
console.log(\\"Best module.\\");
28+
console.log(\\"That's all folks!\\");"
29+
`);
30+
});
31+
32+
it('modules are sorted by id', () => {
33+
expect(
34+
bundleToString({
35+
base: true,
36+
revisionId: 'revisionId',
37+
pre: 'console.log("Hello World!");',
38+
post: 'console.log("That\'s all folks!");',
39+
modules: [[3, '3'], [0, '0'], [2, '2'], [1, '1']],
40+
}),
41+
).toMatchInlineSnapshot(`
42+
"console.log(\\"Hello World!\\");
43+
0
44+
1
45+
2
46+
3
47+
console.log(\\"That's all folks!\\");"
48+
`);
49+
});
50+
});

0 commit comments

Comments
 (0)