-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ServiceWorker-based Delta and HMR client
Reviewed By: mjesun Differential Revision: D10359356 fbshipit-source-id: 79346e488041fa1d14144c0897d6391c07c4d13f
- Loading branch information
1 parent
95441e8
commit 142659e
Showing
12 changed files
with
1,177 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/metro/src/lib/bundle-modules/DeltaClient/__tests__/bundleCache-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/metro/src/lib/bundle-modules/DeltaClient/__tests__/bundleToString-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!\\");" | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.