Skip to content

Commit

Permalink
fix(vue-query): simplify vue hooks, fix types (#4853)
Browse files Browse the repository at this point in the history
* refactor: drop unnecessary type assertions

* fix: eslint errors about signal

* fix: simplify vue hooks, fix types

* test: remove unnecessary conditional
  • Loading branch information
DamianOsipiuk committed Jan 22, 2023
1 parent 95ae966 commit 5b252e6
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 499 deletions.
39 changes: 18 additions & 21 deletions packages/react-query/src/__tests__/useInfiniteQuery.test.tsx
Expand Up @@ -846,14 +846,12 @@ describe('useInfiniteQuery', () => {
Promise<number>,
[QueryFunctionContext<typeof key, number>]
>(async ({ pageParam = start, signal }) => {
if (signal) {
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)
}
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)
await sleep(50)
return Number(pageParam)
})
Expand Down Expand Up @@ -891,7 +889,7 @@ describe('useInfiniteQuery', () => {
expect(firstCtx.pageParam).toBeUndefined()
expect(firstCtx.queryKey).toEqual(key)
expect(firstCtx.signal).toBeInstanceOf(AbortSignal)
expect(firstCtx.signal?.aborted).toBe(false)
expect(firstCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()

Expand All @@ -900,7 +898,7 @@ describe('useInfiniteQuery', () => {
expect(secondCtx.pageParam).toBe(11)
expect(secondCtx.queryKey).toEqual(key)
expect(secondCtx.signal).toBeInstanceOf(AbortSignal)
expect(secondCtx.signal?.aborted).toBe(true)
expect(secondCtx.signal.aborted).toBe(true)
expect(onAborts[callIndex]).toHaveBeenCalledTimes(1)
expect(abortListeners[callIndex]).toHaveBeenCalledTimes(1)

Expand All @@ -909,7 +907,7 @@ describe('useInfiniteQuery', () => {
expect(thirdCtx.pageParam).toBe(11)
expect(thirdCtx.queryKey).toEqual(key)
expect(thirdCtx.signal).toBeInstanceOf(AbortSignal)
expect(thirdCtx.signal?.aborted).toBe(false)
expect(thirdCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()
})
Expand All @@ -923,14 +921,13 @@ describe('useInfiniteQuery', () => {
Promise<number>,
[QueryFunctionContext<typeof key, number>]
>(async ({ pageParam = start, signal }) => {
if (signal) {
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)
}
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)

await sleep(50)
return Number(pageParam)
})
Expand Down Expand Up @@ -968,7 +965,7 @@ describe('useInfiniteQuery', () => {
expect(firstCtx.pageParam).toBeUndefined()
expect(firstCtx.queryKey).toEqual(key)
expect(firstCtx.signal).toBeInstanceOf(AbortSignal)
expect(firstCtx.signal?.aborted).toBe(false)
expect(firstCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()

Expand All @@ -977,7 +974,7 @@ describe('useInfiniteQuery', () => {
expect(secondCtx.pageParam).toBe(11)
expect(secondCtx.queryKey).toEqual(key)
expect(secondCtx.signal).toBeInstanceOf(AbortSignal)
expect(secondCtx.signal?.aborted).toBe(false)
expect(secondCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()
})
Expand Down
5 changes: 3 additions & 2 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Expand Up @@ -4664,6 +4664,7 @@ describe('useQuery', () => {
ctx,
) => {
const [, limit] = ctx.queryKey
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const value = limit % 2 && ctx.signal ? 'abort' : `data ${limit}`
await sleep(25)
return value
Expand Down Expand Up @@ -5719,10 +5720,10 @@ describe('useQuery', () => {
function Component() {
const state = useQuery({
queryKey: key,
queryFn: async ({ signal }) => {
queryFn: async ({ signal: _signal }) => {
count++
await sleep(10)
return `${signal ? 'signal' : 'data'}${count}`
return `signal${count}`
},
})

Expand Down
40 changes: 19 additions & 21 deletions packages/solid-query/src/__tests__/createInfiniteQuery.test.tsx
Expand Up @@ -910,14 +910,13 @@ describe('useInfiniteQuery', () => {
Promise<number>,
[QueryFunctionContext<typeof key, number>]
>(async ({ pageParam = start, signal }) => {
if (signal) {
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)
}
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)

await sleep(50)
return Number(pageParam)
})
Expand Down Expand Up @@ -960,7 +959,7 @@ describe('useInfiniteQuery', () => {
expect(firstCtx.pageParam).toBeUndefined()
expect(firstCtx.queryKey).toEqual(key)
expect(firstCtx.signal).toBeInstanceOf(AbortSignal)
expect(firstCtx.signal?.aborted).toBe(false)
expect(firstCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()

Expand All @@ -969,7 +968,7 @@ describe('useInfiniteQuery', () => {
expect(secondCtx.pageParam).toBe(11)
expect(secondCtx.queryKey).toEqual(key)
expect(secondCtx.signal).toBeInstanceOf(AbortSignal)
expect(secondCtx.signal?.aborted).toBe(true)
expect(secondCtx.signal.aborted).toBe(true)
expect(onAborts[callIndex]).toHaveBeenCalledTimes(1)
expect(abortListeners[callIndex]).toHaveBeenCalledTimes(1)

Expand All @@ -978,7 +977,7 @@ describe('useInfiniteQuery', () => {
expect(thirdCtx.pageParam).toBe(11)
expect(thirdCtx.queryKey).toEqual(key)
expect(thirdCtx.signal).toBeInstanceOf(AbortSignal)
expect(thirdCtx.signal?.aborted).toBe(false)
expect(thirdCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()
})
Expand All @@ -992,14 +991,13 @@ describe('useInfiniteQuery', () => {
Promise<number>,
[QueryFunctionContext<typeof key, number>]
>(async ({ pageParam = start, signal }) => {
if (signal) {
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)
}
const onAbort = jest.fn()
const abortListener = jest.fn()
onAborts.push(onAbort)
abortListeners.push(abortListener)
signal.onabort = onAbort
signal.addEventListener('abort', abortListener)

await sleep(50)
return Number(pageParam)
})
Expand Down Expand Up @@ -1042,7 +1040,7 @@ describe('useInfiniteQuery', () => {
expect(firstCtx.pageParam).toBeUndefined()
expect(firstCtx.queryKey).toEqual(key)
expect(firstCtx.signal).toBeInstanceOf(AbortSignal)
expect(firstCtx.signal?.aborted).toBe(false)
expect(firstCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()

Expand All @@ -1051,7 +1049,7 @@ describe('useInfiniteQuery', () => {
expect(secondCtx.pageParam).toBe(11)
expect(secondCtx.queryKey).toEqual(key)
expect(secondCtx.signal).toBeInstanceOf(AbortSignal)
expect(secondCtx.signal?.aborted).toBe(false)
expect(secondCtx.signal.aborted).toBe(false)
expect(onAborts[callIndex]).not.toHaveBeenCalled()
expect(abortListeners[callIndex]).not.toHaveBeenCalled()
})
Expand Down
2 changes: 1 addition & 1 deletion packages/solid-query/src/__tests__/createQueries.test.tsx
Expand Up @@ -803,7 +803,7 @@ describe('useQueries', () => {
}))
return (
<div>
<h1>Status: {state[0]?.data}</h1>
<h1>Status: {state[0].data}</h1>
</div>
)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/solid-query/src/__tests__/createQuery.test.tsx
Expand Up @@ -4650,6 +4650,7 @@ describe('createQuery', () => {
readonly [typeof key, number]
> = async (ctx) => {
const [, limit] = ctx.queryKey
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const value = limit % 2 && ctx.signal ? 'abort' : `data ${limit}`
await sleep(25)
return value
Expand Down Expand Up @@ -5789,10 +5790,10 @@ describe('createQuery', () => {
function Component() {
const state = createQuery(() => ({
queryKey: key,
queryFn: async ({ signal }) => {
queryFn: async ({ signal: _signal }) => {
count++
await sleep(10)
return `${signal ? 'signal' : 'data'}${count}`
return `signal${count}`
},
}))

Expand Down
@@ -1,7 +1,8 @@
import { InfiniteData } from '@tanstack/query-core'
import type { InfiniteData } from '@tanstack/query-core'
import { reactive } from 'vue'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { doNotExecute, Equal, Expect, simpleFetcher } from './test-utils'
import type { Equal, Expect } from './test-utils'
import { doNotExecute, simpleFetcher } from './test-utils'

describe('Discriminated union return type', () => {
it('data should be possibly undefined by default', () => {
Expand Down
25 changes: 2 additions & 23 deletions packages/vue-query/src/__tests__/useIsFetching.test.ts
@@ -1,8 +1,8 @@
import { onScopeDispose, reactive, ref } from 'vue-demi'
import { onScopeDispose, reactive } from 'vue-demi'

import { flushPromises, simpleFetcher } from './test-utils'
import { useQuery } from '../useQuery'
import { unrefFilterArgs, useIsFetching } from '../useIsFetching'
import { useIsFetching } from '../useIsFetching'

jest.mock('../useQueryClient')

Expand Down Expand Up @@ -74,25 +74,4 @@ describe('useIsFetching', () => {

await flushPromises(100)
})

describe('parseFilterArgs', () => {
test('should merge query key with filters', () => {
const filters = { queryKey: ['key'], stale: true }

const result = unrefFilterArgs(filters)
const expected = { ...filters, queryKey: ['key'] }

expect(result).toEqual(expected)
})

test('should unwrap refs arguments', () => {
const key = ref(['key'])
const filters = ref({ queryKey: key, stale: ref(true) })

const result = unrefFilterArgs(filters)
const expected = { queryKey: ['key'], stale: true }

expect(result).toEqual(expected)
})
})
})
25 changes: 2 additions & 23 deletions packages/vue-query/src/__tests__/useIsMutating.test.ts
@@ -1,8 +1,8 @@
import { onScopeDispose, reactive, ref } from 'vue-demi'
import { onScopeDispose, reactive } from 'vue-demi'

import { flushPromises, successMutator } from './test-utils'
import { useMutation } from '../useMutation'
import { unrefFilterArgs, useIsMutating } from '../useIsMutating'
import { useIsMutating } from '../useIsMutating'

jest.mock('../useQueryClient')

Expand Down Expand Up @@ -78,25 +78,4 @@ describe('useIsMutating', () => {

expect(isMutating.value).toStrictEqual(1)
})

describe('parseMutationFilterArgs', () => {
test('should merge mutation key with filters', () => {
const filters = { mutationKey: ['key'], fetching: true }

const result = unrefFilterArgs(filters)
const expected = { ...filters, mutationKey: ['key'] }

expect(result).toEqual(expected)
})

test('should unwrap refs arguments', () => {
const key = ref(['key'])
const filters = ref({ mutationKey: key, fetching: ref(true) })

const result = unrefFilterArgs(filters)
const expected = { mutationKey: ['key'], fetching: true }

expect(result).toEqual(expected)
})
})
})
58 changes: 1 addition & 57 deletions packages/vue-query/src/__tests__/useMutation.test.ts
@@ -1,6 +1,6 @@
import { reactive, ref } from 'vue-demi'
import { errorMutator, flushPromises, successMutator } from './test-utils'
import { unrefMutationArgs, useMutation } from '../useMutation'
import { useMutation } from '../useMutation'
import { useQueryClient } from '../useQueryClient'

jest.mock('../useQueryClient')
Expand Down Expand Up @@ -331,60 +331,4 @@ describe('useMutation', () => {
})
})
})

describe('parseMutationArgs', () => {
test('should return the same instance of options', () => {
const options = { retry: false }
const result = unrefMutationArgs(options)

expect(result).toEqual(options)
})

test('should merge query key with options', () => {
const options = { mutationKey: ['key'], retry: false }
const result = unrefMutationArgs(options)
const expected = { ...options, mutationKey: ['key'] }

expect(result).toEqual(expected)
})

test('should merge query fn with options', () => {
const options = { mutationFn: successMutator, retry: false }
const result = unrefMutationArgs(options)
const expected = { ...options, mutationFn: successMutator }

expect(result).toEqual(expected)
})

test('should merge query key and fn with options', () => {
const options = {
mutationKey: ['key'],
mutationFn: successMutator,
retry: false,
}
const result = unrefMutationArgs(options)
const expected = {
...options,
mutationKey: ['key'],
mutationFn: successMutator,
}

expect(result).toEqual(expected)
})

test('should unwrap refs arguments', () => {
const key = ref(['key'])
const mutationFn = ref(successMutator)
const options = ref({ mutationKey: key, mutationFn, retry: ref(12) })

const result = unrefMutationArgs(options)
const expected = {
mutationKey: ['key'],
mutationFn: successMutator,
retry: 12,
}

expect(result).toEqual(expected)
})
})
})
3 changes: 2 additions & 1 deletion packages/vue-query/src/__tests__/useMutation.types.test.tsx
@@ -1,6 +1,7 @@
import { reactive } from 'vue'
import { useMutation } from '../useMutation'
import { doNotExecute, Equal, Expect, successMutator } from './test-utils'
import { doNotExecute, successMutator } from './test-utils'
import type { Equal, Expect } from './test-utils'

describe('Discriminated union return type', () => {
it('data should be possibly undefined by default', () => {
Expand Down

0 comments on commit 5b252e6

Please sign in to comment.