|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + action, |
| 4 | + createMemo, |
| 5 | + createRenderEffect, |
| 6 | + createRoot, |
| 7 | + createSignal, |
| 8 | + flush, |
| 9 | + isPending, |
| 10 | + latest, |
| 11 | + untrack |
| 12 | +} from "../src/index.js"; |
| 13 | + |
| 14 | +describe.each([ |
| 15 | + ["Object.is", Object.is], |
| 16 | + ["false", false] |
| 17 | +] as const)("staged signed zero with equals: %s", (_, equals) => { |
| 18 | + it.each([ |
| 19 | + ["-0 to +0", -0, 0], |
| 20 | + ["+0 to -0", 0, -0] |
| 21 | + ] as const)("updates an observed derivation from %s", (_, initial, next) => { |
| 22 | + const { dispose, value, setValue, reciprocal, seen } = createRoot(dispose => { |
| 23 | + const [value, setValue] = createSignal(initial, { equals }); |
| 24 | + const reciprocal = createMemo(() => 1 / value()); |
| 25 | + const seen: number[] = []; |
| 26 | + createRenderEffect(reciprocal, value => void seen.push(value)); |
| 27 | + return { dispose, value, setValue, reciprocal, seen }; |
| 28 | + }); |
| 29 | + try { |
| 30 | + flush(); |
| 31 | + expect(seen).toEqual([1 / initial]); |
| 32 | + |
| 33 | + setValue(next); |
| 34 | + flush(); |
| 35 | + |
| 36 | + expect(value()).toBe(next); |
| 37 | + expect(reciprocal()).toBe(1 / next); |
| 38 | + expect(seen).toEqual([1 / initial, 1 / next]); |
| 39 | + } finally { |
| 40 | + dispose(); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + for (const where of ["in-action", "before-action"] as const) |
| 45 | + for (const strict of [false, true]) |
| 46 | + it.each([ |
| 47 | + ["-0 to +0", -0, 0], |
| 48 | + ["+0 to -0", 0, -0] |
| 49 | + ] as const)( |
| 50 | + `keeps %s pending until reveal (${where}, strict: ${strict})`, |
| 51 | + async (_, initial, next) => { |
| 52 | + const gate = Promise.withResolvers<void>(); |
| 53 | + const state = createRoot(dispose => { |
| 54 | + const [value, setValue] = createSignal(initial, { equals }); |
| 55 | + const [other, setOther] = createSignal(0); |
| 56 | + const plain: [number, boolean][] = []; |
| 57 | + const ahead: [boolean, number][] = []; |
| 58 | + createRenderEffect(other, () => {}); |
| 59 | + createRenderEffect( |
| 60 | + () => { |
| 61 | + value(); |
| 62 | + const v = strict ? untrack(value, "signed-zero") : value(); |
| 63 | + return [1 / v, isPending(value)] as [number, boolean]; |
| 64 | + }, |
| 65 | + v => void plain.push(v) |
| 66 | + ); |
| 67 | + createRenderEffect( |
| 68 | + () => [isPending(value), 1 / latest(value)] as [boolean, number], |
| 69 | + v => void ahead.push(v) |
| 70 | + ); |
| 71 | + return { dispose, value, setValue, setOther, plain, ahead }; |
| 72 | + }); |
| 73 | + try { |
| 74 | + flush(); |
| 75 | + if (where === "before-action") state.setValue(next); |
| 76 | + action(function* () { |
| 77 | + state.setOther(1); |
| 78 | + if (where === "in-action") state.setValue(next); |
| 79 | + yield gate.promise; |
| 80 | + })(); |
| 81 | + flush(); |
| 82 | + |
| 83 | + expect(state.value()).toBe(initial); |
| 84 | + expect(isPending(state.value)).toBe(true); |
| 85 | + expect(state.plain).toEqual([ |
| 86 | + [1 / initial, false], |
| 87 | + [1 / initial, true] |
| 88 | + ]); |
| 89 | + expect(state.ahead).toEqual([ |
| 90 | + [false, 1 / initial], |
| 91 | + [true, 1 / next] |
| 92 | + ]); |
| 93 | + |
| 94 | + gate.resolve(); |
| 95 | + await new Promise<void>(resolve => setTimeout(resolve, 0)); |
| 96 | + flush(); |
| 97 | + |
| 98 | + expect(state.value()).toBe(next); |
| 99 | + expect(isPending(state.value)).toBe(false); |
| 100 | + expect(state.plain).toEqual([ |
| 101 | + [1 / initial, false], |
| 102 | + [1 / initial, true], |
| 103 | + [1 / next, false] |
| 104 | + ]); |
| 105 | + expect(state.ahead).toEqual([ |
| 106 | + [false, 1 / initial], |
| 107 | + [true, 1 / next], |
| 108 | + [false, 1 / next] |
| 109 | + ]); |
| 110 | + } finally { |
| 111 | + gate.resolve(); |
| 112 | + state.dispose(); |
| 113 | + flush(); |
| 114 | + } |
| 115 | + } |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +it("keeps the default comparator's signed-zero equality", () => { |
| 120 | + const { dispose, value, setValue, reciprocal, seen } = createRoot(dispose => { |
| 121 | + const [value, setValue] = createSignal(-0); |
| 122 | + const reciprocal = createMemo(() => 1 / value()); |
| 123 | + const seen: number[] = []; |
| 124 | + createRenderEffect(reciprocal, value => void seen.push(value)); |
| 125 | + return { dispose, value, setValue, reciprocal, seen }; |
| 126 | + }); |
| 127 | + try { |
| 128 | + flush(); |
| 129 | + |
| 130 | + setValue(0); |
| 131 | + flush(); |
| 132 | + |
| 133 | + expect(value()).toBe(-0); |
| 134 | + expect(reciprocal()).toBe(-Infinity); |
| 135 | + expect(seen).toEqual([-Infinity]); |
| 136 | + } finally { |
| 137 | + dispose(); |
| 138 | + } |
| 139 | +}); |
| 140 | + |
| 141 | +it("keeps an accepted NaN rewrite as a proposal", async () => { |
| 142 | + const gate = Promise.withResolvers<void>(); |
| 143 | + const state = createRoot(dispose => { |
| 144 | + const [value, setValue] = createSignal(NaN, { equals: false }); |
| 145 | + const seen: boolean[] = []; |
| 146 | + createRenderEffect( |
| 147 | + () => isPending(value), |
| 148 | + v => void seen.push(v) |
| 149 | + ); |
| 150 | + return { dispose, value, setValue, seen }; |
| 151 | + }); |
| 152 | + try { |
| 153 | + flush(); |
| 154 | + action(function* () { |
| 155 | + state.setValue(NaN); |
| 156 | + yield gate.promise; |
| 157 | + })(); |
| 158 | + flush(); |
| 159 | + expect(state.seen).toEqual([false, true]); |
| 160 | + expect(isPending(state.value)).toBe(true); |
| 161 | + |
| 162 | + gate.resolve(); |
| 163 | + await new Promise<void>(resolve => setTimeout(resolve, 0)); |
| 164 | + flush(); |
| 165 | + expect(state.seen).toEqual([false, true, false]); |
| 166 | + } finally { |
| 167 | + gate.resolve(); |
| 168 | + state.dispose(); |
| 169 | + flush(); |
| 170 | + } |
| 171 | +}); |
0 commit comments