Skip to content

Commit

Permalink
feat(react): tests for useProperty when subscribed in Strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyzu committed Jan 16, 2022
1 parent 75d57fd commit 155c158
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions packages/react/src/use-property.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Property, atom, emitter, property } from '@frp-ts/core'
import React, { useState } from 'react'
import React, { useEffect } from 'react'
import { useProperty } from './use-property'
import { constVoid } from '@frp-ts/utils'
import { render } from '@testing-library/react'
import { render, screen } from '@testing-library/react'
import { act } from 'react-dom/test-utils'

interface TestProps<A> {
Expand All @@ -19,6 +19,7 @@ describe('useProperty', () => {
const a = atom.newAtom(123)
const cb = jest.fn(constVoid)
render(<Test property={a} onValue={cb} />)
expect(cb).toHaveBeenCalledTimes(1)
expect(cb).toHaveBeenLastCalledWith(123)
})
it('returns new value from new property', () => {
Expand All @@ -35,9 +36,10 @@ describe('useProperty', () => {
const Component = () => {
const value = useProperty(a)
cb(value)
useState(() => a.set(2))
useEffect(() => a.set(2), [])
return <></>
}

render(<Component />)
expect(cb.mock.calls).toEqual([[1], [2]])
})
Expand Down Expand Up @@ -84,4 +86,34 @@ describe('useProperty', () => {
a.set(a.get() + 1)
expect(cb).not.toHaveBeenCalled()
})
it('subscribe in strict mode with nested components', () => {
const a = atom.newAtom(1)
const cb = jest.fn()

const InnerComponent = (props: { value: number }) => {
cb(props.value)
return <>Value {props.value}</>
}
const Component = () => {
const value = useProperty(a)
return <InnerComponent value={value} />
}
render(
<React.StrictMode>
<Component />
</React.StrictMode>,
)

expect(screen.getByText('Value 1')).toBeDefined()
expect(cb).toBeCalledTimes(2)
expect(cb).toHaveBeenLastCalledWith(1)

act(() => {
a.set(2)
})

expect(screen.getByText('Value 2')).toBeDefined()
expect(cb).toBeCalledTimes(4)
expect(cb).toHaveBeenLastCalledWith(2)
})
})

0 comments on commit 155c158

Please sign in to comment.