Skip to content

Commit

Permalink
ServiceWorker-based Delta and HMR client
Browse files Browse the repository at this point in the history
Reviewed By: mjesun

Differential Revision: D10359356

fbshipit-source-id: 79346e488041fa1d14144c0897d6391c07c4d13f
  • Loading branch information
Alexandre Kirszenberg authored and facebook-github-bot committed Oct 29, 2018
1 parent 95441e8 commit 142659e
Show file tree
Hide file tree
Showing 12 changed files with 1,177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+js_foundation
* @format
*/

/* eslint-env worker */

'use strict';

const stringToBundle = require('../stringToBundle');

const {getBundle, setBundle} = require('../bundleCache');
const {Request, Response, Headers} = require('node-fetch');
const {URL} = require('url');

jest.mock('../stringToBundle');

describe('bundleCache', () => {
let putMock;
let matchMock;
beforeEach(() => {
global.fetch = jest.fn();
putMock = jest.fn();
matchMock = jest.fn();

global.URL = URL;
global.Response = Response;
global.Request = Request;
global.Headers = Headers;
global.caches = {
open: jest.fn().mockResolvedValue({
put: putMock,
match: matchMock,
}),
};
});

describe('getBundle', () => {
it('retrieves a bundle from the bundle cache', async () => {
const bundle = {
base: true,
revisionId: 'revId',
pre: 'pre',
post: 'post',
modules: [[0, '0'], [100, '100']],
};
const bundleReq = new Request('http://localhost/bundles/cool-bundle');
matchMock.mockResolvedValue(new Response(JSON.stringify(bundle)));
expect(await getBundle(bundleReq)).toEqual(bundle);
expect(fetch).not.toHaveBeenCalled();
expect(matchMock).toHaveBeenCalledWith(bundleReq);
});

it('retrieves a bundle from the browser cache', async () => {
const stringBundle = 'stringBundle';
const bundle = {
base: true,
revisionId: 'revId',
pre: 'pre',
post: 'post',
modules: [[0, '0'], [100, '100']],
};
const bundleReq = new Request('http://localhost/bundles/cool-bundle');
matchMock.mockResolvedValue(null);
fetch.mockResolvedValue(new Response(stringBundle));
stringToBundle.mockReturnValue(bundle);
expect(await getBundle(bundleReq)).toEqual(bundle);
expect(fetch).toHaveBeenCalledWith(bundleReq, {cache: 'force-cache'});
expect(stringToBundle).toHaveBeenCalledWith(stringBundle);
});

it('returns null when a bundle cannot be found', async () => {
matchMock.mockResolvedValue(null);
fetch.mockResolvedValue(null);
expect(await getBundle({})).toEqual(null);
});
});

describe('setBundle', () => {
it('stores a bundle in the bundle cache', async () => {
const bundle = {
base: true,
revisionId: 'revId',
pre: 'pre',
post: 'post',
modules: [[0, '0'], [100, '100']],
};
const bundleReq = new Request('http://localhost/bundles/cool-bundle');
await setBundle(bundleReq, bundle);
const putCall = putMock.mock.calls[0];
expect(putCall[0]).toBe(bundleReq);
expect(await putCall[1].json()).toEqual(bundle);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+js_foundation
* @format
*/

'use strict';

const bundleToString = require('../bundleToString');

describe('bundleToString', () => {
it('serializes a bundle into a plain JS bundle', () => {
expect(
bundleToString({
base: true,
revisionId: 'revisionId',
pre: 'console.log("Hello World!");',
post: 'console.log("That\'s all folks!");',
modules: [[0, 'console.log("Best module.");']],
}),
).toMatchInlineSnapshot(`
"console.log(\\"Hello World!\\");
console.log(\\"Best module.\\");
console.log(\\"That's all folks!\\");"
`);
});

it('modules are sorted by id', () => {
expect(
bundleToString({
base: true,
revisionId: 'revisionId',
pre: 'console.log("Hello World!");',
post: 'console.log("That\'s all folks!");',
modules: [[3, '3'], [0, '0'], [2, '2'], [1, '1']],
}),
).toMatchInlineSnapshot(`
"console.log(\\"Hello World!\\");
0
1
2
3
console.log(\\"That's all folks!\\");"
`);
});
});
Loading

0 comments on commit 142659e

Please sign in to comment.