|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { render } from '@testing-library/svelte' |
3 | 3 | import { QueryClient } from '@tanstack/svelte-query' |
4 | 4 | import SvelteQueryDevtools from '../src/Devtools.svelte' |
5 | 5 | import Wrapper from './Wrapper.svelte' |
| 6 | +import type { TanstackQueryDevtools } from '@tanstack/query-devtools' |
| 7 | + |
| 8 | +const setButtonPositionMock = vi.fn() |
| 9 | +const setPositionMock = vi.fn() |
| 10 | +const setInitialIsOpenMock = vi.fn() |
| 11 | +const setErrorTypesMock = vi.fn() |
| 12 | + |
| 13 | +vi.mock('@tanstack/query-devtools', () => ({ |
| 14 | + TanstackQueryDevtools: vi.fn(function (this: TanstackQueryDevtools) { |
| 15 | + this.mount = vi.fn() |
| 16 | + this.unmount = vi.fn() |
| 17 | + this.setButtonPosition = setButtonPositionMock |
| 18 | + this.setPosition = setPositionMock |
| 19 | + this.setInitialIsOpen = setInitialIsOpenMock |
| 20 | + this.setErrorTypes = setErrorTypesMock |
| 21 | + }), |
| 22 | +})) |
6 | 23 |
|
7 | 24 | describe('SvelteQueryDevtools', () => { |
| 25 | + afterEach(() => { |
| 26 | + vi.clearAllMocks() |
| 27 | + }) |
| 28 | + |
8 | 29 | it('should render the parent container without throwing in non-development environments', () => { |
9 | 30 | const queryClient = new QueryClient() |
10 | 31 |
|
@@ -36,4 +57,98 @@ describe('SvelteQueryDevtools', () => { |
36 | 57 | render(SvelteQueryDevtools, { props: { client: queryClient } }), |
37 | 58 | ).not.toThrow() |
38 | 59 | }) |
| 60 | + |
| 61 | + it('should forward the initial "position" to the devtools instance', async () => { |
| 62 | + const queryClient = new QueryClient() |
| 63 | + render(SvelteQueryDevtools, { |
| 64 | + props: { client: queryClient, position: 'left' }, |
| 65 | + }) |
| 66 | + await vi.dynamicImportSettled() |
| 67 | + |
| 68 | + expect(setPositionMock).toHaveBeenCalledWith('left') |
| 69 | + }) |
| 70 | + |
| 71 | + it('should forward the initial "buttonPosition" to the devtools instance', async () => { |
| 72 | + const queryClient = new QueryClient() |
| 73 | + render(SvelteQueryDevtools, { |
| 74 | + props: { client: queryClient, buttonPosition: 'top-left' }, |
| 75 | + }) |
| 76 | + await vi.dynamicImportSettled() |
| 77 | + |
| 78 | + expect(setButtonPositionMock).toHaveBeenCalledWith('top-left') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should forward the initial "initialIsOpen" to the devtools instance', async () => { |
| 82 | + const queryClient = new QueryClient() |
| 83 | + render(SvelteQueryDevtools, { |
| 84 | + props: { client: queryClient, initialIsOpen: true }, |
| 85 | + }) |
| 86 | + await vi.dynamicImportSettled() |
| 87 | + |
| 88 | + expect(setInitialIsOpenMock).toHaveBeenCalledWith(true) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should forward the initial "errorTypes" to the devtools instance', async () => { |
| 92 | + const queryClient = new QueryClient() |
| 93 | + const errorTypes = [{ name: 'Error', initializer: () => new Error() }] |
| 94 | + render(SvelteQueryDevtools, { |
| 95 | + props: { client: queryClient, errorTypes }, |
| 96 | + }) |
| 97 | + await vi.dynamicImportSettled() |
| 98 | + |
| 99 | + expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should forward a "position" change to the devtools instance after mount', async () => { |
| 103 | + const queryClient = new QueryClient() |
| 104 | + const { rerender } = render(SvelteQueryDevtools, { |
| 105 | + props: { client: queryClient, position: 'bottom' }, |
| 106 | + }) |
| 107 | + await vi.dynamicImportSettled() |
| 108 | + setPositionMock.mockClear() |
| 109 | + |
| 110 | + await rerender({ client: queryClient, position: 'top' }) |
| 111 | + |
| 112 | + expect(setPositionMock).toHaveBeenCalledWith('top') |
| 113 | + }) |
| 114 | + |
| 115 | + it('should forward a "buttonPosition" change to the devtools instance after mount', async () => { |
| 116 | + const queryClient = new QueryClient() |
| 117 | + const { rerender } = render(SvelteQueryDevtools, { |
| 118 | + props: { client: queryClient, buttonPosition: 'bottom-right' }, |
| 119 | + }) |
| 120 | + await vi.dynamicImportSettled() |
| 121 | + setButtonPositionMock.mockClear() |
| 122 | + |
| 123 | + await rerender({ client: queryClient, buttonPosition: 'top-left' }) |
| 124 | + |
| 125 | + expect(setButtonPositionMock).toHaveBeenCalledWith('top-left') |
| 126 | + }) |
| 127 | + |
| 128 | + it('should forward an "initialIsOpen" change to the devtools instance after mount', async () => { |
| 129 | + const queryClient = new QueryClient() |
| 130 | + const { rerender } = render(SvelteQueryDevtools, { |
| 131 | + props: { client: queryClient, initialIsOpen: false }, |
| 132 | + }) |
| 133 | + await vi.dynamicImportSettled() |
| 134 | + setInitialIsOpenMock.mockClear() |
| 135 | + |
| 136 | + await rerender({ client: queryClient, initialIsOpen: true }) |
| 137 | + |
| 138 | + expect(setInitialIsOpenMock).toHaveBeenCalledWith(true) |
| 139 | + }) |
| 140 | + |
| 141 | + it('should forward an "errorTypes" change to the devtools instance after mount', async () => { |
| 142 | + const queryClient = new QueryClient() |
| 143 | + const { rerender } = render(SvelteQueryDevtools, { |
| 144 | + props: { client: queryClient, errorTypes: [] }, |
| 145 | + }) |
| 146 | + await vi.dynamicImportSettled() |
| 147 | + setErrorTypesMock.mockClear() |
| 148 | + |
| 149 | + const errorTypes = [{ name: 'Error', initializer: () => new Error() }] |
| 150 | + await rerender({ client: queryClient, errorTypes }) |
| 151 | + |
| 152 | + expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes) |
| 153 | + }) |
39 | 154 | }) |
0 commit comments