Skip to content

Commit

Permalink
Add QueryCacheProvider test
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins committed Jul 1, 2024
1 parent 00f0d44 commit 6d6a378
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import { createQuery } from '../../src/createQuery'
import { sleep } from '../utils'
const query = createQuery({
queryKey: ['hello'],
queryFn: async () => {
await sleep(10)
return 'test'
},
})
</script>

<div>{$query.data}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import { QueryClient } from '@tanstack/query-core'
import QueryClientProvider from '../../src/QueryClientProvider.svelte'
import ChildComponent from './ChildComponent.svelte'
import type { QueryCache } from '@tanstack/query-core'
export let queryCache: QueryCache
const queryClient = new QueryClient({ queryCache })
</script>

<QueryClientProvider client={queryClient}>
<ChildComponent />
</QueryClientProvider>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, test } from 'vitest'
import { render, waitFor } from '@testing-library/svelte'
import { QueryCache } from '@tanstack/query-core'
import ParentComponent from './ParentComponent.svelte'

describe('QueryClientProvider', () => {
test('Sets a specific cache for all queries to use', async () => {
const queryCache = new QueryCache()

const rendered = render(ParentComponent, {
props: {
queryCache: queryCache,
},
})

await waitFor(() => rendered.getByText('test'))

expect(queryCache.find({ queryKey: ['hello'] })).toBeDefined()
})
})

0 comments on commit 6d6a378

Please sign in to comment.