Add type definitions for service-worker-mock (#36791)

* Add type definitions for service-worker-mock

* Fix linting issues

* Process feedback
This commit is contained in:
Remco Haszing 2019-07-11 22:59:46 +02:00 committed by Armando Aguirre
parent dc8a044062
commit dd813f10a9
4 changed files with 99 additions and 0 deletions

56
types/service-worker-mock/index.d.ts vendored Normal file
View File

@ -0,0 +1,56 @@
// Type definitions for service-worker-mock 2.0
// Project: https://github.com/pinterest/service-workers/tree/master/packages/service-worker-mock#readme
// Definitions by: Remco Haszing <https://github.com/remcohaszing>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
export = makeServiceWorkerEnv;
declare function makeServiceWorkerEnv(): WorkerGlobalScope;
declare namespace makeServiceWorkerEnv {
interface Caches {
[key: string]: Cache;
}
type Listeners = Map<keyof ServiceWorkerGlobalScopeEventMap, EventListener>;
interface Snapshot {
/**
* A key/value map of current cache contents.
*/
caches: Caches;
/**
* A list of active clients.
*/
clients: Client[];
/**
* A list of active notifications.
*/
notifications: Notification[];
}
}
declare global {
/**
* A key/value map of active listeners (`install`/`activate`/`fetch`/etc).
*/
const listeners: makeServiceWorkerEnv.Listeners;
/**
* Used to trigger active listeners.
*/
function trigger(type: keyof ServiceWorkerGlobalScopeEventMap): Promise<void>;
/**
* Used to generate a snapshot of the service worker internals.
*/
function snapshot(): makeServiceWorkerEnv.Snapshot;
interface WorkerGlobalScope {
listeners: typeof listeners;
trigger: typeof trigger;
snapshot: typeof snapshot;
}
}

View File

@ -0,0 +1,18 @@
import makeServiceWorkerEnv = require('service-worker-mock');
const mock = makeServiceWorkerEnv();
Object.assign(self, mock);
trigger('install');
mock.trigger('install');
trigger('fetch');
mock.trigger('fetch');
const emitFetch = listeners.get('fetch') as EventListener;
emitFetch(new Event('fetch'));
mock.caches.open('OLD_CACHE');
console.log(snapshot().caches.OLD_CACHE);
console.log(self.snapshot().clients[0].id);
console.log(mock.snapshot().notifications[0].body);

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"webworker"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"service-worker-mock-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }