Skip to content

Commit af6f63a

Browse files
authored
test(svelte-query/useMutationState): add test for 'select' option in 'useMutationState' (#10150)
1 parent d27b5be commit af6f63a

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<script lang="ts">
2+
import { QueryClient } from '@tanstack/query-core'
3+
import {
4+
createMutation,
5+
setQueryClientContext,
6+
useMutationState,
7+
} from '../../src/index.js'
8+
import type {
9+
Accessor,
10+
CreateMutationOptions,
11+
MutationStateOptions,
12+
} from '../../src/index.js'
13+
14+
let {
15+
mutationOpts,
16+
mutationStateOpts,
17+
}: {
18+
mutationOpts: Accessor<CreateMutationOptions>
19+
mutationStateOpts: MutationStateOptions<any>
20+
} = $props()
21+
22+
const queryClient = new QueryClient()
23+
setQueryClientContext(queryClient)
24+
25+
const mutation = createMutation(mutationOpts)
26+
27+
const variables = useMutationState(mutationStateOpts)
28+
</script>
29+
30+
<button onclick={() => mutation.mutate()}>mutate</button>
31+
32+
<div>
33+
Variables: {JSON.stringify(variables)}
34+
</div>

packages/svelte-query/tests/useMutationState/useMutationState.svelte.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
22
import { fireEvent, render } from '@testing-library/svelte'
33
import { sleep } from '@tanstack/query-test-utils'
44
import BaseExample from './BaseExample.svelte'
5+
import SelectExample from './SelectExample.svelte'
6+
import type { Mutation } from '@tanstack/query-core'
57

68
describe('useMutationState', () => {
79
beforeEach(() => {
@@ -79,6 +81,32 @@ describe('useMutationState', () => {
7981
expect(rendered.getByText('Data: ["error"]')).toBeInTheDocument()
8082
})
8183

84+
test('should return selected value when using select option', async () => {
85+
const mutationKey = ['select']
86+
87+
const rendered = render(SelectExample, {
88+
props: {
89+
mutationOpts: () => ({
90+
mutationKey,
91+
mutationFn: () => sleep(10).then(() => 'data'),
92+
}),
93+
mutationStateOpts: {
94+
filters: { mutationKey },
95+
select: (mutation: Mutation) => mutation.state.status,
96+
},
97+
},
98+
})
99+
100+
expect(rendered.getByText('Variables: []')).toBeInTheDocument()
101+
102+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
103+
await vi.advanceTimersByTimeAsync(0)
104+
expect(rendered.getByText('Variables: ["pending"]')).toBeInTheDocument()
105+
106+
await vi.advanceTimersByTimeAsync(10)
107+
expect(rendered.getByText('Variables: ["success"]')).toBeInTheDocument()
108+
})
109+
82110
test('Can select specific mutation using mutation key', async () => {
83111
const successMutationFn = vi.fn(() => sleep(10).then(() => 'data'))
84112
const errorMutationFn = vi

0 commit comments

Comments
 (0)