// example.jsx import { h, Fragment, render, mount, ref, nextTick } from '@/index'; const testChangeRef = ref void)>(null); const Checkbox = mount(r => { let checkedList = ['sara']; const handleInput = (event: InputEvent) => { const value = (event.target as HTMLInputElement).value; const isInclude = checkedList.includes(value); if (isInclude) { checkedList = [...checkedList.filter(item => item !== value)]; } else { checkedList = [...checkedList, value]; } r(); }; testChangeRef.value = (newList: string[]) => { checkedList = newList; r(); }; return () => ( <> {' '} John {' '} Sara {' '} Tom ); }); const testWrap = document.getElementById('root') || document.createElement('div'); render(, testWrap); if (import.meta.vitest) { const { it, expect } = import.meta.vitest; it('You should see the initial value of the checkbox.', () => { expect(testWrap.outerHTML).toBe( '
John Sara Tom
' ); expect( ( Array.from( testWrap.querySelectorAll('input[name="checkname"]:checked') ) as HTMLFormElement[] ).map(item => item.value) ).toEqual(['sara']); }); it('The changed value should be reflected in the checkbox.', () => { if (testChangeRef.value) { testChangeRef.value(['john', 'tom']); } nextTick().then(() => { expect( ( Array.from( testWrap.querySelectorAll('input[name="checkname"]:checked') ) as HTMLFormElement[] ).map(item => item.value) ).toEqual(['john', 'tom']); }); }); }