Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { render } from '@testing-library/preact'
import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
import type { TanstackQueryDevtools } from '@tanstack/query-devtools'
import { TanstackQueryDevtools } from '@tanstack/query-devtools'

const mountMock = vi.fn()
const unmountMock = vi.fn()
Expand Down Expand Up @@ -82,6 +82,116 @@ describe('PreactQueryDevtools', () => {
expect(setPositionMock).toHaveBeenCalledWith('left')
})

it('should forward "initialIsOpen" to the devtools instance', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(<PreactQueryDevtools client={queryClient} initialIsOpen={true} />)

expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
})

it('should default "initialIsOpen" to "false" when the prop is omitted', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(<PreactQueryDevtools client={queryClient} />)

expect(setInitialIsOpenMock).toHaveBeenCalledWith(false)
})

it('should forward "errorTypes" to the devtools instance', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()
const errorTypes = [
{ name: 'Network', initializer: () => new Error('Network') },
]

render(<PreactQueryDevtools client={queryClient} errorTypes={errorTypes} />)

expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
})

it('should default "errorTypes" to an empty array when the prop is omitted', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(<PreactQueryDevtools client={queryClient} />)

expect(setErrorTypesMock).toHaveBeenCalledWith([])
})

it('should forward "theme" to the devtools instance', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(<PreactQueryDevtools client={queryClient} theme="dark" />)

expect(setThemeMock).toHaveBeenCalledWith('dark')
})

it('should forward the resolved "QueryClient" via "setClient"', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(<PreactQueryDevtools client={queryClient} />)

expect(setClientMock).toHaveBeenCalledWith(queryClient)
})

it('should forward "styleNonce" to the devtools constructor', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(<PreactQueryDevtools client={queryClient} styleNonce="abc" />)

expect(TanstackQueryDevtools).toHaveBeenCalledWith(
expect.objectContaining({ styleNonce: 'abc' }),
)
})

it('should forward "shadowDOMTarget" to the devtools constructor', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()
const shadowDOMTarget = document
.createElement('div')
.attachShadow({ mode: 'open' })

render(
<PreactQueryDevtools
client={queryClient}
shadowDOMTarget={shadowDOMTarget}
/>,
)

expect(TanstackQueryDevtools).toHaveBeenCalledWith(
expect.objectContaining({ shadowDOMTarget }),
)
})

it('should forward "hideDisabledQueries" to the devtools constructor', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

render(
<PreactQueryDevtools client={queryClient} hideDisabledQueries={true} />,
)

expect(TanstackQueryDevtools).toHaveBeenCalledWith(
expect.objectContaining({ hideDisabledQueries: true }),
)
})

it('should call "unmount" on the devtools instance when the component unmounts', async () => {
const { PreactQueryDevtools } = await import('../PreactQueryDevtools')
const queryClient = new QueryClient()

const { unmount } = render(<PreactQueryDevtools client={queryClient} />)
unmount()

expect(unmountMock).toHaveBeenCalled()
})

it('should return null in non-development environments', async () => {
vi.stubEnv('NODE_ENV', 'production')
vi.resetModules()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { render } from '@testing-library/preact'
import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
import type { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'

const mountMock = vi.fn()
const unmountMock = vi.fn()
Expand Down Expand Up @@ -63,6 +63,170 @@ describe('PreactQueryDevtoolsPanel', () => {
expect(mountMock).toHaveBeenCalled()
})

it('should forward "onClose" to the devtools instance', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()
const onClose = vi.fn()

render(<PreactQueryDevtoolsPanel client={queryClient} onClose={onClose} />)

expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function))
})

it('should default "onClose" to a no-op function when the prop is omitted', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

render(<PreactQueryDevtoolsPanel client={queryClient} />)

expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function))
})

it('should forward "errorTypes" to the devtools instance', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()
const errorTypes = [
{ name: 'Network', initializer: () => new Error('Network') },
]

render(
<PreactQueryDevtoolsPanel client={queryClient} errorTypes={errorTypes} />,
)

expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
})

it('should default "errorTypes" to an empty array when the prop is omitted', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

render(<PreactQueryDevtoolsPanel client={queryClient} />)

expect(setErrorTypesMock).toHaveBeenCalledWith([])
})

it('should forward "theme" to the devtools instance', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

render(<PreactQueryDevtoolsPanel client={queryClient} theme="dark" />)

expect(setThemeMock).toHaveBeenCalledWith('dark')
})

it('should forward the resolved "QueryClient" via "setClient"', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

render(<PreactQueryDevtoolsPanel client={queryClient} />)

expect(setClientMock).toHaveBeenCalledWith(queryClient)
})

it('should forward "styleNonce" to the devtools constructor', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

render(<PreactQueryDevtoolsPanel client={queryClient} styleNonce="abc" />)

expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
expect.objectContaining({ styleNonce: 'abc' }),
)
})

it('should forward "shadowDOMTarget" to the devtools constructor', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()
const shadowDOMTarget = document
.createElement('div')
.attachShadow({ mode: 'open' })

render(
<PreactQueryDevtoolsPanel
client={queryClient}
shadowDOMTarget={shadowDOMTarget}
/>,
)

expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
expect.objectContaining({ shadowDOMTarget }),
)
})

it('should forward "hideDisabledQueries" to the devtools constructor', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

render(
<PreactQueryDevtoolsPanel
client={queryClient}
hideDisabledQueries={true}
/>,
)

expect(TanstackQueryDevtoolsPanel).toHaveBeenCalledWith(
expect.objectContaining({ hideDisabledQueries: true }),
)
})

it('should preserve the default container height when "style" omits "height"', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

const { container } = render(
<PreactQueryDevtoolsPanel
client={queryClient}
style={{ width: '300px' }}
/>,
)

expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
height: '500px',
width: '300px',
})
})

it('should let "style" override the default container height on the rendered element', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

const { container } = render(
<PreactQueryDevtoolsPanel
client={queryClient}
style={{ width: '300px', height: '300px' }}
/>,
)

expect(container.querySelector('.tsqd-parent-container')).toHaveStyle({
height: '300px',
width: '300px',
})
})

it('should call "unmount" on the devtools instance when the component unmounts', async () => {
const { PreactQueryDevtoolsPanel } =
await import('../PreactQueryDevtoolsPanel')
const queryClient = new QueryClient()

const { unmount } = render(
<PreactQueryDevtoolsPanel client={queryClient} />,
)
unmount()

expect(unmountMock).toHaveBeenCalled()
})

it('should return null in non-development environments', async () => {
vi.stubEnv('NODE_ENV', 'production')
vi.resetModules()
Expand Down
Loading