|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import type { ViteDevServer } from 'vite'; |
| 3 | +import { sendRouterCssHotUpdate } from './dev-middleware'; |
| 4 | + |
| 5 | +const file = '/app/src/routes/docs/docs.css'; |
| 6 | + |
| 7 | +/** Minimal ViteDevServer stand-in with controllable client/SSR graphs and a spy on the HMR channel. */ |
| 8 | +function makeServer(opts: { client?: { url: string }[]; ssr?: { url: string }[] }) { |
| 9 | + const send = vi.fn(); |
| 10 | + const graph = (mods?: { url: string }[]) => ({ |
| 11 | + getModulesByFile: () => (mods ? new Set(mods) : undefined), |
| 12 | + }); |
| 13 | + const server = { |
| 14 | + environments: { |
| 15 | + client: { moduleGraph: graph(opts.client), hot: { send } }, |
| 16 | + ssr: { moduleGraph: graph(opts.ssr) }, |
| 17 | + }, |
| 18 | + } as unknown as ViteDevServer; |
| 19 | + return { server, send }; |
| 20 | +} |
| 21 | + |
| 22 | +describe('sendRouterCssHotUpdate', () => { |
| 23 | + it('ignores non-CSS files', () => { |
| 24 | + const { server, send } = makeServer({ ssr: [{ url: '/x.tsx' }] }); |
| 25 | + expect(sendRouterCssHotUpdate(server, '/app/src/routes/index.tsx', 1)).toBe(false); |
| 26 | + expect(send).not.toHaveBeenCalled(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('emits a deduped css-update for route CSS that only lives in the SSR graph', () => { |
| 30 | + const { server, send } = makeServer({ |
| 31 | + ssr: [{ url: '/src/routes/docs/docs.css' }, { url: '/src/routes/docs/docs.css?inline' }], |
| 32 | + }); |
| 33 | + expect(sendRouterCssHotUpdate(server, file, 123)).toBe(true); |
| 34 | + expect(send).toHaveBeenCalledWith({ |
| 35 | + type: 'update', |
| 36 | + updates: [ |
| 37 | + { |
| 38 | + type: 'css-update', |
| 39 | + path: '/src/routes/docs/docs.css', |
| 40 | + acceptedPath: '/src/routes/docs/docs.css', |
| 41 | + timestamp: 123, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('defers to Vite when the CSS is already in the client graph', () => { |
| 48 | + const { server, send } = makeServer({ |
| 49 | + client: [{ url: '/src/routes/docs/docs.css' }], |
| 50 | + ssr: [{ url: '/src/routes/docs/docs.css' }], |
| 51 | + }); |
| 52 | + expect(sendRouterCssHotUpdate(server, file, 1)).toBe(false); |
| 53 | + expect(send).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments