42 lines
2.0 KiB
TypeScript
42 lines
2.0 KiB
TypeScript
|
|
import { describe, it, expect } from 'vitest';
|
||
|
|
import { resolveStrategy } from '../../src/lib/sw/cache-strategy';
|
||
|
|
|
||
|
|
describe('resolveStrategy', () => {
|
||
|
|
it('images bucket for /images/*', () => {
|
||
|
|
expect(resolveStrategy({ url: '/images/favicon-abc.png', method: 'GET' })).toBe('images');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('swr for recipe HTML pages', () => {
|
||
|
|
expect(resolveStrategy({ url: '/recipes/42', method: 'GET' })).toBe('swr');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('swr for recipe API reads', () => {
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/42', method: 'GET' })).toBe('swr');
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/all?sort=name', method: 'GET' })).toBe('swr');
|
||
|
|
expect(resolveStrategy({ url: '/api/wishlist', method: 'GET' })).toBe('swr');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('network-only for write methods', () => {
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/42', method: 'PATCH' })).toBe('network-only');
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/42/favorite', method: 'PUT' })).toBe('network-only');
|
||
|
|
expect(resolveStrategy({ url: '/api/wishlist', method: 'POST' })).toBe('network-only');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('network-only for online-only endpoints even on GET', () => {
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/import', method: 'GET' })).toBe('network-only');
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/preview?url=x', method: 'GET' })).toBe('network-only');
|
||
|
|
expect(resolveStrategy({ url: '/api/recipes/search/web?q=x', method: 'GET' })).toBe('network-only');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('shell bucket for build/static assets', () => {
|
||
|
|
expect(resolveStrategy({ url: '/_app/immutable/chunks/x.js', method: 'GET' })).toBe('shell');
|
||
|
|
expect(resolveStrategy({ url: '/icon-192.png', method: 'GET' })).toBe('shell');
|
||
|
|
expect(resolveStrategy({ url: '/manifest.webmanifest', method: 'GET' })).toBe('shell');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('falls through to swr for other same-origin GETs (e.g. root page)', () => {
|
||
|
|
expect(resolveStrategy({ url: '/', method: 'GET' })).toBe('swr');
|
||
|
|
expect(resolveStrategy({ url: '/wishlist', method: 'GET' })).toBe('swr');
|
||
|
|
});
|
||
|
|
});
|