|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { QueryClient } from '@tanstack/query-core-v4' |
| 3 | +import { subscribeQueryCallbacks } from './index' |
| 4 | + |
| 5 | +vi.mock('@tanstack/query-core', () => import('@tanstack/query-core-v4')) |
| 6 | + |
| 7 | +describe('core (v4)', () => { |
| 8 | + let queryClient: QueryClient |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + queryClient = new QueryClient() |
| 12 | + queryClient.mount() |
| 13 | + }) |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + queryClient.clear() |
| 17 | + }) |
| 18 | + |
| 19 | + it('should call onSuccess & onSettled', async () => { |
| 20 | + const onSuccess = vi.fn() |
| 21 | + const onSettled = vi.fn() |
| 22 | + const QUERY_KEY = ['foo'] |
| 23 | + |
| 24 | + subscribeQueryCallbacks({ |
| 25 | + queryKey: QUERY_KEY, |
| 26 | + queryClient: queryClient as any, |
| 27 | + onSuccess, |
| 28 | + onSettled, |
| 29 | + }) |
| 30 | + |
| 31 | + await queryClient.fetchQuery({ |
| 32 | + queryKey: QUERY_KEY, |
| 33 | + queryFn: () => Promise.resolve('bar'), |
| 34 | + }) |
| 35 | + |
| 36 | + expect(onSuccess).toBeCalledTimes(1) |
| 37 | + expect(onSuccess).toBeCalledWith('bar') |
| 38 | + expect(onSettled).toBeCalledTimes(1) |
| 39 | + expect(onSettled).toBeCalledWith('bar', null) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should call onError, onSettled', async () => { |
| 43 | + const onError = vi.fn() |
| 44 | + const onSettled = vi.fn() |
| 45 | + const QUERY_KEY = ['foo'] |
| 46 | + |
| 47 | + subscribeQueryCallbacks({ |
| 48 | + queryKey: QUERY_KEY, |
| 49 | + queryClient: queryClient as any, |
| 50 | + onError, |
| 51 | + onSettled, |
| 52 | + }) |
| 53 | + |
| 54 | + await queryClient.fetchQuery({ |
| 55 | + queryKey: QUERY_KEY, |
| 56 | + // eslint-disable-next-line prefer-promise-reject-errors |
| 57 | + queryFn: () => Promise.reject('bar'), |
| 58 | + }).catch(() => {}) |
| 59 | + |
| 60 | + expect(onError).toBeCalledTimes(1) |
| 61 | + expect(onError).toBeCalledWith('bar') |
| 62 | + expect(onSettled).toBeCalledTimes(1) |
| 63 | + expect(onSettled).toBeCalledWith(undefined, 'bar') |
| 64 | + }) |
| 65 | + |
| 66 | + it('should not call anything when fetch other query', async () => { |
| 67 | + const onError = vi.fn() |
| 68 | + const onSettled = vi.fn() |
| 69 | + const onSuccess = vi.fn() |
| 70 | + const QUERY_KEY = ['foo'] |
| 71 | + const OTHER_QUERY_KEY = ['bar'] |
| 72 | + |
| 73 | + subscribeQueryCallbacks({ |
| 74 | + queryKey: QUERY_KEY, |
| 75 | + queryClient: queryClient as any, |
| 76 | + onSuccess, |
| 77 | + onError, |
| 78 | + onSettled, |
| 79 | + }) |
| 80 | + |
| 81 | + await queryClient.prefetchQuery({ |
| 82 | + queryKey: OTHER_QUERY_KEY, |
| 83 | + // eslint-disable-next-line prefer-promise-reject-errors |
| 84 | + queryFn: () => Promise.reject('bar'), |
| 85 | + }).catch(() => {}) |
| 86 | + |
| 87 | + expect(onSuccess).not.toBeCalled() |
| 88 | + expect(onError).not.toBeCalled() |
| 89 | + expect(onSettled).not.toBeCalled() |
| 90 | + }) |
| 91 | + |
| 92 | + it('should not call anything when unsubscribed', async () => { |
| 93 | + const onSuccess = vi.fn() |
| 94 | + const onError = vi.fn() |
| 95 | + const onSettled = vi.fn() |
| 96 | + const QUERY_KEY = ['foo'] |
| 97 | + |
| 98 | + const unsubscribe = subscribeQueryCallbacks({ |
| 99 | + queryKey: QUERY_KEY, |
| 100 | + queryClient: queryClient as any, |
| 101 | + onSuccess, |
| 102 | + onError, |
| 103 | + onSettled, |
| 104 | + }) |
| 105 | + |
| 106 | + unsubscribe() |
| 107 | + |
| 108 | + await queryClient.fetchQuery({ |
| 109 | + queryKey: QUERY_KEY, |
| 110 | + queryFn: () => Promise.resolve('bar'), |
| 111 | + }).catch(() => {}) |
| 112 | + |
| 113 | + expect(onSuccess).not.toBeCalled() |
| 114 | + expect(onError).not.toBeCalled() |
| 115 | + expect(onSettled).not.toBeCalled() |
| 116 | + }) |
| 117 | + |
| 118 | + it('should return empty func', async () => { |
| 119 | + const QUERY_KEY = ['foo'] |
| 120 | + |
| 121 | + const unsubscribe = subscribeQueryCallbacks({ |
| 122 | + queryKey: QUERY_KEY, |
| 123 | + queryClient: queryClient as any, |
| 124 | + }) |
| 125 | + |
| 126 | + expect(unsubscribe).toBeTypeOf('function') |
| 127 | + }) |
| 128 | +}) |
0 commit comments