@tanstack/svelte-query 4.29.5 → 4.29.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/__tests__/CreateMutation.svelte +10 -0
- package/build/lib/__tests__/CreateMutation.svelte.d.ts +17 -0
- package/build/lib/__tests__/CreateQueries.svelte +14 -0
- package/build/lib/__tests__/CreateQueries.svelte.d.ts +17 -0
- package/build/lib/__tests__/CreateQuery.svelte +16 -0
- package/build/lib/__tests__/CreateQuery.svelte.d.ts +17 -0
- package/build/lib/__tests__/createMutation.test.d.ts +1 -0
- package/build/lib/__tests__/createMutation.test.js +19 -0
- package/build/lib/__tests__/createQueries.test.d.ts +1 -0
- package/build/lib/__tests__/createQueries.test.js +36 -0
- package/build/lib/__tests__/createQuery.test.d.ts +1 -0
- package/build/lib/__tests__/createQuery.test.js +25 -0
- package/build/lib/__tests__/utils.d.ts +23 -0
- package/build/lib/__tests__/utils.js +54 -0
- package/package.json +21 -14
- package/src/__tests__/CreateMutation.svelte +0 -17
- package/src/__tests__/CreateQueries.svelte +0 -18
- package/src/__tests__/CreateQuery.svelte +0 -19
- package/src/__tests__/createMutation.test.ts +0 -24
- package/src/__tests__/createQueries.test.ts +0 -37
- package/src/__tests__/createQuery.test.ts +0 -28
- package/src/__tests__/utils.ts +0 -72
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<script>import { QueryClient } from "@tanstack/query-core";
|
|
2
|
+
import { setQueryClientContext } from "../context";
|
|
3
|
+
import { createMutation } from "../createMutation";
|
|
4
|
+
export let options;
|
|
5
|
+
const queryClient = new QueryClient();
|
|
6
|
+
setQueryClientContext(queryClient);
|
|
7
|
+
const mutation = createMutation(options);
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<button on:click={() => $mutation.mutate()}>Click</button>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { CreateMutationOptions } from '../types';
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
options: CreateMutationOptions;
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type CreateMutationProps = typeof __propDef.props;
|
|
13
|
+
export declare type CreateMutationEvents = typeof __propDef.events;
|
|
14
|
+
export declare type CreateMutationSlots = typeof __propDef.slots;
|
|
15
|
+
export default class CreateMutation extends SvelteComponentTyped<CreateMutationProps, CreateMutationEvents, CreateMutationSlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<script>import { QueryClient } from "@tanstack/query-core";
|
|
2
|
+
import { setQueryClientContext } from "../context";
|
|
3
|
+
import { createQueries } from "../createQueries";
|
|
4
|
+
export let options;
|
|
5
|
+
const queryClient = new QueryClient();
|
|
6
|
+
setQueryClientContext(queryClient);
|
|
7
|
+
const queries = createQueries(options);
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
{#each $queries as query}
|
|
11
|
+
{#if query.isSuccess}
|
|
12
|
+
<p>{query.data}</p>
|
|
13
|
+
{/if}
|
|
14
|
+
{/each}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { QueriesOptions } from '../createQueries';
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
options: readonly [...QueriesOptions<any>];
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type CreateQueriesProps = typeof __propDef.props;
|
|
13
|
+
export declare type CreateQueriesEvents = typeof __propDef.events;
|
|
14
|
+
export declare type CreateQueriesSlots = typeof __propDef.slots;
|
|
15
|
+
export default class CreateQueries extends SvelteComponentTyped<CreateQueriesProps, CreateQueriesEvents, CreateQueriesSlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script>import { QueryClient } from "@tanstack/query-core";
|
|
2
|
+
import { setQueryClientContext } from "../context";
|
|
3
|
+
import { createQuery } from "../createQuery";
|
|
4
|
+
export let options;
|
|
5
|
+
const queryClient = new QueryClient();
|
|
6
|
+
setQueryClientContext(queryClient);
|
|
7
|
+
const query = createQuery(options);
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
{#if $query.isLoading}
|
|
11
|
+
<p>Loading</p>
|
|
12
|
+
{:else if $query.isError}
|
|
13
|
+
<p>Error</p>
|
|
14
|
+
{:else if $query.isSuccess}
|
|
15
|
+
<p>Success</p>
|
|
16
|
+
{/if}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { CreateQueryOptions } from '../types';
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
options: CreateQueryOptions;
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type CreateQueryProps = typeof __propDef.props;
|
|
13
|
+
export declare type CreateQueryEvents = typeof __propDef.events;
|
|
14
|
+
export declare type CreateQuerySlots = typeof __propDef.slots;
|
|
15
|
+
export default class CreateQuery extends SvelteComponentTyped<CreateQueryProps, CreateQueryEvents, CreateQuerySlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { fireEvent, render, waitFor } from '@testing-library/svelte';
|
|
3
|
+
import CreateMutation from './CreateMutation.svelte';
|
|
4
|
+
describe('createMutation', () => {
|
|
5
|
+
it('Call mutate and check function runs', async () => {
|
|
6
|
+
const mutationFn = vi.fn();
|
|
7
|
+
const rendered = render(CreateMutation, {
|
|
8
|
+
props: {
|
|
9
|
+
options: {
|
|
10
|
+
mutationFn,
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
fireEvent.click(rendered.getByRole('button'));
|
|
15
|
+
await waitFor(() => {
|
|
16
|
+
expect(mutationFn).toHaveBeenCalledTimes(1);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { render, waitFor } from '@testing-library/svelte';
|
|
3
|
+
import CreateQueries from './CreateQueries.svelte';
|
|
4
|
+
import { sleep } from './utils';
|
|
5
|
+
describe('createQueries', () => {
|
|
6
|
+
it('Render and wait for success', async () => {
|
|
7
|
+
const rendered = render(CreateQueries, {
|
|
8
|
+
props: {
|
|
9
|
+
options: [
|
|
10
|
+
{
|
|
11
|
+
queryKey: ['key-1'],
|
|
12
|
+
queryFn: async () => {
|
|
13
|
+
await sleep(10);
|
|
14
|
+
return 'Success 1';
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
queryKey: ['key-2'],
|
|
19
|
+
queryFn: async () => {
|
|
20
|
+
await sleep(10);
|
|
21
|
+
return 'Success 2';
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
await waitFor(() => {
|
|
28
|
+
expect(rendered.queryByText('Success 1')).not.toBeInTheDocument();
|
|
29
|
+
expect(rendered.queryByText('Success 2')).not.toBeInTheDocument();
|
|
30
|
+
});
|
|
31
|
+
await waitFor(() => {
|
|
32
|
+
expect(rendered.queryByText('Success 1')).toBeInTheDocument();
|
|
33
|
+
expect(rendered.queryByText('Success 2')).toBeInTheDocument();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { render, waitFor } from '@testing-library/svelte';
|
|
3
|
+
import CreateQuery from './CreateQuery.svelte';
|
|
4
|
+
import { sleep } from './utils';
|
|
5
|
+
describe('createQuery', () => {
|
|
6
|
+
it('Render and wait for success', async () => {
|
|
7
|
+
const rendered = render(CreateQuery, {
|
|
8
|
+
props: {
|
|
9
|
+
options: {
|
|
10
|
+
queryKey: ['test'],
|
|
11
|
+
queryFn: async () => {
|
|
12
|
+
await sleep(10);
|
|
13
|
+
return 'Success';
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
await waitFor(() => {
|
|
19
|
+
expect(rendered.getByText('Loading')).toBeInTheDocument();
|
|
20
|
+
});
|
|
21
|
+
await waitFor(() => {
|
|
22
|
+
expect(rendered.getByText('Success')).toBeInTheDocument();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { QueryClient, type QueryClientConfig, type MutationOptions } from '../index';
|
|
3
|
+
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
4
|
+
export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
|
|
5
|
+
export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
|
|
6
|
+
export declare const mockLogger: {
|
|
7
|
+
log: import("vitest/dist/index-1cfc7f58").x<any[], any>;
|
|
8
|
+
warn: import("vitest/dist/index-1cfc7f58").x<any[], any>;
|
|
9
|
+
error: import("vitest/dist/index-1cfc7f58").x<any[], any>;
|
|
10
|
+
};
|
|
11
|
+
export declare function queryKey(): Array<string>;
|
|
12
|
+
export declare function sleep(timeout: number): Promise<void>;
|
|
13
|
+
export declare function simplefetcher(): Promise<string>;
|
|
14
|
+
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
15
|
+
/**
|
|
16
|
+
* Assert the parameter is of a specific type.
|
|
17
|
+
*/
|
|
18
|
+
export declare function expectType<T>(_: T): void;
|
|
19
|
+
/**
|
|
20
|
+
* Assert the parameter is not typed as `any`
|
|
21
|
+
*/
|
|
22
|
+
export declare function expectTypeNotAny<T>(_: 0 extends 1 & T ? never : T): void;
|
|
23
|
+
export declare function executeMutation(queryClient: QueryClient, options: MutationOptions<any, any, any, any>): Promise<unknown>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
import { act } from '@testing-library/svelte';
|
|
3
|
+
import { QueryClient, } from '../index';
|
|
4
|
+
export function createQueryClient(config) {
|
|
5
|
+
vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
6
|
+
return new QueryClient({ logger: mockLogger, ...config });
|
|
7
|
+
}
|
|
8
|
+
export function mockVisibilityState(value) {
|
|
9
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value);
|
|
10
|
+
}
|
|
11
|
+
export function mockNavigatorOnLine(value) {
|
|
12
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value);
|
|
13
|
+
}
|
|
14
|
+
export const mockLogger = {
|
|
15
|
+
log: vi.fn(),
|
|
16
|
+
warn: vi.fn(),
|
|
17
|
+
error: vi.fn(),
|
|
18
|
+
};
|
|
19
|
+
let queryKeyCount = 0;
|
|
20
|
+
export function queryKey() {
|
|
21
|
+
queryKeyCount++;
|
|
22
|
+
return [`query_${queryKeyCount}`];
|
|
23
|
+
}
|
|
24
|
+
export function sleep(timeout) {
|
|
25
|
+
return new Promise((resolve, _reject) => {
|
|
26
|
+
setTimeout(resolve, timeout);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export async function simplefetcher() {
|
|
30
|
+
await sleep(10);
|
|
31
|
+
return 'test';
|
|
32
|
+
}
|
|
33
|
+
export function setActTimeout(fn, ms) {
|
|
34
|
+
return setTimeout(() => {
|
|
35
|
+
act(() => {
|
|
36
|
+
fn();
|
|
37
|
+
});
|
|
38
|
+
}, ms);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Assert the parameter is of a specific type.
|
|
42
|
+
*/
|
|
43
|
+
export function expectType(_) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Assert the parameter is not typed as `any`
|
|
48
|
+
*/
|
|
49
|
+
export function expectTypeNotAny(_) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
export function executeMutation(queryClient, options) {
|
|
53
|
+
return queryClient.getMutationCache().build(queryClient, options).execute();
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "4.29.
|
|
3
|
+
"version": "4.29.10",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
|
-
"author": "
|
|
5
|
+
"author": "Lachlan Collins",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "tanstack/query",
|
|
8
8
|
"homepage": "https://tanstack.com/query",
|
|
@@ -10,9 +10,25 @@
|
|
|
10
10
|
"type": "github",
|
|
11
11
|
"url": "https://github.com/sponsors/tannerlinsley"
|
|
12
12
|
},
|
|
13
|
+
"type": "module",
|
|
13
14
|
"types": "build/lib/index.d.ts",
|
|
14
15
|
"module": "build/lib/index.js",
|
|
15
|
-
"
|
|
16
|
+
"svelte": "build/lib/index.js",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./build/lib/index.d.ts",
|
|
20
|
+
"import": "./build/lib/index.js",
|
|
21
|
+
"svelte": "./build/lib/index.js",
|
|
22
|
+
"default": "./build/lib/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"build/lib",
|
|
28
|
+
"src",
|
|
29
|
+
"!build/lib/__tests__",
|
|
30
|
+
"!src/__tests__"
|
|
31
|
+
],
|
|
16
32
|
"devDependencies": {
|
|
17
33
|
"@sveltejs/package": "^2.0.2",
|
|
18
34
|
"@sveltejs/vite-plugin-svelte": "^2.0.2",
|
|
@@ -28,26 +44,17 @@
|
|
|
28
44
|
"vitest": "^0.27.1"
|
|
29
45
|
},
|
|
30
46
|
"dependencies": {
|
|
31
|
-
"@tanstack/query-core": "4.29.
|
|
47
|
+
"@tanstack/query-core": "4.29.10"
|
|
32
48
|
},
|
|
33
49
|
"peerDependencies": {
|
|
34
50
|
"svelte": "^3.54.0"
|
|
35
51
|
},
|
|
36
|
-
"exports": {
|
|
37
|
-
"./package.json": "./package.json",
|
|
38
|
-
".": "./build/lib/index.js"
|
|
39
|
-
},
|
|
40
|
-
"svelte": "./build/lib/index.js",
|
|
41
|
-
"files": [
|
|
42
|
-
"build/lib/*",
|
|
43
|
-
"src"
|
|
44
|
-
],
|
|
45
52
|
"scripts": {
|
|
46
53
|
"clean": "rimraf ./build",
|
|
47
54
|
"test:types": "svelte-check --tsconfig ./tsconfig.json",
|
|
48
55
|
"test:eslint": "eslint --ext .svelte,.ts ./src",
|
|
49
56
|
"test:lib": "vitest run --coverage true",
|
|
50
57
|
"test:lib:dev": "vitest watch",
|
|
51
|
-
"build": "svelte-package --input ./src --output ./build/lib
|
|
58
|
+
"build": "svelte-package --input ./src --output ./build/lib"
|
|
52
59
|
}
|
|
53
60
|
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
createMutation,
|
|
4
|
-
QueryClient,
|
|
5
|
-
type CreateMutationOptions,
|
|
6
|
-
} from '../index'
|
|
7
|
-
import { setQueryClientContext } from '../context'
|
|
8
|
-
|
|
9
|
-
export let options: CreateMutationOptions
|
|
10
|
-
|
|
11
|
-
const queryClient = new QueryClient()
|
|
12
|
-
setQueryClientContext(queryClient)
|
|
13
|
-
|
|
14
|
-
const mutation = createMutation(options)
|
|
15
|
-
</script>
|
|
16
|
-
|
|
17
|
-
<button on:click={() => $mutation.mutate()}>Click</button>
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { createQueries, QueryClient } from '../index'
|
|
3
|
-
import { setQueryClientContext } from '../context'
|
|
4
|
-
import type { QueriesOptions } from '../createQueries'
|
|
5
|
-
|
|
6
|
-
export let options: readonly [...QueriesOptions<any>]
|
|
7
|
-
|
|
8
|
-
const queryClient = new QueryClient()
|
|
9
|
-
setQueryClientContext(queryClient)
|
|
10
|
-
|
|
11
|
-
const queries = createQueries(options)
|
|
12
|
-
</script>
|
|
13
|
-
|
|
14
|
-
{#each $queries as query}
|
|
15
|
-
{#if query.isSuccess}
|
|
16
|
-
<p>{query.data}</p>
|
|
17
|
-
{/if}
|
|
18
|
-
{/each}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { createQuery, QueryClient, type CreateQueryOptions } from '../index'
|
|
3
|
-
import { setQueryClientContext } from '../context'
|
|
4
|
-
|
|
5
|
-
export let options: CreateQueryOptions
|
|
6
|
-
|
|
7
|
-
const queryClient = new QueryClient()
|
|
8
|
-
setQueryClientContext(queryClient)
|
|
9
|
-
|
|
10
|
-
const query = createQuery(options)
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
{#if $query.isLoading}
|
|
14
|
-
<p>Loading</p>
|
|
15
|
-
{:else if $query.isError}
|
|
16
|
-
<p>Error</p>
|
|
17
|
-
{:else if $query.isSuccess}
|
|
18
|
-
<p>Success</p>
|
|
19
|
-
{/if}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/svelte'
|
|
3
|
-
import CreateMutation from './CreateMutation.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createMutation', () => {
|
|
7
|
-
it('Call mutate and check function runs', async () => {
|
|
8
|
-
const mutationFn = vi.fn()
|
|
9
|
-
|
|
10
|
-
render(CreateMutation, {
|
|
11
|
-
props: {
|
|
12
|
-
options: {
|
|
13
|
-
mutationFn,
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
fireEvent.click(screen.getByRole('button'))
|
|
19
|
-
|
|
20
|
-
await sleep(20)
|
|
21
|
-
|
|
22
|
-
expect(mutationFn).toHaveBeenCalledTimes(1)
|
|
23
|
-
})
|
|
24
|
-
})
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render, screen } from '@testing-library/svelte'
|
|
3
|
-
import CreateQueries from './CreateQueries.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createQueries', () => {
|
|
7
|
-
it('Render and wait for success', async () => {
|
|
8
|
-
render(CreateQueries, {
|
|
9
|
-
props: {
|
|
10
|
-
options: [
|
|
11
|
-
{
|
|
12
|
-
queryKey: ['key-1'],
|
|
13
|
-
queryFn: async () => {
|
|
14
|
-
await sleep(10)
|
|
15
|
-
return 'Success 1'
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
queryKey: ['key-2'],
|
|
20
|
-
queryFn: async () => {
|
|
21
|
-
await sleep(10)
|
|
22
|
-
return 'Success 2'
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
},
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
expect(screen.queryByText('Success 1')).not.toBeInTheDocument()
|
|
30
|
-
expect(screen.queryByText('Success 2')).not.toBeInTheDocument()
|
|
31
|
-
|
|
32
|
-
await sleep(20)
|
|
33
|
-
|
|
34
|
-
expect(screen.queryByText('Success 1')).toBeInTheDocument()
|
|
35
|
-
expect(screen.queryByText('Success 2')).toBeInTheDocument()
|
|
36
|
-
})
|
|
37
|
-
})
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render, waitFor } from '@testing-library/svelte'
|
|
3
|
-
import CreateQuery from './CreateQuery.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createQuery', () => {
|
|
7
|
-
it('Render and wait for success', async () => {
|
|
8
|
-
const rendered = render(CreateQuery, {
|
|
9
|
-
props: {
|
|
10
|
-
options: {
|
|
11
|
-
queryKey: ['test'],
|
|
12
|
-
queryFn: async () => {
|
|
13
|
-
await sleep(10)
|
|
14
|
-
return 'Success'
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
await waitFor(() => {
|
|
21
|
-
expect(rendered.getByText('Loading')).toBeInTheDocument()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
await waitFor(() => {
|
|
25
|
-
expect(rendered.getByText('Success')).toBeInTheDocument()
|
|
26
|
-
})
|
|
27
|
-
})
|
|
28
|
-
})
|
package/src/__tests__/utils.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { vi } from 'vitest'
|
|
2
|
-
import { act } from '@testing-library/svelte'
|
|
3
|
-
import {
|
|
4
|
-
QueryClient,
|
|
5
|
-
type QueryClientConfig,
|
|
6
|
-
type MutationOptions,
|
|
7
|
-
} from '../index'
|
|
8
|
-
|
|
9
|
-
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
10
|
-
vi.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
11
|
-
return new QueryClient({ logger: mockLogger, ...config })
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
15
|
-
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function mockNavigatorOnLine(value: boolean) {
|
|
19
|
-
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const mockLogger = {
|
|
23
|
-
log: vi.fn(),
|
|
24
|
-
warn: vi.fn(),
|
|
25
|
-
error: vi.fn(),
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let queryKeyCount = 0
|
|
29
|
-
export function queryKey(): Array<string> {
|
|
30
|
-
queryKeyCount++
|
|
31
|
-
return [`query_${queryKeyCount}`]
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function sleep(timeout: number): Promise<void> {
|
|
35
|
-
return new Promise((resolve, _reject) => {
|
|
36
|
-
setTimeout(resolve, timeout)
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export async function simplefetcher() {
|
|
41
|
-
await sleep(10)
|
|
42
|
-
return 'test'
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function setActTimeout(fn: () => void, ms?: number) {
|
|
46
|
-
return setTimeout(() => {
|
|
47
|
-
act(() => {
|
|
48
|
-
fn()
|
|
49
|
-
})
|
|
50
|
-
}, ms)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Assert the parameter is of a specific type.
|
|
55
|
-
*/
|
|
56
|
-
export function expectType<T>(_: T): void {
|
|
57
|
-
return undefined
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Assert the parameter is not typed as `any`
|
|
62
|
-
*/
|
|
63
|
-
export function expectTypeNotAny<T>(_: 0 extends 1 & T ? never : T): void {
|
|
64
|
-
return undefined
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function executeMutation(
|
|
68
|
-
queryClient: QueryClient,
|
|
69
|
-
options: MutationOptions<any, any, any, any>,
|
|
70
|
-
): Promise<unknown> {
|
|
71
|
-
return queryClient.getMutationCache().build(queryClient, options).execute()
|
|
72
|
-
}
|