Skip to content

Commit

Permalink
test(react): add more assertions for useViewEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 17, 2023
1 parent df5ae01 commit a7bf255
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/react/tests/create.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,39 @@ describe('createCodeMirror', () => {
const { useViewEffect, useContainerRef } = createCodeMirror<HTMLDivElement>()
function TestComponent() {
useViewEffect((view) => {
console.log('effect created')
view.dispatch({
changes: {
from: 0,
insert: 'hello',
},
})
return () => {
console.log('effect destroyed')
}
}, [])
const containerRef = useContainerRef()
return <div ref={containerRef} />
}
render(<TestComponent />)
vi.spyOn(console, 'log').mockImplementation(noop)
const { rerender, unmount } = render(<TestComponent />)
act(() => {
vi.runAllTimers()
})
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log).toHaveBeenCalledWith('effect created')
expect(screen.getByText('hello')).toBeInTheDocument()
rerender(<TestComponent />)
act(() => {
vi.runAllTimers()
})
expect(console.log).toHaveBeenCalledTimes(1)
unmount()
act(() => {
vi.runAllTimers()
})
expect(console.log).toHaveBeenCalledTimes(2)
expect(console.log).toHaveBeenLastCalledWith('effect destroyed')
})

test('useViewDispatch hook', async () => {
Expand Down

0 comments on commit a7bf255

Please sign in to comment.