|
| 1 | +/** |
| 2 | + * Unit tests for the $RefreshSig$ factory injected by @prefresh/vite. |
| 3 | + * |
| 4 | + * The factory is defined inline in the prelude string. These tests replicate |
| 5 | + * it verbatim and drive it against a mock __PREFRESH__.sign so we can assert |
| 6 | + * on registration order and status values without spinning up Vite. |
| 7 | + */ |
| 8 | + |
| 9 | +import assert from 'node:assert/strict'; |
| 10 | +import test from 'node:test'; |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// Helpers |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | + |
| 16 | +/** |
| 17 | + * Builds a mock __PREFRESH__.sign implementation that records every call and |
| 18 | + * mirrors the real sign() state machine from @prefresh/core. |
| 19 | + */ |
| 20 | +function makeMockSign() { |
| 21 | + const signaturesForType = new Map(); |
| 22 | + const calls = []; |
| 23 | + |
| 24 | + function sign(type, key, forceReset, getCustomHooks, status) { |
| 25 | + calls.push({ type, key, status }); |
| 26 | + if (!type) return; |
| 27 | + |
| 28 | + if (status === 'begin') { |
| 29 | + if (!signaturesForType.has(type)) { |
| 30 | + signaturesForType.set(type, { |
| 31 | + type, |
| 32 | + key, |
| 33 | + forceReset, |
| 34 | + getCustomHooks: getCustomHooks || (() => []), |
| 35 | + fullKey: null, |
| 36 | + }); |
| 37 | + } |
| 38 | + return 'needsHooks'; |
| 39 | + } else if (status === 'needsHooks') { |
| 40 | + const sig = signaturesForType.get(type); |
| 41 | + if (sig) sig.fullKey = sig.key; // simplified computeKey |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return { sign, calls, signaturesForType }; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Builds the $RefreshSig$ factory exactly as the vite plugin injects it, |
| 50 | + * wired to a provided sign mock. |
| 51 | + */ |
| 52 | +function makeFactory(sign) { |
| 53 | + return function $RefreshSig$() { |
| 54 | + let savedType; |
| 55 | + let hasCustomHooks = false; |
| 56 | + let didCollectHooks = false; |
| 57 | + return (type, key, forceReset, getCustomHooks) => { |
| 58 | + if (typeof key === 'string') { |
| 59 | + if (!savedType) { |
| 60 | + savedType = type; |
| 61 | + hasCustomHooks = typeof getCustomHooks === 'function'; |
| 62 | + } |
| 63 | + if (type != null) { |
| 64 | + sign(type, key, forceReset, getCustomHooks, 'begin'); |
| 65 | + } |
| 66 | + } else { |
| 67 | + if (!didCollectHooks && hasCustomHooks) { |
| 68 | + didCollectHooks = true; |
| 69 | + sign(savedType, undefined, undefined, undefined, 'needsHooks'); |
| 70 | + } |
| 71 | + } |
| 72 | + return type; |
| 73 | + }; |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +// --------------------------------------------------------------------------- |
| 78 | +// Tests |
| 79 | +// --------------------------------------------------------------------------- |
| 80 | + |
| 81 | +test('plain component: registers on keyed call and collects hooks on body call', () => { |
| 82 | + const { sign, calls, signaturesForType } = makeMockSign(); |
| 83 | + const $RefreshSig$ = makeFactory(sign); |
| 84 | + |
| 85 | + const getCustomHooks = () => []; |
| 86 | + const _s = $RefreshSig$(); |
| 87 | + const Comp = function MyComponent() {}; |
| 88 | + const key = 'abc123='; |
| 89 | + |
| 90 | + // keyed call — emitted at module evaluation |
| 91 | + _s(Comp, key, false, getCustomHooks); |
| 92 | + |
| 93 | + assert.equal(calls.length, 1); |
| 94 | + assert.equal(calls[0].type, Comp); |
| 95 | + assert.equal(calls[0].status, 'begin'); |
| 96 | + assert.ok(signaturesForType.has(Comp)); |
| 97 | + |
| 98 | + // body call — emitted inside the component function, runs on first render |
| 99 | + _s(); |
| 100 | + |
| 101 | + assert.equal(calls.length, 2); |
| 102 | + assert.equal(calls[1].type, Comp); |
| 103 | + assert.equal(calls[1].status, 'needsHooks'); |
| 104 | + |
| 105 | + // subsequent renders must not re-register |
| 106 | + _s(); |
| 107 | + assert.equal(calls.length, 2, 'body call is a one-shot'); |
| 108 | +}); |
| 109 | + |
| 110 | +test('plain component without custom hooks: body call is skipped', () => { |
| 111 | + const { sign, calls } = makeMockSign(); |
| 112 | + const $RefreshSig$ = makeFactory(sign); |
| 113 | + |
| 114 | + const _s = $RefreshSig$(); |
| 115 | + const Comp = function MyComponent() {}; |
| 116 | + |
| 117 | + _s(Comp, 'key='); // no getCustomHooks argument |
| 118 | + |
| 119 | + _s(); // body call — should be a no-op since hasCustomHooks is false |
| 120 | + |
| 121 | + assert.equal(calls.length, 1, 'only the begin call, no needsHooks'); |
| 122 | +}); |
| 123 | + |
| 124 | +test('HOC chain (memo-wrapped): both inner and outer types are registered', () => { |
| 125 | + const { sign, calls, signaturesForType } = makeMockSign(); |
| 126 | + const $RefreshSig$ = makeFactory(sign); |
| 127 | + |
| 128 | + const getCustomHooks = () => []; |
| 129 | + const _s = $RefreshSig$(); |
| 130 | + |
| 131 | + const innerFn = function Button() {}; |
| 132 | + const memoResult = { type: innerFn, $$typeof: Symbol.for('preact.memo') }; |
| 133 | + const key = 'WQ9WH5eCVGcEPdUJDepp+VlX1/c='; |
| 134 | + |
| 135 | + // Mirrors: const Button = _s(memo(_c = _s(innerFn, key)), key) |
| 136 | + // Evaluation is inside-out, so innerFn is registered first. |
| 137 | + _s(innerFn, key, false, getCustomHooks); |
| 138 | + _s(memoResult, key); |
| 139 | + |
| 140 | + assert.equal(calls.length, 2); |
| 141 | + assert.equal(calls[0].type, innerFn); |
| 142 | + assert.equal(calls[0].status, 'begin'); |
| 143 | + assert.equal(calls[1].type, memoResult); |
| 144 | + assert.equal(calls[1].status, 'begin'); |
| 145 | + |
| 146 | + assert.ok(signaturesForType.has(innerFn), 'inner function registered'); |
| 147 | + assert.ok(signaturesForType.has(memoResult), 'memo wrapper registered'); |
| 148 | + |
| 149 | + // savedType is the innermost (innerFn) — body call uses it |
| 150 | + _s(); |
| 151 | + assert.equal(calls.length, 3); |
| 152 | + assert.equal(calls[2].type, innerFn); |
| 153 | + assert.equal(calls[2].status, 'needsHooks'); |
| 154 | +}); |
| 155 | + |
| 156 | +test('HOC chain: innermost registration is not overwritten by outer call', () => { |
| 157 | + const { sign, signaturesForType } = makeMockSign(); |
| 158 | + const $RefreshSig$ = makeFactory(sign); |
| 159 | + |
| 160 | + const getCustomHooks = () => ['useFoo']; |
| 161 | + const _s = $RefreshSig$(); |
| 162 | + |
| 163 | + const innerFn = function Card() {}; |
| 164 | + const wrapper = { type: innerFn }; |
| 165 | + const key = 'somekey='; |
| 166 | + |
| 167 | + _s(innerFn, key, false, getCustomHooks); // inner: has getCustomHooks |
| 168 | + _s(wrapper, key); // outer: no getCustomHooks |
| 169 | + |
| 170 | + // inner registration must preserve its getCustomHooks |
| 171 | + const sig = signaturesForType.get(innerFn); |
| 172 | + assert.equal(sig.getCustomHooks, getCustomHooks); |
| 173 | +}); |
| 174 | + |
| 175 | +test('multiple renders: body call only triggers needsHooks once', () => { |
| 176 | + const { sign, calls } = makeMockSign(); |
| 177 | + const $RefreshSig$ = makeFactory(sign); |
| 178 | + |
| 179 | + const getCustomHooks = () => []; |
| 180 | + const _s = $RefreshSig$(); |
| 181 | + const Comp = function Foo() {}; |
| 182 | + |
| 183 | + _s(Comp, 'k=', false, getCustomHooks); |
| 184 | + |
| 185 | + _s(); // render 1 |
| 186 | + _s(); // render 2 |
| 187 | + _s(); // render 3 |
| 188 | + |
| 189 | + const needsHooksCalls = calls.filter(c => c.status === 'needsHooks'); |
| 190 | + assert.equal(needsHooksCalls.length, 1); |
| 191 | +}); |
0 commit comments