// @vitest-environment node import { renderToString } from 'react-dom/server'; import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { useSelector, useWire, useWireValue, Wire } from './index'; describe('ssr', () => { afterEach(() => { vi.restoreAllMocks(); }); beforeEach(() => { vi.clearAllMocks(); }); describe('wire', () => { test('use wire value', () => { function MyComponent() { let wire = useWire(null, 5); const value = useWireValue(wire) ?? 'missing'; return
{value}
; } const mockConsoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); let view = renderToString(); expect(mockConsoleError.mock.lastCall).toBeUndefined(); expect(view).toBe('
5
'); }); test('wire with uplink', () => { function Parent() { let wire = useWire(null, 5); const value = useWireValue(wire) ?? 'missing'; return (
{value}
); } function Child(props: { wire: Wire }) { let wire = useWire(props.wire, 10); const value = useWireValue(wire) ?? 'missing'; return {value}; } const mockConsoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); let view = renderToString(); expect(mockConsoleError.mock.lastCall).toBeUndefined(); expect(view).toBe('
55
'); }); test('wire with uninitialized uplink', () => { function Parent() { let wire = useWire(null); const value = useWireValue(wire) ?? 'missing'; return (
{value}
); } function Child(props: { wire: Wire }) { let wire = useWire(props.wire, 10); const value = useWireValue(wire) ?? 'missing'; return {value}; } const mockConsoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); let view = renderToString(); expect(mockConsoleError.mock.lastCall).toEqual([ 'upLink value is undefined. uplink without value is not supported in server side rendering', ]); expect(view).toBe('
missing10
'); }); test('wire with uninitialized uplink and no initial value', () => { function Parent() { let wire = useWire(null); const value = useWireValue(wire) ?? 'missing'; return (
{value}
); } function Child(props: { wire: Wire }) { let wire = useWire(props.wire); const value = useWireValue(wire) ?? 'missing'; return {value}; } const mockConsoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); let view = renderToString(); expect(mockConsoleError.mock.lastCall).toBeUndefined(); expect(view).toBe('
missingmissing
'); }); test('wire with uninitialized uplink with sibling', () => { function Parent() { let wire = useWire(null); const value = useWireValue(wire) ?? 'missing'; return (
{value}
); } function Child(props: { wire: Wire; init: number }) { let wire = useWire(props.wire, props.init); const value = useWireValue(wire) ?? 'missing'; return {value}; } const mockConsoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); let view = renderToString(); expect(mockConsoleError.mock.lastCall).toEqual([ 'upLink value is undefined. uplink without value is not supported in server side rendering', ]); expect(view).toBe('
missing12
'); }); }); describe('selector', () => { test('selector', () => { function MyComponent() { let wire = useWire(null, 5); let double = useSelector({ get: ({ get }) => get(wire) * 2 }, [wire]); const value = useWireValue(wire) ?? 'missing'; const doubleValue = useWireValue(double) ?? 'missing'; return (
{value}x2={doubleValue}
); } const mockConsoleError = vi .spyOn(console, 'error') .mockImplementation(() => {}); let view = renderToString(); expect(mockConsoleError.mock.lastCall).toBeUndefined(); expect(view).toBe('
5x2=10
'); }); }); });