-
-
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): Increase test coverage (#7659)
* tests(svelte-query): Add createInfiniteQuery, useIsMutating * Add infiniteQueryOptions, update context
- Loading branch information
1 parent
f1cdea9
commit 941ba88
Showing
12 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
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,5 @@ | ||
<script lang="ts"> | ||
import { getQueryClientContext } from '../../src/context' | ||
getQueryClientContext() | ||
</script> |
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,18 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { describe, expect, test } from 'vitest' | ||
import { render } from '@testing-library/svelte' | ||
import { getIsRestoringContext } from '../../src/context' | ||
import BaseExample from './BaseExample.svelte' | ||
|
||
describe('getQueryClientContext', () => { | ||
test('Throw when called without a client in context', () => { | ||
expect(() => render(BaseExample)).toThrowError( | ||
'No QueryClient was found in Svelte context. Did you forget to wrap your component with QueryClientProvider?', | ||
) | ||
}) | ||
}) | ||
|
||
describe('getIsRestoringContext', () => { | ||
it('Should not throw when called outside of a component', () => { | ||
expect(() => getIsRestoringContext()).to.not.throw() | ||
test('Do not throw when called outside of a component', () => { | ||
expect(() => getIsRestoringContext()).not.toThrowError() | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
packages/svelte-query/tests/createInfiniteQuery/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,19 @@ | ||
<script lang="ts"> | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { createInfiniteQuery } from '../../src/createInfiniteQuery' | ||
const queryClient = new QueryClient() | ||
const query = createInfiniteQuery( | ||
{ | ||
queryKey: ['test'], | ||
queryFn: ({ pageParam }) => Number(pageParam), | ||
getNextPageParam: (lastPage) => lastPage + 1, | ||
initialPageParam: 0, | ||
}, | ||
queryClient, | ||
) | ||
</script> | ||
|
||
<div>Data: {JSON.stringify($query.data)}</div> | ||
<div>Status: {$query.status}</div> |
19 changes: 19 additions & 0 deletions
19
packages/svelte-query/tests/createInfiniteQuery/createInfiniteQuery.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,19 @@ | ||
import { describe, test } from 'vitest' | ||
import { render } from '@testing-library/svelte' | ||
|
||
import { sleep } from '../utils' | ||
import BaseExample from './BaseExample.svelte' | ||
|
||
describe('createQuery', () => { | ||
test('Render and wait for success', async () => { | ||
const rendered = render(BaseExample) | ||
|
||
await rendered.findByText('Data: undefined') | ||
await rendered.findByText('Status: pending') | ||
|
||
await sleep(20) | ||
|
||
await rendered.findByText('Data: {"pages":[0],"pageParams":[0]}') | ||
await rendered.findByText('Status: success') | ||
}) | ||
}) |
4 changes: 2 additions & 2 deletions
4
packages/svelte-query/tests/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
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
28 changes: 28 additions & 0 deletions
28
packages/svelte-query/tests/infiniteQueryOptions/infiniteQueryOptions.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,28 @@ | ||
import { describe, expectTypeOf, test } from 'vitest' | ||
import { type InfiniteData } from '@tanstack/query-core' | ||
import { infiniteQueryOptions } from '../../src/infiniteQueryOptions' | ||
|
||
describe('queryOptions', () => { | ||
test('Should not allow excess properties', () => { | ||
infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('data'), | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
// @ts-expect-error this is a good error, because stallTime does not exist! | ||
stallTime: 1000, | ||
}) | ||
}) | ||
test('Should infer types for callbacks', () => { | ||
infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('data'), | ||
staleTime: 1000, | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
select: (data) => { | ||
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() | ||
}, | ||
}) | ||
}) | ||
}) |
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
24 changes: 24 additions & 0 deletions
24
packages/svelte-query/tests/useIsMutating/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,24 @@ | ||
<script lang="ts"> | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { createMutation } from '../../src/createMutation' | ||
import { sleep } from '../utils' | ||
import { useIsMutating } from '../../src/useIsMutating' | ||
const queryClient = new QueryClient() | ||
const isMutating = useIsMutating(undefined, queryClient) | ||
const mutation = createMutation( | ||
{ | ||
mutationKey: ['mutation1'], | ||
mutationFn: async () => { | ||
await sleep(20) | ||
return 'data' | ||
}, | ||
}, | ||
queryClient, | ||
) | ||
</script> | ||
|
||
<button on:click={() => $mutation.mutate()}>Trigger</button> | ||
|
||
<div>isMutating: {$isMutating}</div> |
14 changes: 14 additions & 0 deletions
14
packages/svelte-query/tests/useIsMutating/useIsMutating.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('isMutating: 0') | ||
fireEvent.click(rendered.getByRole('button', { name: /Trigger/i })) | ||
await rendered.findByText('isMutating: 1') | ||
await rendered.findByText('isMutating: 0') | ||
}) | ||
}) |
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