import { jest } from '@jest/globals'; import { waitFor } from '@testing-library/react'; import type { ComponentType } from 'react'; import React from 'react'; import { runInDom } from '../../../__test_dependency__/run-in-dom.js'; import type { Binding } from '../../../binding/types/binding'; import { useBinding } from '../../../binding/use-binding.js'; import { BindingsConsumer } from '../index.js'; describe('BindingsConsumer', () => { it('should work with undefined bindings', () => runInDom(({ onMount }) => { const MyComponent: ComponentType = jest.fn(() => ( {(values) => { expect(values).toBeUndefined(); return ; }} )); onMount((rootElement) => { expect(MyComponent).toHaveBeenCalledTimes(1); expect(rootElement.innerHTML).toBe('
'); }); return ; })); it('initial render should work', () => runInDom(({ onMount }) => { const MyComponent: ComponentType = jest.fn(() => { const b = useBinding(() => 0, { id: 'test' }); return {({ b }) => {b}}; }); onMount((rootElement) => { expect(MyComponent).toHaveBeenCalledTimes(1); expect(rootElement.innerHTML).toBe('
0
'); }); return ; })); it('updating should rerender', () => runInDom(({ onMount }) => { let b: Binding; const consumerRenderer = jest.fn(({ b }: { b: number }) => {b}); const MyComponent: ComponentType = jest.fn(() => { b = useBinding(() => 0, { id: 'test' }); return {consumerRenderer}; }); onMount(async (rootElement) => { expect(MyComponent).toHaveBeenCalledTimes(1); expect(consumerRenderer).toHaveBeenCalledTimes(1); expect(rootElement.innerHTML).toBe('
0
'); b.set(1); await waitFor(() => expect(consumerRenderer).toHaveBeenCalledTimes(2)); expect(MyComponent).toHaveBeenCalledTimes(1); expect(rootElement.innerHTML).toBe('
1
'); }); return ; })); });