Skip to content

Commit

Permalink
fix(cache/name): remove inconsistent references
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Esquire authored Jun 12, 2020
1 parent 6899ede commit 56d1289
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 50 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"extends": [
"amex/test",
"plugin:prettier/recommended"
]
],
"rules": {
"jest/no-large-snapshots": "off"
}
},
{
"files": [
Expand Down
107 changes: 106 additions & 1 deletion __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,108 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`library consistently exports 1`] = `"on :: function once :: function off :: function emit :: function emitter :: function eventListeners :: object calls :: object createMiddleware :: function createMiddlewareContext :: function createMiddlewareFactory :: function TYPES :: object ENUMS :: object expectedType :: function expectedArrayOfType :: function enumerableException :: function unknownKey :: function unknownEventName :: function validateInput :: function getCacheOptions :: function OneServiceWorkerError :: function errorFactory :: function notEnabled :: function notSupported :: function failedToInstall :: function failure :: function configure :: function getConfig :: function isDevelopment :: function isEventsEnabled :: function isNonStandardEnabled :: function isNavigationPreloadEnabled :: function isServiceWorker :: function isServiceWorkerSupported :: function isNotificationSupported :: function isPushSupported :: function isBackgroundSyncSupported :: function isCacheStorageSupported :: function isIndexedDBSupported :: function isPermissionsSupported :: function isOffline :: function getRegistration :: function getRegistrations :: function register :: function getWorker :: function postMessage :: function update :: function unregister :: function escapeHatch :: function showNotification :: function getNotifications :: function getSubscription :: function subscribe :: function unsubscribe :: function registerTag :: function getTags :: function urlBase64ToUint8Array :: function defaultCacheName :: string defaultCacheOptions :: object normalizeRequest :: function open :: function has :: function keys :: function match :: function matchAll :: function add :: function addAll :: function put :: function remove :: function removeAll :: function entries :: function clear :: function metaDataCacheName :: string createCacheName :: function createCacheEntryName :: function createMetaRequest :: function createMetaResponse :: function getMetaStore :: function getMetaData :: function setMetaData :: function deleteMetaData :: function onInstall :: function onActivate :: function onMessage :: function onPush :: function onSync :: function onFetch :: function cacheBusting :: function cacheRouter :: function cacheStrategy :: function clientsClaim :: function escapeHatchRoute :: function manifest :: function messageContext :: function messenger :: function navigationPreloadActivation :: function navigationPreloadResponse :: function appShell :: function precache :: function skipWaiting :: function expiration :: function"`;
exports[`library consistently exports 1`] = `
"on :: function
once :: function
off :: function
emit :: function
emitter :: function
eventListeners :: object
calls :: object
createMiddleware :: function
createMiddlewareContext :: function
createMiddlewareFactory :: function
TYPES :: object
ENUMS :: object
expectedType :: function
expectedArrayOfType :: function
enumerableException :: function
unknownKey :: function
unknownEventName :: function
validateInput :: function
getCacheOptions :: function
OneServiceWorkerError :: function
errorFactory :: function
notEnabled :: function
notSupported :: function
failedToInstall :: function
failure :: function
configure :: function
getConfig :: function
isDevelopment :: function
isEventsEnabled :: function
isNonStandardEnabled :: function
isNavigationPreloadEnabled :: function
isServiceWorker :: function
isServiceWorkerSupported :: function
isNotificationSupported :: function
isPushSupported :: function
isBackgroundSyncSupported :: function
isCacheStorageSupported :: function
isIndexedDBSupported :: function
isPermissionsSupported :: function
isOffline :: function
getRegistration :: function
getRegistrations :: function
register :: function
getWorker :: function
postMessage :: function
update :: function
unregister :: function
escapeHatch :: function
showNotification :: function
getNotifications :: function
getSubscription :: function
subscribe :: function
unsubscribe :: function
registerTag :: function
getTags :: function
urlBase64ToUint8Array :: function
cachePrefix :: string
cacheDelimiter :: string
defaultCacheName :: string
defaultCacheOptions :: object
createCacheName :: function
normalizeRequest :: function
open :: function
has :: function
keys :: function
match :: function
matchAll :: function
add :: function
addAll :: function
put :: function
remove :: function
removeAll :: function
entries :: function
clear :: function
metaDataCacheName :: string
createMetaCacheName :: function
createMetaCacheEntryName :: function
createMetaRequest :: function
createMetaResponse :: function
getMetaStore :: function
getMetaData :: function
setMetaData :: function
deleteMetaData :: function
createCacheEntryName :: function
onInstall :: function
onActivate :: function
onMessage :: function
onPush :: function
onSync :: function
onFetch :: function
cacheBusting :: function
cacheRouter :: function
cacheStrategy :: function
clientsClaim :: function
escapeHatchRoute :: function
manifest :: function
messageContext :: function
messenger :: function
navigationPreloadActivation :: function
navigationPreloadResponse :: function
appShell :: function
precache :: function
skipWaiting :: function
expiration :: function"
`;
12 changes: 8 additions & 4 deletions __tests__/cache/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cache consistently exports 1`] = `
"defaultCacheName :: string
"cachePrefix :: string
cacheDelimiter :: string
defaultCacheName :: string
defaultCacheOptions :: object
createCacheName :: function
normalizeRequest :: function
open :: function
has :: function
Expand All @@ -17,12 +20,13 @@ removeAll :: function
entries :: function
clear :: function
metaDataCacheName :: string
createCacheName :: function
createCacheEntryName :: function
createMetaCacheName :: function
createMetaCacheEntryName :: function
createMetaRequest :: function
createMetaResponse :: function
getMetaStore :: function
getMetaData :: function
setMetaData :: function
deleteMetaData :: function"
deleteMetaData :: function
createCacheEntryName :: function"
`;
34 changes: 29 additions & 5 deletions __tests__/cache/cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
removeAll,
clear,
normalizeRequest,
defaultCacheName,
createCacheName,
cacheDelimiter,
cachePrefix,
defaultCacheOptions,
} from '../../src/cache/cache';

const cacheName = 'my-cache';
Expand Down Expand Up @@ -61,7 +64,7 @@ describe('playground', () => {
// would match
await expect(match(html)).resolves.toEqual(expect.any(Response));
// would not match different cache name
await expect(match(html, { cacheName: 'one-cache' })).resolves.toBe(null);
await expect(match(html, { cacheName: createCacheName() })).resolves.toBe(null);
// matches the newly added response with cache specified
await expect(match(html, { cacheName })).resolves.toEqual(expect.any(Response));
// returns only the single html we added
Expand Down Expand Up @@ -113,6 +116,25 @@ describe('playground', () => {
});
});

describe('createCacheName', () => {
test('returns a string from a given cacheName', async () => {
expect.assertions(1);

const myCacheName = 'my-cache';
expect(createCacheName(myCacheName)).toEqual(
[cachePrefix, cacheDelimiter, myCacheName].join(''),
);
});
});

describe('normalizeRequest', () => {
test('returns a request with the string url passed in', async () => {
expect.assertions(1);

expect(normalizeRequest('/index.js')).toEqual(new Request('/index.js'));
});
});

describe('open', () => {
test('open defaults to new cache with the default name', async () => {
expect.assertions(3);
Expand Down Expand Up @@ -144,7 +166,7 @@ describe('has', () => {
});

describe('keys', () => {
test('retturns all the keys in the cache', async () => {
test('returns all the keys in the cache', async () => {
expect.assertions(3);

await expect(keys()).resolves.toHaveLength(0);
Expand All @@ -165,7 +187,9 @@ describe('entries', () => {

await addAll(['/index.js', '/analytics.js']);

await expect(entries()).resolves.toEqual([[await keys(), await open(), defaultCacheName]]);
await expect(entries()).resolves.toEqual([
[await keys(), await open(), defaultCacheOptions.cacheName],
]);
});

test('can select which caches is returned', async () => {
Expand All @@ -184,7 +208,7 @@ describe('entries', () => {
[[], await open(randomCacheName), randomCacheName],
]);
await expect(entries()).resolves.toEqual([
[await keys(), await open(), defaultCacheName],
[await keys(), await open(), defaultCacheOptions.cacheName],
[[], await open(randomCacheName), randomCacheName],
]);
});
Expand Down
34 changes: 24 additions & 10 deletions __tests__/cache/meta-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,49 @@
*/

import {
createCacheName,
createCacheEntryName,
createMetaCacheName,
createMetaCacheEntryName,
createMetaRequest,
createMetaResponse,
getMetaData,
setMetaData,
deleteMetaData,
metaDataCacheName,
createCacheEntryName,
} from '../../src/cache/meta-data';
import { clear, defaultCacheName, cachePrefix } from '../../src/cache/cache';
import { clear, defaultCacheName, cachePrefix, cacheDelimiter } from '../../src/cache/cache';

beforeEach(async () => {
await clear();
});

describe('createCacheName', () => {
describe('createCacheEntryName - deprecated', () => {
beforeAll(() => {
jest.spyOn(console, 'warn');
console.warn.mockImplementation();
});

test('warns about the deprecation', () => {
const cacheName = 'my-cache';
expect(createCacheEntryName(cacheName)).toEqual(createMetaCacheEntryName(cacheName));
expect(console.warn).toHaveBeenCalledTimes(1);
});
});

describe('createMetaCacheName', () => {
test('returns the cache name to use for meta data', () => {
expect.assertions(1);

expect(createCacheName()).toEqual(`${cachePrefix}/${metaDataCacheName}`);
expect(createMetaCacheName()).toEqual(`${cachePrefix}${cacheDelimiter}${metaDataCacheName}`);
});
});

describe('createCacheEntryName', () => {
describe('createMetaCacheEntryName', () => {
test('returns a prefixed url based on the cache name', () => {
expect.assertions(1);

expect(createCacheEntryName()).toEqual(
`/${cachePrefix}/${metaDataCacheName}/${defaultCacheName}`,
expect(createMetaCacheEntryName()).toEqual(
`/${cachePrefix}${cacheDelimiter}${metaDataCacheName}/${defaultCacheName}`,
);
});
});
Expand All @@ -54,8 +68,8 @@ describe('createMetaRequest', () => {

const metaRequest = createMetaRequest();

expect(metaRequest).toEqual(new Request(createCacheEntryName()));
expect(metaRequest.url).toMatch(createCacheEntryName());
expect(metaRequest).toEqual(new Request(createMetaCacheEntryName()));
expect(metaRequest.url).toMatch(createMetaCacheEntryName());
});
});

Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import { printExports } from './helpers';
describe('library', () => {
test('consistently exports', () => {
expect.assertions(1);
expect(printExports(library, true)).toMatchSnapshot();
expect(printExports(library)).toMatchSnapshot();
});
});
8 changes: 4 additions & 4 deletions __tests__/middleware/expiration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import createExpirationMiddleware from '../../src/middleware/expiration';
import {
createMetaCacheName,
createMetaRequest,
createCacheName,
getMetaData,
setMetaData,
deleteMetaData,
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('createExpirationMiddleware', () => {
await waitFor(event.waitUntil);
await expect(
match(createMetaRequest(), {
cacheName: createCacheName(),
cacheName: createMetaCacheName(),
}),
).resolves.toBeInstanceOf(Response);
await expect(
Expand All @@ -116,7 +116,7 @@ describe('createExpirationMiddleware', () => {
await waitFor(event.waitUntil);
await expect(
match(createMetaRequest(), {
cacheName: createCacheName(),
cacheName: createMetaCacheName(),
}),
).resolves.toBeInstanceOf(Response);
await expect(
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('createExpirationMiddleware', () => {
await waitFor(event.waitUntil);
await expect(
match(createMetaRequest(), {
cacheName: createCacheName(),
cacheName: createMetaCacheName(),
}),
).resolves.toBeInstanceOf(Response);
await expect(
Expand Down
8 changes: 4 additions & 4 deletions packages/one-service-worker-demo/src/client/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ on('fetch', [
route: '/index.html',
}),
cacheRouter({
matcher: event => /@americanexpress/.test(event.request.url),
match: event => /@americanexpress/.test(event.request.url),
}),
cacheRouter({
cacheName: 'unpkg-cache',
matcher: event => /unpkg\.com/.test(event.request.url),
match: event => /unpkg\.com/.test(event.request.url),
}),
cacheRouter({
cacheName: 'example-scripts-cache',
matcher: event => /(scripts.*\.js$)/.test(event.request.url),
match: event => /(scripts.*\.js$)/.test(event.request.url),
}),
cacheRouter({
cacheName: 'example-static-cache',
matcher: event => /(static|dls).*\.(js|css|svg|png|ttf|woff)$/.test(event.request.url),
match: event => /(static|dls).*\.(js|css|svg|png|ttf|woff)$/.test(event.request.url),
}),
expiration(),
]);
Expand Down
2 changes: 2 additions & 0 deletions src/cache/__mocks__/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ Object.keys(imported).forEach(

export const {
cachePrefix,
cacheDelimiter,
defaultCacheName,
defaultCacheOptions,
createCacheName,
normalizeRequest,
open,
has,
Expand Down
4 changes: 2 additions & 2 deletions src/cache/__mocks__/meta-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Object.keys(imported).forEach(

export const {
metaDataCacheName,
createCacheName,
createCacheEntryName,
createMetaCacheName,
createMetaCacheEntryName,
createMetaRequest,
createMetaResponse,
getMetaStore,
Expand Down
Loading

0 comments on commit 56d1289

Please sign in to comment.