|
| 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 | +}); |
0 commit comments