-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(svelte-query): Reorganise for multiple components per test (#7654)
* Move tests * Add useIsFetching test, disabled createQuery test * Add QueryCacheProvider test
- Loading branch information
1 parent
46bed74
commit f1cdea9
Showing
17 changed files
with
173 additions
and
32 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/svelte-query/tests/QueryClientProvider/ChildComponent.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
packages/svelte-query/tests/QueryClientProvider/ParentComponent.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
20 changes: 20 additions & 0 deletions
20
packages/svelte-query/tests/QueryClientProvider/QueryClientProvider.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
4 changes: 2 additions & 2 deletions
4
packages/svelte-query/tests/context.test.ts → ...velte-query/tests/context/context.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { getIsRestoringContext } from '../src/context' | ||
import { getIsRestoringContext } from '../../src/context' | ||
|
||
describe('getIsRestoringContext', () => { | ||
it('Should not throw when called outside of a component', async () => { | ||
it('Should not throw when called outside of a component', () => { | ||
expect(() => getIsRestoringContext()).to.not.throw() | ||
}) | ||
}) |
6 changes: 3 additions & 3 deletions
6
.../svelte-query/tests/CreateMutation.svelte → ...y/tests/createMutation/BaseExample.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...svelte-query/tests/createMutation.test.ts → ...sts/createMutation/createMutation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...s/svelte-query/tests/CreateQueries.svelte → ...ry/tests/createQueries/BaseExample.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ges/svelte-query/tests/CreateQuery.svelte → ...uery/tests/createQuery/BaseExample.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/svelte-query/tests/createQuery/DisabledExample.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script lang="ts"> | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { derived, writable } from 'svelte/store' | ||
import { createQuery } from '../../src/createQuery' | ||
import { queryKey, sleep } from '../utils' | ||
const queryClient = new QueryClient() | ||
const key = queryKey() | ||
const count = writable(0) | ||
const options = derived(count, ($count) => ({ | ||
queryKey: [key, $count], | ||
queryFn: async () => { | ||
await sleep(5) | ||
return $count | ||
}, | ||
enabled: $count === 0, | ||
})) | ||
const query = createQuery(options, queryClient) | ||
</script> | ||
|
||
<button on:click={() => ($count += 1)}>Increment</button> | ||
<div>Data: {$query.data ?? 'undefined'}</div> | ||
<div>Count: {$count}</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/svelte-query/tests/queryOptions.test.ts → ...y/tests/queryOptions/queryOptions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/svelte-query/tests/useIsFetching/BaseExample.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<script lang="ts"> | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { derived, writable } from 'svelte/store' | ||
import { createQuery } from '../../src/createQuery' | ||
import { useIsFetching } from '../../src/useIsFetching' | ||
import { queryKey, sleep } from '../utils' | ||
const queryClient = new QueryClient() | ||
const key = queryKey() | ||
const ready = writable(false) | ||
const isFetching = useIsFetching(undefined, queryClient) | ||
const options = derived(ready, ($ready) => ({ | ||
queryKey: [key], | ||
queryFn: async () => { | ||
await sleep(50) | ||
return 'test' | ||
}, | ||
enabled: $ready, | ||
})) | ||
const query = createQuery(options, queryClient) | ||
$: console.log($isFetching) | ||
</script> | ||
|
||
<button on:click={() => ($ready = true)}>setReady</button> | ||
<div>isFetching: {$isFetching}</div> | ||
|
||
{#if $query.isSuccess} | ||
{$query.data} | ||
{/if} |
14 changes: 14 additions & 0 deletions
14
packages/svelte-query/tests/useIsFetching/useIsFetching.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { describe, test } from 'vitest' | ||
import { fireEvent, render } from '@testing-library/svelte' | ||
import BaseExample from './BaseExample.svelte' | ||
|
||
describe('useIsFetching', () => { | ||
test('should update as queries start and stop fetching', async () => { | ||
const rendered = render(BaseExample) | ||
|
||
await rendered.findByText('isFetching: 0') | ||
fireEvent.click(rendered.getByRole('button', { name: /setReady/i })) | ||
await rendered.findByText('isFetching: 1') | ||
await rendered.findByText('isFetching: 0') | ||
}) | ||
}) |
8 changes: 4 additions & 4 deletions
8
...velte-query/tests/UseMutationState.svelte → ...tests/useMutationState/BaseExample.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters