Skip to content

Commit

Permalink
Merge branch 'main' into react/feat/useErrorBoundary-to-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli authored Oct 27, 2023
2 parents 068642c + b962122 commit 9c4155d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-owls-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react": patch
---

test(react): add useTimeout test case
22 changes: 0 additions & 22 deletions packages/react/src/hooks/useTimeout.spec.ts

This file was deleted.

47 changes: 47 additions & 0 deletions packages/react/src/hooks/useTimeout.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { MS_100 } from '@suspensive/test-utils'
import { act, render, renderHook, screen } from '@testing-library/react'
import { useState } from 'react'
import { describe, expect, it, vi } from 'vitest'
import { useTimeout } from '.'

vi.useFakeTimers()

const TestComponent = () => {
const [number, setNumber] = useState(0)

useTimeout(() => {
setNumber(number + 1)
}, MS_100)

return <div>{number}</div>
}

describe('useTimeout', () => {
it('should run given function once after given timeout', () => {
const fn = vi.fn()
const rendered = renderHook(() => useTimeout(fn, MS_100))
expect(fn).toHaveBeenCalledTimes(0)
act(() => vi.advanceTimersByTime(MS_100))
expect(fn).toHaveBeenCalledTimes(1)
act(() => vi.advanceTimersByTime(MS_100))
expect(fn).toHaveBeenCalledTimes(1)
rendered.rerender()
expect(fn).toHaveBeenCalledTimes(1)
act(() => vi.advanceTimersByTime(MS_100))
expect(fn).toHaveBeenCalledTimes(1)
})

it('should not re-call callback reveived as argument even if component using this hook is rerendered', () => {
const { rerender } = render(<TestComponent />)

expect(screen.getByText('0')).toBeInTheDocument()

act(() => vi.advanceTimersByTime(MS_100))
expect(screen.getByText('1')).toBeInTheDocument()

rerender(<TestComponent />)

act(() => vi.advanceTimersByTime(MS_100))
expect(screen.getByText('1')).toBeInTheDocument()
})
})

0 comments on commit 9c4155d

Please sign in to comment.