import { signal, computed, useComputed, useSignal } from "../../src/lib"; import React, { useReducer, StrictMode, useState } from "react"; import { vi, describe, it, beforeEach, afterEach, expect } from "vitest"; import { getConsoleErrorSpy, checkConsoleErrorLogs } from "./utils"; export function mountSignalsTests( render: (element: JSX.Element) => string | Promise ) { beforeEach(async () => { getConsoleErrorSpy().mockClear(); }); afterEach(async () => { checkConsoleErrorLogs(); }); describe("mount text bindings", () => { it("should render text without signals", async () => { const html = await render(test); expect(html).to.equal("test"); }); it("should render Signals as SignalValue", async () => { const sig = signal("test"); const html = await render({sig}); expect(html).to.equal("test"); }); it("should render computed as SignalValue", async () => { const sig = signal("test"); const comp = computed(() => `${sig} ${sig}`); const html = await render({comp}); expect(html).to.equal("test test"); }); }); describe("mount component bindings", () => { it("should mount component with signals as text", async () => { const sig = signal("foo"); function App() { const value = sig.value; return

{value}

; } const html = await render(); expect(html).to.equal("

foo

"); }); it("should activate signal accessed in render", async () => { const sig = signal(null); function App() { const arr = useComputed(() => { // trigger read sig.value; return []; }); const str = arr.value.join(", "); return

{str}

; } try { await render(); } catch (e: any) { expect.fail(e.stack); } }); it("should properly mount in strict mode", async () => { const sig = signal(-1); const Test = () =>

{sig.value}

; const App = () => ( ); const html = await render(); expect(html).to.equal("

-1

"); }); it("should correctly mount components that have useReducer()", async () => { const count = signal(0); const Test = () => { const [state] = useReducer((state: number, action: number) => { return state + action; }, -2); const doubled = count.value * 2; return (
            {state}
            {doubled}
          
); }; const html = await render(); expect(html).to.equal("
-20
"); }); it("should not fail when a component calls setState while mounting", async () => { function App() { const [state, setState] = useState(0); if (state == 0) { setState(1); } return
{state}
; } const html = await render(); expect(html).to.equal("
1
"); }); it("should not fail when a component calls setState multiple times while mounting", async () => { function App() { const [state, setState] = useState(0); if (state < 5) { setState(state + 1); } return
{state}
; } const html = await render(); expect(html).to.equal("
5
"); }); }); describe("useSignal()", () => { it("should create a signal from a primitive value", async () => { function App() { const count = useSignal(1); return (
{count}
); } const html = await render(); expect(html).to.equal("
1
"); }); it("should properly update signal values changed during mount", async () => { function App() { const count = useSignal(0); if (count.value == 0) { count.value++; } return (
{count}
); } const html = await render(); expect(html).to.equal("
1
"); const html2 = await render(); expect(html2).to.equal("
1
"); }); }); }