import { waitFor } from '@testing-library/react'; import React from 'react'; import { useBinding } from 'react-bindings'; import { runInDom } from '../../../__test_dependency__/run-in-dom.js'; import { sleep } from '../../../__test_dependency__/sleep.js'; import { useDerivedWaitable } from '../../../specialized-waitables/use-derived-waitable/use-derived-waitable.js'; import { useWaitableFunction } from '../../../specialized-waitables/use-waitable-function.js'; import { WaitablesConsumer } from '../WaitablesConsumer.js'; describe('WaitablesConsumer', () => { it('with multiple transformers as children should use the first applicable one', () => runInDom(({ onMount }) => { const a = useBinding(() => 1, { id: 'a' }); const b = useWaitableFunction( async () => { await sleep(50); return { ok: true, value: 2 }; }, { id: 'b' } ); const c = useDerivedWaitable({ a, b }, ({ a, b }) => a + b, { id: 'c' }); onMount(async (rootElement) => { expect(rootElement.innerHTML).toBe('
loading
'); await waitFor(() => expect(rootElement.innerHTML).toBe('
1+2=3
')); }); return ( {[ ({ a, b, c }) => (
{a}+{b}={c}
), { ifLoading: () => loading } ]}
); })); it('with multiple transformers, mixed as children and props, should use the first applicable one where children take precedence over props', () => runInDom(({ onMount }) => { const a = useBinding(() => 1, { id: 'a' }); const b = useWaitableFunction( async () => { await sleep(50); return { ok: true, value: 2 }; }, { id: 'b' } ); const c = useDerivedWaitable({ a, b }, ({ a, b }) => a + b, { id: 'c' }); onMount(async (rootElement) => { expect(rootElement.innerHTML).toBe('
loading
'); await waitFor(() => expect(rootElement.innerHTML).toBe('
1+2=3
')); }); return ( loading}> {({ a, b, c }) => (
{a}+{b}={c}
)}
); })); });