Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor placeholderData test
  • Loading branch information
lachlancollins committed Jul 6, 2023
commit 637c70b96b1499ef3a8dcec2625ca4701c67751a
14 changes: 7 additions & 7 deletions packages/svelte-query/src/__tests__/CreateQuery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
{:else if $query.isError}
<p>Error</p>
{:else if $query.isSuccess}
<p>{$query.data}</p>
{#if Array.isArray($query.data)}
{#each $query.data as item}
<p>{item}</p>
{/each}
{:else}
<p>{$query.data}</p>
{/if}
{/if}

<ul>
{#each $query.data ?? [] as entry}
<li>id: {entry.id}</li>
{/each}
</ul>
91 changes: 45 additions & 46 deletions packages/svelte-query/src/__tests__/createQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { derived, writable } from 'svelte/store'
import { QueryClient } from '@tanstack/query-core'
import CreateQuery from './CreateQuery.svelte'
import { sleep } from './utils'
import type { CreateQueryOptions } from '../types'

describe('createQuery', () => {
test('Render and wait for success', async () => {
Expand All @@ -30,56 +29,14 @@ describe('createQuery', () => {
})
})

test('Keep previous data when returned as placeholder data', async () => {
const optionsStore = writable({
queryKey: ['test', [1]],
queryFn: async ({ queryKey }) => {
await sleep(10)
const ids = queryKey[1]
if (!ids || !Array.isArray(ids)) return []
return ids.map((id) => ({ id }))
},
placeholderData: (previousData: { id: number }[]) => previousData,
}) satisfies CreateQueryOptions

const rendered = render(CreateQuery, {
props: {
options: optionsStore,
queryClient: new QueryClient(),
},
})

await waitFor(() => {
expect(rendered.queryByText('id: 1')).not.toBeInTheDocument()
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
})

await waitFor(() => {
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
})

optionsStore.update((o) => ({ ...o, queryKey: ['test', [1, 2]] }))

await waitFor(() => {
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
})

await waitFor(() => {
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
expect(rendered.queryByText('id: 2')).toBeInTheDocument()
})
})

test('Accept a writable store for options', async () => {
const optionsStore = writable({
queryKey: ['test'],
queryFn: async () => {
await sleep(10)
return 'Success'
},
}) satisfies CreateQueryOptions
})

const rendered = render(CreateQuery, {
props: {
Expand All @@ -102,7 +59,7 @@ describe('createQuery', () => {
await sleep(10)
return 'Success'
},
})) satisfies CreateQueryOptions
}))

const rendered = render(CreateQuery, {
props: {
Expand All @@ -125,7 +82,7 @@ describe('createQuery', () => {
await sleep(10)
return `Success ${$store}`
},
})) satisfies CreateQueryOptions
}))

const rendered = render(CreateQuery, {
props: {
Expand Down Expand Up @@ -155,4 +112,46 @@ describe('createQuery', () => {
expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
})
})

test('Keep previous data when returned as placeholder data', async () => {
const writableStore = writable<number[]>([1])

const derivedStore = derived(writableStore, ($store) => ({
queryKey: ['test', $store],
queryFn: async () => {
await sleep(10)
return $store.map((id) => `Success ${id}`)
},
placeholderData: (previousData: string) => previousData,
}))

const rendered = render(CreateQuery, {
props: {
options: derivedStore,
queryClient: new QueryClient(),
},
})

await waitFor(() => {
expect(rendered.queryByText('Success 1')).not.toBeInTheDocument()
expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
})

await waitFor(() => {
expect(rendered.queryByText('Success 1')).toBeInTheDocument()
expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
})

writableStore.set([1, 2])

await waitFor(() => {
expect(rendered.queryByText('Success 1')).toBeInTheDocument()
expect(rendered.queryByText('Success 2')).not.toBeInTheDocument()
})

await waitFor(() => {
expect(rendered.queryByText('Success 1')).toBeInTheDocument()
expect(rendered.queryByText('Success 2')).toBeInTheDocument()
})
})
})