import { Activity, Profiler, useEffect } from 'react'
import { describe, expect, test, vi } from 'vitest'
import { render } from 'vitest-browser-react'
import { configure } from 'vitest-browser-react/pure'
import { useControllableState } from './useControllableState.js'
configure({ reactStrictMode: true })
describe('useControllableState', () => {
test('should use the initial prop value', async () => {
const screen = await render()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'prop')
})
for (const value of [null, undefined]) {
test(`should use the fallback for ${String(value)}`, async () => {
const screen = await render()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'fallback')
})
}
for (const value of ['', 0, false]) {
test(`should preserve the falsy value ${JSON.stringify(value)}`, async () => {
const screen = await render()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', String(value))
})
}
test('should preserve local updates when the prop is unchanged', async () => {
const screen = await render()
await screen.getByRole('button', { name: 'Set local' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
await screen.rerender()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
})
test('should let changed props override local updates, including the original prop', async () => {
const screen = await render()
await screen.getByRole('button', { name: 'Set local' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
await screen.rerender()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'changed')
await screen.getByRole('button', { name: 'Set local' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
await screen.rerender()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'prop')
})
test('should avoid an extra render when the prop catches up with local state', async () => {
const onRender = vi.fn()
const screen = await render(, {
wrapper: ({ children }) => (
{children}
),
})
await screen.getByRole('button', { name: 'Set local' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
onRender.mockClear()
await screen.rerender()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
expect(onRender).toHaveBeenCalledTimes(1)
})
test('should compose functional updates in the same event', async () => {
const screen = await render()
await screen.getByRole('button', { name: 'Append twice' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'prop!!')
})
test('should preserve local updates when Strict Mode replays mount effects', async () => {
const screen = await render()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
})
test('should preserve local updates across Activity hide and show', async () => {
const screen = await render()
await screen.getByRole('button', { name: 'Set local' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
await screen.rerender()
await expect.element(screen.getByRole('status', { includeHidden: true })).not.toBeVisible()
await screen.rerender()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
})
test('should apply a prop changed while Activity is hidden', async () => {
const screen = await render()
await screen.getByRole('button', { name: 'Set local' }).click()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'local')
await screen.rerender()
await expect.element(screen.getByRole('status', { includeHidden: true })).not.toBeVisible()
await screen.rerender()
await expect.element(screen.getByRole('status')).toHaveProperty('textContent', 'changed')
})
})
type StateFixtureProps = {
fallback?: string
isHidden?: boolean
shouldUpdateOnMount?: boolean
value?: boolean | null | number | string
}
function ActivityFixture(props: StateFixtureProps) {
return (
)
}
function StateFixture({ fallback = 'fallback', shouldUpdateOnMount, value }: StateFixtureProps) {
const [localValue, setValue] = useControllableState(value, fallback)
return (
{shouldUpdateOnMount && }
)
}
function MountUpdate({ setValue }: { setValue: (value: string) => void }) {
useEffect(() => {
setValue('local')
}, [setValue])
return null
}