Skip to content

Instantly share code, notes, and snippets.

@zackargyle
Created March 27, 2017 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackargyle/eaa93a9fc01e9ba510c999dcfdcf9ab4 to your computer and use it in GitHub Desktop.
Save zackargyle/eaa93a9fc01e9ba510c999dcfdcf9ab4 to your computer and use it in GitHub Desktop.
const makeServiceWorkerEnv = require('service-worker-mock');
const Response = () => ({ clone: jest.fn() });
const Request = () => ({ url: '/test' });
describe('Service worker', () => {
beforeEach(() => {
Object.assign(global, makeServiceWorkerEnv());
jest.resetModules();
});
it('should return a cached response', async () => {
global.fetch = jest.fn(() => Response());
require('../sw.js');
// Cache a request
const cachedResponse = Response();
const cache = await self.caches.open('TEST');
cache.put(Request(), cachedResponse);
// Verify the response is the cachedResponse
const response = await self.trigger('fetch', Request());
expect(response).toBe(cachedResponse);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment