Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

cache types #8

Closed
mac2000 opened this issue Sep 14, 2019 · 4 comments · Fixed by #21
Closed

cache types #8

mac2000 opened this issue Sep 14, 2019 · 4 comments · Fixed by #21
Milestone

Comments

@mac2000
Copy link

mac2000 commented Sep 14, 2019

wondering whether cache types will be added here in future?

@emillime
Copy link

I'm also wondering this. How are we supposed to use const cache = caches.default right now it throws an error because default does not exist on CahceStorage. Is there a workaround?

@mac2000
Copy link
Author

mac2000 commented Sep 18, 2019

Workaround for now is:

export interface Caches {
    default: {
        put(request: Request | string, response: Response): Promise<undefined>;
        match(request: Request | string): Promise<Response | undefined>;
    };
}

declare let caches: Caches;

@emillime
Copy link

Thanks, now I just need to figure out how to set up the test environment.

@mac2000
Copy link
Author

mac2000 commented Sep 19, 2019

just implement interface, e.g.

export interface Caches {
    default: {
        put(request: Request | string, response: Response): Promise<undefined>;
        match(request: Request | string): Promise<Response | undefined>;
    };
}

declare let caches: Caches;

export interface GlobalCaches {
    caches: Caches;
}

export function makeFakeCache(): GlobalCaches {
    return {
        caches: {
            default: {
                put: (): Promise<undefined> => Promise.resolve(undefined),
                match: (): Promise<Response | undefined> => Promise.resolve(undefined),
            },
        },
    };
}


beforeEach(() => {
    Object.assign(global, makeServiceWorkerEnv(), makeFakeCache())
});

test('handler should use cache', async () => {
    caches.default.match = (): Promise<Response | undefined> =>
        Promise.resolve(new Response(JSON.stringify({ foo: "bar" })));

    const result = await doWork() // doWork inside uses global caches and trying to match on it will return { foo: "bar" }
    // ...
}); 

or use some mocking libraries, e.g. sinon, or maybe something built into jest, etc

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants