import { act, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useEffect, useState } from 'react'
import { describe, expect, it } from 'vitest'
import { CommandProvider } from '../CommandProvider'
import { PageType } from '../utils'
import { useCurrentPage, useRegisterPage, useSetPage } from './pagesHooks'
const PageAdderOne = ({ enabled = true }) => {
useRegisterPage('Page one', { type: PageType.Commands, sections: [] }, { enabled })
const setPage = useSetPage()
useEffect(() => {
if (enabled) {
setPage('Page one')
}
}, [enabled])
return
Page Adder One
}
const TogglePageAdder = () => {
const [mounted, setIsMounted] = useState(true)
return (
<>
{mounted && }
>
)
}
const TogglePageEnabled = () => {
const [enabled, setIsEnabled] = useState(true)
return (
<>
>
)
}
const PageDisplay = () => {
const currentPage = useCurrentPage()
return {currentPage?.name ?? '[None]'}
}
describe('useRegisterPage', () => {
it('registers a page on mount', async () => {
act(() => {
render(
)
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('Page one')
})
})
it("doesn't register a page on mount if disabled", async () => {
act(() => {
render(
)
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('[None]')
})
})
it('unregisters a page on dismount', async () => {
act(() => {
render(
)
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('Page one')
})
await act(async () => {
await userEvent.click(screen.getByRole('button'))
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('[None]')
})
})
it('unregisters and reregisters a page if disabled toggled', async () => {
act(() => {
render(
)
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('Page one')
})
await act(async () => {
await userEvent.click(screen.getByRole('button'))
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('[None]')
})
await act(async () => {
await userEvent.click(screen.getByRole('button'))
})
await waitFor(() => {
expect(screen.getByTestId('current-page')).toHaveTextContent('Page one')
})
})
})