Skip to content

Commit

Permalink
test(svelte-query): Refactor test logic (#7744)
Browse files Browse the repository at this point in the history
* test(svelte-query): Simplify test logic

* Improve createQueries tests
  • Loading branch information
lachlancollins authored Jul 17, 2024
1 parent 5c50ca7 commit 01212de
Show file tree
Hide file tree
Showing 32 changed files with 262 additions and 205 deletions.
2 changes: 1 addition & 1 deletion examples/svelte/auto-refetching/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
transition:: {!$todos.isFetching ? 'all .3s ease' : 'none'};
border-radius: 100%;
transform: 'scale(2)"
/>
></span>
</div>
</label>
<h2>Todo List</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
return undefined
},
})
const { error }: { error: any } = $query
</script>

{#if $query.isPending}
Loading...
{/if}
{#if $query.error}
<span>Error: {error.message}</span>
<span>Error: {$query.error.message}</span>
{/if}
{#if $query.isSuccess}
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import { sleep } from '../utils'
import type { Writable } from 'svelte/store'
export let key: Array<string>
export let states: Writable<Array<string>>
const state = createQuery({
queryKey: key,
const query = createQuery({
queryKey: ['test'],
queryFn: async () => {
states.update((s) => [...s, 'fetching'])
await sleep(10)
Expand All @@ -17,7 +16,5 @@
})
</script>

<div>
<h1>{$state.data}</h1>
<h2>fetchStatus: {$state.fetchStatus}</h2>
</div>
<div>{$query.data}</div>
<div>fetchStatus: {$query.fetchStatus}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let onSuccess: () => Promise<void>
export let states: Writable<Array<string>>
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions} {onSuccess}>
<AwaitOnSuccess {key} {states} />
<AwaitOnSuccess {states} />
</PersistQueryClientProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import type { Writable } from 'svelte/store'
import type { StatusResult } from '../utils'
export let key: Array<string>
export let states: Writable<Array<StatusResult<string>>>
export let fetched: Writable<boolean>
const state = createQuery({
queryKey: key,
const query = createQuery({
queryKey: ['test'],
queryFn: async () => {
fetched.set(true)
await sleep(10)
Expand All @@ -19,10 +18,8 @@
staleTime: Infinity,
})
$: states.update((prev) => [...prev, $state])
$: states.update((prev) => [...prev, $query])
</script>

<div>
<h1>data: {$state.data ?? 'null'}</h1>
<h2>fetchStatus: {$state.fetchStatus}</h2>
</div>
<div>data: {$query.data ?? 'null'}</div>
<div>fetchStatus: {$query.fetchStatus}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let states: Writable<Array<StatusResult<string>>>
export let fetched: Writable<boolean>
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
<FreshData {key} {states} {fetched} />
<FreshData {states} {fetched} />
</PersistQueryClientProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import type { Writable } from 'svelte/store'
import type { StatusResult } from '../utils'
export let key: Array<string>
export let states: Writable<Array<StatusResult<string>>>
const state = createQuery({
queryKey: key,
const query = createQuery({
queryKey: ['test'],
queryFn: async () => {
await sleep(10)
return 'fetched'
Expand All @@ -20,10 +19,8 @@
initialDataUpdatedAt: 1,
})
$: states.update((prev) => [...prev, $state])
$: states.update((prev) => [...prev, $query])
</script>

<div>
<h1>{$state.data}</h1>
<h2>fetchStatus: {$state.fetchStatus}</h2>
</div>
<div>{$query.data}</div>
<div>fetchStatus: {$query.fetchStatus}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let states: Writable<Array<StatusResult<string>>>
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
<InitialData {key} {states} />
<InitialData {states} />
</PersistQueryClientProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
import { createQuery } from '@tanstack/svelte-query'
import { sleep } from '../utils'
export let key: Array<string>
const state = createQuery({
queryKey: key,
const query = createQuery({
queryKey: ['test'],
queryFn: async () => {
await sleep(10)
return 'fetched'
},
})
</script>

<div>
<h1>{$state.data}</h1>
<h2>fetchStatus: {$state.fetchStatus}</h2>
</div>
<div>{$query.data}</div>
<div>fetchStatus: {$query.fetchStatus}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let onSuccess: () => void
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions} {onSuccess}>
<OnSuccess {key} />
<OnSuccess />
</PersistQueryClientProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import InitialData from './InitialData/Provider.svelte'
import RemoveCache from './RemoveCache/Provider.svelte'
import RestoreCache from './RestoreCache/Provider.svelte'
import UseQueries from './UseQueries/Provider.svelte'
import { createQueryClient, queryKey, sleep } from './utils'
import { createQueryClient, sleep } from './utils'

import type {
PersistedClient,
Expand Down Expand Up @@ -56,12 +56,11 @@ const createMockErrorPersister = (

describe('PersistQueryClientProvider', () => {
test('restores cache from persister', async () => {
const key = queryKey()
const statesStore: Writable<Array<StatusResult<string>>> = writable([])

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryKey: ['test'],
queryFn: () => Promise.resolve('hydrated'),
})

Expand All @@ -75,7 +74,6 @@ describe('PersistQueryClientProvider', () => {
props: {
queryClient,
persistOptions: { persister },
key,
states: statesStore,
},
})
Expand Down Expand Up @@ -113,12 +111,11 @@ describe('PersistQueryClientProvider', () => {
})

test('should also put useQueries into idle state', async () => {
const key = queryKey()
const statesStore: Writable<Array<StatusResult<string>>> = writable([])

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryKey: ['test'],
queryFn: () => Promise.resolve('hydrated'),
})

Expand All @@ -132,7 +129,6 @@ describe('PersistQueryClientProvider', () => {
props: {
queryClient,
persistOptions: { persister },
key,
states: statesStore,
},
})
Expand Down Expand Up @@ -171,12 +167,11 @@ describe('PersistQueryClientProvider', () => {
})

test('should show initialData while restoring', async () => {
const key = queryKey()
const statesStore: Writable<Array<StatusResult<string>>> = writable([])

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryKey: ['test'],
queryFn: () => Promise.resolve('hydrated'),
})

Expand All @@ -190,7 +185,6 @@ describe('PersistQueryClientProvider', () => {
props: {
queryClient,
persistOptions: { persister },
key,
states: statesStore,
},
})
Expand Down Expand Up @@ -228,12 +222,11 @@ describe('PersistQueryClientProvider', () => {
})

test('should not refetch after restoring when data is fresh', async () => {
const key = queryKey()
const statesStore: Writable<Array<StatusResult<string>>> = writable([])

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryKey: ['test'],
queryFn: () => Promise.resolve('hydrated'),
})

Expand All @@ -249,7 +242,6 @@ describe('PersistQueryClientProvider', () => {
props: {
queryClient,
persistOptions: { persister },
key,
states: statesStore,
fetched,
},
Expand Down Expand Up @@ -277,11 +269,9 @@ describe('PersistQueryClientProvider', () => {
})

test('should call onSuccess after successful restoring', async () => {
const key = queryKey()

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryKey: ['test'],
queryFn: () => Promise.resolve('hydrated'),
})

Expand All @@ -297,7 +287,6 @@ describe('PersistQueryClientProvider', () => {
props: {
queryClient,
persistOptions: { persister },
key,
onSuccess,
},
})
Expand All @@ -310,11 +299,9 @@ describe('PersistQueryClientProvider', () => {
})

test('should await onSuccess after successful restoring', async () => {
const key = queryKey()

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryKey: ['test'],
queryFn: () => Promise.resolve('hydrated'),
})

Expand All @@ -330,7 +317,6 @@ describe('PersistQueryClientProvider', () => {
props: {
queryClient,
persistOptions: { persister },
key,
states: statesStore,
onSuccess: async () => {
statesStore.update((s) => [...s, 'onSuccess'])
Expand All @@ -354,7 +340,6 @@ describe('PersistQueryClientProvider', () => {
})

test('should remove cache after non-successful restoring', async () => {
const key = queryKey()
const consoleMock = vi.spyOn(console, 'error')
const consoleWarn = vi
.spyOn(console, 'warn')
Expand All @@ -367,7 +352,7 @@ describe('PersistQueryClientProvider', () => {
const [error, persister] = createMockErrorPersister(removeClient)

const rendered = render(RemoveCache, {
props: { queryClient, persistOptions: { persister }, key },
props: { queryClient, persistOptions: { persister } },
})

await waitFor(() => rendered.getByText('fetched'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
<RemoveCache {key} />
<RemoveCache />
</PersistQueryClientProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
import { createQuery } from '@tanstack/svelte-query'
import { sleep } from '../utils'
export let key: Array<string>
const state = createQuery({
queryKey: key,
const query = createQuery({
queryKey: ['test'],
queryFn: async () => {
await sleep(10)
return 'fetched'
},
})
</script>

<div>
<h1>{$state.data}</h1>
<h2>fetchStatus: {$state.fetchStatus}</h2>
</div>
<div>{$query.data}</div>
<div>fetchStatus: {$query.fetchStatus}</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
export let queryClient: QueryClient
export let persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'>
export let key: Array<string>
export let states: Writable<Array<StatusResult<string>>>
</script>

<PersistQueryClientProvider client={queryClient} {persistOptions}>
<RestoreCache {key} {states} />
<RestoreCache {states} />
</PersistQueryClientProvider>
Loading

0 comments on commit 01212de

Please sign in to comment.