@tanstack/svelte-query 5.0.0-alpha.26 → 5.0.0-alpha.27

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.
@@ -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,16 @@
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, index}
11
+ {#if query.isPending}
12
+ <p>Loading {index + 1}</p>
13
+ {:else if query.isSuccess}
14
+ <p>{query.data}</p>
15
+ {/if}
16
+ {/each}
@@ -0,0 +1,19 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ import type { QueriesOptions } from '../createQueries';
3
+ declare const __propDef: {
4
+ props: {
5
+ options: {
6
+ queries: [...QueriesOptions<any>];
7
+ };
8
+ };
9
+ events: {
10
+ [evt: string]: CustomEvent<any>;
11
+ };
12
+ slots: {};
13
+ };
14
+ export declare type CreateQueriesProps = typeof __propDef.props;
15
+ export declare type CreateQueriesEvents = typeof __propDef.events;
16
+ export declare type CreateQueriesSlots = typeof __propDef.slots;
17
+ export default class CreateQueries extends SvelteComponentTyped<CreateQueriesProps, CreateQueriesEvents, CreateQueriesSlots> {
18
+ }
19
+ export {};
@@ -0,0 +1,22 @@
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.isPending}
11
+ <p>Loading</p>
12
+ {:else if $query.isError}
13
+ <p>Error</p>
14
+ {:else if $query.isSuccess}
15
+ <p>Success</p>
16
+ {/if}
17
+
18
+ <ul>
19
+ {#each $query.data ?? [] as entry}
20
+ <li>id: {entry.id}</li>
21
+ {/each}
22
+ </ul>
@@ -0,0 +1,17 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ import type { CreateQueryOptions, WritableOrVal } from '../types';
3
+ declare const __propDef: {
4
+ props: {
5
+ options: WritableOrVal<CreateQueryOptions<any>>;
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,38 @@
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
+ queries: [
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
+ await waitFor(() => {
30
+ expect(rendered.getByText('Loading 1')).toBeInTheDocument();
31
+ expect(rendered.getByText('Loading 2')).toBeInTheDocument();
32
+ });
33
+ await waitFor(() => {
34
+ expect(rendered.getByText('Success 1')).toBeInTheDocument();
35
+ expect(rendered.getByText('Success 2')).toBeInTheDocument();
36
+ });
37
+ });
38
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,57 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { render, waitFor } from '@testing-library/svelte';
3
+ import { writable } from 'svelte/store';
4
+ import CreateQuery from './CreateQuery.svelte';
5
+ import { sleep } from './utils';
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
+ await waitFor(() => {
20
+ expect(rendered.getByText('Loading')).toBeInTheDocument();
21
+ });
22
+ await waitFor(() => {
23
+ expect(rendered.getByText('Success')).toBeInTheDocument();
24
+ });
25
+ });
26
+ it('should keep previous data when returned as placeholder data', async () => {
27
+ const options = writable({
28
+ queryKey: ['test', [1]],
29
+ queryFn: async ({ queryKey }) => {
30
+ await sleep(10);
31
+ const ids = queryKey[1];
32
+ if (!ids || !Array.isArray(ids))
33
+ return [];
34
+ return ids.map((id) => ({ id }));
35
+ },
36
+ placeholderData: (previousData) => previousData,
37
+ });
38
+ const rendered = render(CreateQuery, { props: { options } });
39
+ await waitFor(() => {
40
+ expect(rendered.queryByText('id: 1')).not.toBeInTheDocument();
41
+ expect(rendered.queryByText('id: 2')).not.toBeInTheDocument();
42
+ });
43
+ await waitFor(() => {
44
+ expect(rendered.queryByText('id: 1')).toBeInTheDocument();
45
+ expect(rendered.queryByText('id: 2')).not.toBeInTheDocument();
46
+ });
47
+ options.update((o) => ({ ...o, queryKey: ['test', [1, 2]] }));
48
+ await waitFor(() => {
49
+ expect(rendered.queryByText('id: 1')).toBeInTheDocument();
50
+ expect(rendered.queryByText('id: 2')).not.toBeInTheDocument();
51
+ });
52
+ await waitFor(() => {
53
+ expect(rendered.queryByText('id: 1')).toBeInTheDocument();
54
+ expect(rendered.queryByText('id: 2')).toBeInTheDocument();
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { QueryClient, type QueryClientConfig } 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 function queryKey(): Array<string>;
7
+ export declare function sleep(timeout: number): Promise<void>;
8
+ export declare function simplefetcher(): Promise<string>;
9
+ export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
@@ -0,0 +1,33 @@
1
+ import { vi } from 'vitest';
2
+ import { act } from '@testing-library/svelte';
3
+ import { QueryClient } from '../index';
4
+ export function createQueryClient(config) {
5
+ return new QueryClient(config);
6
+ }
7
+ export function mockVisibilityState(value) {
8
+ return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value);
9
+ }
10
+ export function mockNavigatorOnLine(value) {
11
+ return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value);
12
+ }
13
+ let queryKeyCount = 0;
14
+ export function queryKey() {
15
+ queryKeyCount++;
16
+ return [`query_${queryKeyCount}`];
17
+ }
18
+ export function sleep(timeout) {
19
+ return new Promise((resolve, _reject) => {
20
+ setTimeout(resolve, timeout);
21
+ });
22
+ }
23
+ export async function simplefetcher() {
24
+ await sleep(10);
25
+ return 'test';
26
+ }
27
+ export function setActTimeout(fn, ms) {
28
+ return setTimeout(() => {
29
+ act(() => {
30
+ fn();
31
+ });
32
+ }, ms);
33
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-query",
3
- "version": "5.0.0-alpha.26",
3
+ "version": "5.0.0-alpha.27",
4
4
  "description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
5
- "author": "Dre Johnson",
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
- "type": "module",
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",
@@ -31,21 +47,12 @@
31
47
  "peerDependencies": {
32
48
  "svelte": "^3.54.0"
33
49
  },
34
- "exports": {
35
- "./package.json": "./package.json",
36
- ".": "./build/lib/index.js"
37
- },
38
- "svelte": "./build/lib/index.js",
39
- "files": [
40
- "build/lib/*",
41
- "src"
42
- ],
43
50
  "scripts": {
44
51
  "clean": "rimraf ./build",
45
52
  "test:types": "svelte-check --tsconfig ./tsconfig.json",
46
53
  "test:eslint": "eslint --ext .svelte,.ts ./src",
47
54
  "test:lib": "vitest run --coverage",
48
55
  "test:lib:dev": "pnpm run test:lib --watch",
49
- "build": "svelte-package --input ./src --output ./build/lib && rimraf ./build/lib/__tests__"
56
+ "build": "svelte-package --input ./src --output ./build/lib"
50
57
  }
51
58
  }
@@ -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,20 +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: { queries: [...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, index}
15
- {#if query.isPending}
16
- <p>Loading {index + 1}</p>
17
- {:else if query.isSuccess}
18
- <p>{query.data}</p>
19
- {/if}
20
- {/each}
@@ -1,30 +0,0 @@
1
- <script lang="ts">
2
- import {
3
- createQuery,
4
- QueryClient,
5
- type CreateQueryOptions,
6
- type WritableOrVal,
7
- } from '../index'
8
- import { setQueryClientContext } from '../context'
9
-
10
- export let options: WritableOrVal<CreateQueryOptions<any>>
11
-
12
- const queryClient = new QueryClient()
13
- setQueryClientContext(queryClient)
14
-
15
- const query = createQuery(options)
16
- </script>
17
-
18
- {#if $query.isPending}
19
- <p>Loading</p>
20
- {:else if $query.isError}
21
- <p>Error</p>
22
- {:else if $query.isSuccess}
23
- <p>Success</p>
24
- {/if}
25
-
26
- <ul>
27
- {#each $query.data ?? [] as entry}
28
- <li>id: {entry.id}</li>
29
- {/each}
30
- </ul>
@@ -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,41 +0,0 @@
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
-
6
- describe('createQueries', () => {
7
- it('Render and wait for success', async () => {
8
- const rendered = render(CreateQueries, {
9
- props: {
10
- options: {
11
- queries: [
12
- {
13
- queryKey: ['key-1'],
14
- queryFn: async () => {
15
- await sleep(10)
16
- return 'Success 1'
17
- },
18
- },
19
- {
20
- queryKey: ['key-2'],
21
- queryFn: async () => {
22
- await sleep(10)
23
- return 'Success 2'
24
- },
25
- },
26
- ],
27
- },
28
- },
29
- })
30
-
31
- await waitFor(() => {
32
- expect(rendered.getByText('Loading 1')).toBeInTheDocument()
33
- expect(rendered.getByText('Loading 2')).toBeInTheDocument()
34
- })
35
-
36
- await waitFor(() => {
37
- expect(rendered.getByText('Success 1')).toBeInTheDocument()
38
- expect(rendered.getByText('Success 2')).toBeInTheDocument()
39
- })
40
- })
41
- })
@@ -1,64 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import { render, waitFor } from '@testing-library/svelte'
3
- import { writable } from 'svelte/store'
4
- import CreateQuery from './CreateQuery.svelte'
5
- import { sleep } from './utils'
6
- import type { CreateQueryOptions, WritableOrVal } from '../types'
7
-
8
- describe('createQuery', () => {
9
- it('Render and wait for success', async () => {
10
- const rendered = render(CreateQuery, {
11
- props: {
12
- options: {
13
- queryKey: ['test'],
14
- queryFn: async () => {
15
- await sleep(10)
16
- return 'Success'
17
- },
18
- },
19
- },
20
- })
21
-
22
- await waitFor(() => {
23
- expect(rendered.getByText('Loading')).toBeInTheDocument()
24
- })
25
-
26
- await waitFor(() => {
27
- expect(rendered.getByText('Success')).toBeInTheDocument()
28
- })
29
- })
30
-
31
- it('should keep previous data when returned as placeholder data', async () => {
32
- const options: WritableOrVal<CreateQueryOptions> = writable({
33
- queryKey: ['test', [1]],
34
- queryFn: async ({ queryKey }) => {
35
- await sleep(50)
36
- const ids = queryKey[1]
37
- if (!ids || !Array.isArray(ids)) return []
38
- return ids.map((id) => ({ id }))
39
- },
40
- placeholderData: (previousData: { id: number }[]) => previousData,
41
- })
42
- const rendered = render(CreateQuery, { props: { options } })
43
-
44
- expect(rendered.queryByText('id: 1')).not.toBeInTheDocument()
45
- expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
46
-
47
- await sleep(100)
48
-
49
- expect(rendered.queryByText('id: 1')).toBeInTheDocument()
50
- expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
51
-
52
- options.update((o) => ({ ...o, queryKey: ['test', [1, 2]] }))
53
-
54
- await sleep(0)
55
-
56
- expect(rendered.queryByText('id: 1')).toBeInTheDocument()
57
- expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
58
-
59
- await sleep(100)
60
-
61
- expect(rendered.queryByText('id: 1')).toBeInTheDocument()
62
- expect(rendered.queryByText('id: 2')).toBeInTheDocument()
63
- })
64
- })
@@ -1,40 +0,0 @@
1
- import { vi } from 'vitest'
2
- import { act } from '@testing-library/svelte'
3
- import { QueryClient, type QueryClientConfig } from '../index'
4
-
5
- export function createQueryClient(config?: QueryClientConfig): QueryClient {
6
- return new QueryClient(config)
7
- }
8
-
9
- export function mockVisibilityState(value: DocumentVisibilityState) {
10
- return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
11
- }
12
-
13
- export function mockNavigatorOnLine(value: boolean) {
14
- return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
15
- }
16
-
17
- let queryKeyCount = 0
18
- export function queryKey(): Array<string> {
19
- queryKeyCount++
20
- return [`query_${queryKeyCount}`]
21
- }
22
-
23
- export function sleep(timeout: number): Promise<void> {
24
- return new Promise((resolve, _reject) => {
25
- setTimeout(resolve, timeout)
26
- })
27
- }
28
-
29
- export async function simplefetcher() {
30
- await sleep(10)
31
- return 'test'
32
- }
33
-
34
- export function setActTimeout(fn: () => void, ms?: number) {
35
- return setTimeout(() => {
36
- act(() => {
37
- fn()
38
- })
39
- }, ms)
40
- }