|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
| 2 | +import { toJSONAsync } from 'seroval' |
| 3 | + |
| 4 | +// `getDefaultSerovalPlugins` reads the Start options through an isomorphic |
| 5 | +// function. Uncompiled, that chain resolves to its server implementation and |
| 6 | +// wants a Start context in AsyncLocalStorage, which a browser never has. The |
| 7 | +// adapter list is irrelevant to the cache lookup under test, so stub it out and |
| 8 | +// serialize the fixtures the same way. |
| 9 | +vi.mock('@tanstack/start-client-core', async (importOriginal) => { |
| 10 | + const actual = |
| 11 | + await importOriginal<typeof import('@tanstack/start-client-core')>() |
| 12 | + return { ...actual, getDefaultSerovalPlugins: () => [] } |
| 13 | +}) |
| 14 | + |
| 15 | +const { staticFunctionMiddleware } = |
| 16 | + await import('../src/staticFunctionMiddleware') |
| 17 | + |
| 18 | +const clientMiddleware = staticFunctionMiddleware.options.client! |
| 19 | + |
| 20 | +/** The result the live server function produces when the cache is not used. */ |
| 21 | +const LIVE_RESULT = { result: 'from the server function' } |
| 22 | + |
| 23 | +/** |
| 24 | + * Each test uses its own `data`, so it hashes to its own cache URL and cannot |
| 25 | + * be served by the module level client cache another test populated. |
| 26 | + */ |
| 27 | +function callClientMiddleware(data: unknown) { |
| 28 | + const next = vi.fn(async () => LIVE_RESULT) |
| 29 | + const promise = clientMiddleware({ |
| 30 | + serverFnMeta: { id: 'test_fn' }, |
| 31 | + data, |
| 32 | + context: {}, |
| 33 | + next, |
| 34 | + } as any) |
| 35 | + return { promise, next } |
| 36 | +} |
| 37 | + |
| 38 | +function jsonResponse(body: string, status = 200) { |
| 39 | + return new Response(body, { |
| 40 | + status, |
| 41 | + headers: { 'content-type': 'application/json' }, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +/** What the application's catch-all route serves for a missing cache file. */ |
| 46 | +function htmlShellResponse(status = 200) { |
| 47 | + return new Response('<!DOCTYPE html><html><body></body></html>', { |
| 48 | + status, |
| 49 | + headers: { 'content-type': 'text/html' }, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +beforeEach(() => { |
| 54 | + vi.stubEnv('NODE_ENV', 'production') |
| 55 | +}) |
| 56 | + |
| 57 | +afterEach(() => { |
| 58 | + vi.unstubAllEnvs() |
| 59 | + vi.unstubAllGlobals() |
| 60 | + vi.restoreAllMocks() |
| 61 | +}) |
| 62 | + |
| 63 | +describe('staticFunctionMiddleware client on a cache miss', () => { |
| 64 | + test('falls back to the server function when the HTML shell is served with a 200', async () => { |
| 65 | + vi.stubGlobal( |
| 66 | + 'fetch', |
| 67 | + vi.fn(async () => htmlShellResponse(200)), |
| 68 | + ) |
| 69 | + |
| 70 | + const { promise, next } = callClientMiddleware({ case: 'html-200' }) |
| 71 | + |
| 72 | + await expect(promise).resolves.toBe(LIVE_RESULT) |
| 73 | + expect(next).toHaveBeenCalledTimes(1) |
| 74 | + }) |
| 75 | + |
| 76 | + test('falls back to the server function on a 404', async () => { |
| 77 | + vi.stubGlobal( |
| 78 | + 'fetch', |
| 79 | + vi.fn(async () => htmlShellResponse(404)), |
| 80 | + ) |
| 81 | + |
| 82 | + const { promise, next } = callClientMiddleware({ case: 'html-404' }) |
| 83 | + |
| 84 | + await expect(promise).resolves.toBe(LIVE_RESULT) |
| 85 | + expect(next).toHaveBeenCalledTimes(1) |
| 86 | + }) |
| 87 | + |
| 88 | + test('falls back to the server function when the body is not valid JSON', async () => { |
| 89 | + vi.stubGlobal( |
| 90 | + 'fetch', |
| 91 | + vi.fn(async () => jsonResponse('not json at all')), |
| 92 | + ) |
| 93 | + |
| 94 | + const { promise, next } = callClientMiddleware({ case: 'bad-json' }) |
| 95 | + |
| 96 | + await expect(promise).resolves.toBe(LIVE_RESULT) |
| 97 | + expect(next).toHaveBeenCalledTimes(1) |
| 98 | + }) |
| 99 | + |
| 100 | + test('falls back to the server function when the request fails outright', async () => { |
| 101 | + vi.stubGlobal( |
| 102 | + 'fetch', |
| 103 | + vi.fn(async () => { |
| 104 | + throw new TypeError('Failed to fetch') |
| 105 | + }), |
| 106 | + ) |
| 107 | + |
| 108 | + const { promise, next } = callClientMiddleware({ case: 'network-error' }) |
| 109 | + |
| 110 | + await expect(promise).resolves.toBe(LIVE_RESULT) |
| 111 | + expect(next).toHaveBeenCalledTimes(1) |
| 112 | + }) |
| 113 | +}) |
| 114 | + |
| 115 | +describe('staticFunctionMiddleware client on a cache hit', () => { |
| 116 | + test('returns the prerendered result without calling the server function', async () => { |
| 117 | + const payload = JSON.stringify( |
| 118 | + await toJSONAsync({ |
| 119 | + result: 'from the static cache', |
| 120 | + context: { user: 'sean' }, |
| 121 | + }), |
| 122 | + ) |
| 123 | + const fetchMock = vi.fn(async () => jsonResponse(payload)) |
| 124 | + vi.stubGlobal('fetch', fetchMock) |
| 125 | + |
| 126 | + const first = callClientMiddleware({ case: 'hit' }) |
| 127 | + await expect(first.promise).resolves.toMatchObject({ |
| 128 | + result: 'from the static cache', |
| 129 | + context: { user: 'sean' }, |
| 130 | + }) |
| 131 | + expect(first.next).not.toHaveBeenCalled() |
| 132 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 133 | + |
| 134 | + // The same call is served from the client cache rather than refetched. |
| 135 | + const second = callClientMiddleware({ case: 'hit' }) |
| 136 | + await expect(second.promise).resolves.toMatchObject({ |
| 137 | + result: 'from the static cache', |
| 138 | + }) |
| 139 | + expect(second.next).not.toHaveBeenCalled() |
| 140 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 141 | + }) |
| 142 | +}) |
| 143 | + |
| 144 | +describe('staticFunctionMiddleware client outside production', () => { |
| 145 | + test('does not request the cache at all', async () => { |
| 146 | + vi.stubEnv('NODE_ENV', 'development') |
| 147 | + const fetchMock = vi.fn(async () => jsonResponse('{}')) |
| 148 | + vi.stubGlobal('fetch', fetchMock) |
| 149 | + |
| 150 | + const { promise, next } = callClientMiddleware({ case: 'development' }) |
| 151 | + |
| 152 | + await expect(promise).resolves.toBe(LIVE_RESULT) |
| 153 | + expect(fetchMock).not.toHaveBeenCalled() |
| 154 | + expect(next).toHaveBeenCalledTimes(1) |
| 155 | + }) |
| 156 | +}) |
0 commit comments