@tanstack/react-query 5.0.0-alpha.84 → 5.0.0-alpha.86

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.
@@ -3,14 +3,15 @@ import * as React from 'react';
3
3
  import { render } from '@testing-library/react';
4
4
  import { QueryClient } from '..';
5
5
  import type { QueryClientConfig } from '..';
6
+ import type { SpyInstance } from 'vitest';
6
7
  export declare function renderWithClient(client: QueryClient, ui: React.ReactElement): ReturnType<typeof render>;
7
8
  export declare const Blink: ({ duration, children, }: {
8
9
  duration: number;
9
10
  children: React.ReactNode;
10
11
  }) => JSX.Element;
11
12
  export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
12
- export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
13
- export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
13
+ export declare function mockVisibilityState(value: DocumentVisibilityState): SpyInstance<[], DocumentVisibilityState>;
14
+ export declare function mockOnlineManagerIsOnline(value: boolean): SpyInstance<[], boolean>;
14
15
  export declare function queryKey(): Array<string>;
15
16
  export declare function sleep(timeout: number): Promise<void>;
16
17
  export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/__tests__/utils.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAO,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAGpD,OAAO,EAAE,WAAW,EAAuB,MAAM,IAAI,CAAA;AACrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAA;AAE3C,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,WAAW,EACnB,EAAE,EAAE,KAAK,CAAC,YAAY,GACrB,UAAU,CAAC,OAAO,MAAM,CAAC,CAW3B;AAED,eA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I,IAAI,CAAC,CAAA;AAEtC;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,IAAI,CAExE;AAID,wBAAgB,WAAW,CAAC,QAAQ,EAAE,OAAO,cAW5C;AAED,eAAO,MAAM,YAAY,UAAW,MAAM,IAAI,YAAS,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-query",
3
- "version": "5.0.0-alpha.84",
3
+ "version": "5.0.0-alpha.86",
4
4
  "description": "Hooks for managing, caching and syncing asynchronous and remote data in React",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "client-only": "0.0.1",
37
- "@tanstack/query-core": "5.0.0-alpha.84"
37
+ "@tanstack/query-core": "5.0.0-alpha.86"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/react": "^18.2.4",
@@ -7,7 +7,7 @@ import { vi } from 'vitest'
7
7
  import { MutationCache, QueryCache, useMutation } from '..'
8
8
  import {
9
9
  createQueryClient,
10
- mockNavigatorOnLine,
10
+ mockOnlineManagerIsOnline,
11
11
  queryKey,
12
12
  renderWithClient,
13
13
  setActTimeout,
@@ -427,7 +427,7 @@ describe('useMutation', () => {
427
427
  })
428
428
 
429
429
  it('should not retry mutations while offline', async () => {
430
- const onlineMock = mockNavigatorOnLine(false)
430
+ const onlineMock = mockOnlineManagerIsOnline(false)
431
431
 
432
432
  let count = 0
433
433
 
@@ -455,6 +455,8 @@ describe('useMutation', () => {
455
455
 
456
456
  const rendered = renderWithClient(queryClient, <Page />)
457
457
 
458
+ window.dispatchEvent(new Event('offline'))
459
+
458
460
  await waitFor(() => {
459
461
  expect(
460
462
  rendered.getByText('error: null, status: idle, isPaused: false'),
@@ -471,7 +473,7 @@ describe('useMutation', () => {
471
473
 
472
474
  expect(count).toBe(0)
473
475
 
474
- onlineMock.mockReturnValue(true)
476
+ onlineMock.mockRestore()
475
477
  window.dispatchEvent(new Event('online'))
476
478
 
477
479
  await sleep(100)
@@ -483,12 +485,10 @@ describe('useMutation', () => {
483
485
  })
484
486
 
485
487
  expect(count).toBe(2)
486
-
487
- onlineMock.mockRestore()
488
488
  })
489
489
 
490
490
  it('should call onMutate even if paused', async () => {
491
- const onlineMock = mockNavigatorOnLine(false)
491
+ const onlineMock = mockOnlineManagerIsOnline(false)
492
492
  const onMutate = vi.fn()
493
493
  let count = 0
494
494
 
@@ -517,6 +517,8 @@ describe('useMutation', () => {
517
517
 
518
518
  await rendered.findByText('data: null, status: idle, isPaused: false')
519
519
 
520
+ window.dispatchEvent(new Event('offline'))
521
+
520
522
  fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
521
523
 
522
524
  await rendered.findByText('data: null, status: pending, isPaused: true')
@@ -524,19 +526,17 @@ describe('useMutation', () => {
524
526
  expect(onMutate).toHaveBeenCalledTimes(1)
525
527
  expect(onMutate).toHaveBeenCalledWith('todo')
526
528
 
527
- onlineMock.mockReturnValue(true)
529
+ onlineMock.mockRestore()
528
530
  window.dispatchEvent(new Event('online'))
529
531
 
530
532
  await rendered.findByText('data: 1, status: success, isPaused: false')
531
533
 
532
534
  expect(onMutate).toHaveBeenCalledTimes(1)
533
535
  expect(count).toBe(1)
534
-
535
- onlineMock.mockRestore()
536
536
  })
537
537
 
538
538
  it('should optimistically go to paused state if offline', async () => {
539
- const onlineMock = mockNavigatorOnLine(false)
539
+ const onlineMock = mockOnlineManagerIsOnline(false)
540
540
  let count = 0
541
541
  const states: Array<string> = []
542
542
 
@@ -566,6 +566,8 @@ describe('useMutation', () => {
566
566
 
567
567
  await rendered.findByText('data: null, status: idle, isPaused: false')
568
568
 
569
+ window.dispatchEvent(new Event('offline'))
570
+
569
571
  fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
570
572
 
571
573
  await rendered.findByText('data: null, status: pending, isPaused: true')
@@ -574,16 +576,14 @@ describe('useMutation', () => {
574
576
  expect(states[0]).toBe('idle, false')
575
577
  expect(states[1]).toBe('pending, true')
576
578
 
577
- onlineMock.mockReturnValue(true)
579
+ onlineMock.mockRestore()
578
580
  window.dispatchEvent(new Event('online'))
579
581
 
580
582
  await rendered.findByText('data: 1, status: success, isPaused: false')
581
-
582
- onlineMock.mockRestore()
583
583
  })
584
584
 
585
585
  it('should be able to retry a mutation when online', async () => {
586
- const onlineMock = mockNavigatorOnLine(false)
586
+ const onlineMock = mockOnlineManagerIsOnline(false)
587
587
 
588
588
  let count = 0
589
589
  const states: UseMutationResult<any, any, any, any>[] = []
@@ -608,6 +608,7 @@ describe('useMutation', () => {
608
608
 
609
609
  React.useEffect(() => {
610
610
  setActTimeout(() => {
611
+ window.dispatchEvent(new Event('offline'))
611
612
  mutate('todo')
612
613
  }, 10)
613
614
  }, [mutate])
@@ -645,7 +646,7 @@ describe('useMutation', () => {
645
646
  failureReason: new Error('oops'),
646
647
  })
647
648
 
648
- onlineMock.mockReturnValue(true)
649
+ onlineMock.mockRestore()
649
650
  window.dispatchEvent(new Event('online'))
650
651
 
651
652
  await sleep(50)
@@ -664,8 +665,6 @@ describe('useMutation', () => {
664
665
  failureReason: null,
665
666
  data: 'data',
666
667
  })
667
-
668
- onlineMock.mockRestore()
669
668
  })
670
669
 
671
670
  it('should not change state if unmounted', async () => {
@@ -8,7 +8,7 @@ import {
8
8
  Blink,
9
9
  createQueryClient,
10
10
  expectType,
11
- mockNavigatorOnLine,
11
+ mockOnlineManagerIsOnline,
12
12
  mockVisibilityState,
13
13
  queryKey,
14
14
  renderWithClient,
@@ -18,7 +18,6 @@ import {
18
18
  import type {
19
19
  DefinedUseQueryResult,
20
20
  QueryFunction,
21
- QueryFunctionContext,
22
21
  UseQueryOptions,
23
22
  UseQueryResult,
24
23
  } from '..'
@@ -2437,33 +2436,6 @@ describe('useQuery', () => {
2437
2436
  rendered.getByText('status: pending')
2438
2437
  })
2439
2438
 
2440
- // See https://github.com/tannerlinsley/react-query/issues/147
2441
- it('should not pass stringified variables to query function', async () => {
2442
- const key = queryKey()
2443
- const variables = { number: 5, boolean: false, object: {}, array: [] }
2444
- type CustomQueryKey = [typeof key, typeof variables]
2445
- const states: UseQueryResult<CustomQueryKey>[] = []
2446
-
2447
- const queryFn = async (ctx: QueryFunctionContext<CustomQueryKey>) => {
2448
- await sleep(10)
2449
- return ctx.queryKey
2450
- }
2451
-
2452
- function Page() {
2453
- const state = useQuery({ queryKey: [key, variables], queryFn })
2454
- states.push(state)
2455
- return <div>{state.status}</div>
2456
- }
2457
-
2458
- const rendered = renderWithClient(queryClient, <Page />)
2459
-
2460
- await waitFor(() => {
2461
- rendered.getByText('success')
2462
- })
2463
-
2464
- expect(states[1]?.data).toEqual([key, variables])
2465
- })
2466
-
2467
2439
  it('should not refetch query on focus when `enabled` is set to `false`', async () => {
2468
2440
  const key = queryKey()
2469
2441
  const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
@@ -2603,24 +2575,24 @@ describe('useQuery', () => {
2603
2575
  refetchOnWindowFocus: 'always',
2604
2576
  })
2605
2577
  states.push(state)
2606
- return null
2578
+ return (
2579
+ <div>
2580
+ <div>
2581
+ data: {state.data}, isFetching: {String(state.isFetching)}
2582
+ </div>
2583
+ </div>
2584
+ )
2607
2585
  }
2608
2586
 
2609
- renderWithClient(queryClient, <Page />)
2587
+ const rendered = renderWithClient(queryClient, <Page />)
2610
2588
 
2611
- await sleep(20)
2589
+ await waitFor(() => rendered.getByText('data: 0, isFetching: false'))
2612
2590
 
2613
2591
  act(() => {
2614
2592
  window.dispatchEvent(new Event('visibilitychange'))
2615
2593
  })
2616
2594
 
2617
- await sleep(20)
2618
-
2619
- await waitFor(() => expect(states.length).toBe(4))
2620
- expect(states[0]).toMatchObject({ data: undefined, isFetching: true })
2621
- expect(states[1]).toMatchObject({ data: 0, isFetching: false })
2622
- expect(states[2]).toMatchObject({ data: 0, isFetching: true })
2623
- expect(states[3]).toMatchObject({ data: 1, isFetching: false })
2595
+ await waitFor(() => rendered.getByText('data: 1, isFetching: false'))
2624
2596
  })
2625
2597
 
2626
2598
  it('should calculate focus behaviour for `refetchOnWindowFocus` depending on function', async () => {
@@ -5081,7 +5053,7 @@ describe('useQuery', () => {
5081
5053
 
5082
5054
  describe('networkMode online', () => {
5083
5055
  it('online queries should not start fetching if you are offline', async () => {
5084
- const onlineMock = mockNavigatorOnLine(false)
5056
+ const onlineMock = mockOnlineManagerIsOnline(false)
5085
5057
 
5086
5058
  const key = queryKey()
5087
5059
  const states: Array<any> = []
@@ -5110,10 +5082,11 @@ describe('useQuery', () => {
5110
5082
  }
5111
5083
 
5112
5084
  const rendered = renderWithClient(queryClient, <Page />)
5085
+ window.dispatchEvent(new Event('offline'))
5113
5086
 
5114
5087
  await waitFor(() => rendered.getByText('status: pending, isPaused: true'))
5115
5088
 
5116
- onlineMock.mockReturnValue(true)
5089
+ onlineMock.mockRestore()
5117
5090
  window.dispatchEvent(new Event('online'))
5118
5091
 
5119
5092
  await waitFor(() =>
@@ -5124,8 +5097,6 @@ describe('useQuery', () => {
5124
5097
  })
5125
5098
 
5126
5099
  expect(states).toEqual(['paused', 'fetching', 'idle'])
5127
-
5128
- onlineMock.mockRestore()
5129
5100
  })
5130
5101
 
5131
5102
  it('online queries should not refetch if you are offline', async () => {
@@ -5163,7 +5134,9 @@ describe('useQuery', () => {
5163
5134
 
5164
5135
  await waitFor(() => rendered.getByText('data: data1'))
5165
5136
 
5166
- const onlineMock = mockNavigatorOnLine(false)
5137
+ const onlineMock = mockOnlineManagerIsOnline(false)
5138
+ window.dispatchEvent(new Event('offline'))
5139
+
5167
5140
  fireEvent.click(rendered.getByRole('button', { name: /invalidate/i }))
5168
5141
 
5169
5142
  await waitFor(() =>
@@ -5173,7 +5146,7 @@ describe('useQuery', () => {
5173
5146
  )
5174
5147
  await waitFor(() => rendered.getByText('failureReason: null'))
5175
5148
 
5176
- onlineMock.mockReturnValue(true)
5149
+ onlineMock.mockRestore()
5177
5150
  window.dispatchEvent(new Event('online'))
5178
5151
 
5179
5152
  await waitFor(() =>
@@ -5192,8 +5165,6 @@ describe('useQuery', () => {
5192
5165
  await waitFor(() => {
5193
5166
  expect(rendered.getByText('data: data2')).toBeInTheDocument()
5194
5167
  })
5195
-
5196
- onlineMock.mockRestore()
5197
5168
  })
5198
5169
 
5199
5170
  it('online queries should not refetch if you are offline and refocus', async () => {
@@ -5229,7 +5200,7 @@ describe('useQuery', () => {
5229
5200
 
5230
5201
  await waitFor(() => rendered.getByText('data: data1'))
5231
5202
 
5232
- const onlineMock = mockNavigatorOnLine(false)
5203
+ const onlineMock = mockOnlineManagerIsOnline(false)
5233
5204
  fireEvent.click(rendered.getByRole('button', { name: /invalidate/i }))
5234
5205
 
5235
5206
  await waitFor(() =>
@@ -5275,7 +5246,7 @@ describe('useQuery', () => {
5275
5246
  )
5276
5247
  }
5277
5248
 
5278
- const onlineMock = mockNavigatorOnLine(false)
5249
+ const onlineMock = mockOnlineManagerIsOnline(false)
5279
5250
 
5280
5251
  const rendered = renderWithClient(queryClient, <Page />)
5281
5252
 
@@ -5326,7 +5297,7 @@ describe('useQuery', () => {
5326
5297
  )
5327
5298
  }
5328
5299
 
5329
- const onlineMock = mockNavigatorOnLine(false)
5300
+ const onlineMock = mockOnlineManagerIsOnline(false)
5330
5301
 
5331
5302
  const rendered = renderWithClient(queryClient, <Page />)
5332
5303
 
@@ -5380,9 +5351,10 @@ describe('useQuery', () => {
5380
5351
  )
5381
5352
  }
5382
5353
 
5383
- const onlineMock = mockNavigatorOnLine(false)
5354
+ const onlineMock = mockOnlineManagerIsOnline(false)
5384
5355
 
5385
5356
  const rendered = renderWithClient(queryClient, <Page />)
5357
+ window.dispatchEvent(new Event('offline'))
5386
5358
 
5387
5359
  await waitFor(() =>
5388
5360
  rendered.getByText('status: success, fetchStatus: paused'),
@@ -5405,7 +5377,7 @@ describe('useQuery', () => {
5405
5377
  window.dispatchEvent(new Event('visibilitychange'))
5406
5378
  })
5407
5379
 
5408
- onlineMock.mockReturnValue(true)
5380
+ onlineMock.mockRestore()
5409
5381
  act(() => {
5410
5382
  window.dispatchEvent(new Event('online'))
5411
5383
  })
@@ -5418,7 +5390,6 @@ describe('useQuery', () => {
5418
5390
  })
5419
5391
 
5420
5392
  expect(count).toBe(1)
5421
- onlineMock.mockRestore()
5422
5393
  })
5423
5394
 
5424
5395
  it('online queries should pause retries if you are offline', async () => {
@@ -5457,7 +5428,8 @@ describe('useQuery', () => {
5457
5428
  )
5458
5429
  await waitFor(() => rendered.getByText('failureReason: failed1'))
5459
5430
 
5460
- const onlineMock = mockNavigatorOnLine(false)
5431
+ const onlineMock = mockOnlineManagerIsOnline(false)
5432
+ window.dispatchEvent(new Event('offline'))
5461
5433
 
5462
5434
  await sleep(20)
5463
5435
 
@@ -5518,17 +5490,19 @@ describe('useQuery', () => {
5518
5490
  )
5519
5491
  }
5520
5492
 
5521
- const onlineMock = mockNavigatorOnLine(false)
5493
+ const onlineMock = mockOnlineManagerIsOnline(false)
5522
5494
 
5523
5495
  const rendered = renderWithClient(queryClient, <Page />)
5524
5496
 
5497
+ window.dispatchEvent(new Event('offline'))
5498
+
5525
5499
  await waitFor(() =>
5526
5500
  rendered.getByText('status: pending, fetchStatus: paused'),
5527
5501
  )
5528
5502
 
5529
5503
  fireEvent.click(rendered.getByRole('button', { name: /hide/i }))
5530
5504
 
5531
- onlineMock.mockReturnValue(true)
5505
+ onlineMock.mockRestore()
5532
5506
  window.dispatchEvent(new Event('online'))
5533
5507
 
5534
5508
  await waitFor(() => {
@@ -5541,8 +5515,6 @@ describe('useQuery', () => {
5541
5515
  // give it a bit more time to make sure queryFn is not called again
5542
5516
  await sleep(15)
5543
5517
  expect(count).toBe(1)
5544
-
5545
- onlineMock.mockRestore()
5546
5518
  })
5547
5519
 
5548
5520
  it('online queries should not fetch if paused and we go online when cancelled and no refetchOnReconnect', async () => {
@@ -5575,7 +5547,7 @@ describe('useQuery', () => {
5575
5547
  )
5576
5548
  }
5577
5549
 
5578
- const onlineMock = mockNavigatorOnLine(false)
5550
+ const onlineMock = mockOnlineManagerIsOnline(false)
5579
5551
 
5580
5552
  const rendered = renderWithClient(queryClient, <Page />)
5581
5553
 
@@ -5651,7 +5623,7 @@ describe('useQuery', () => {
5651
5623
  rendered.getByText('status: success, fetchStatus: idle'),
5652
5624
  )
5653
5625
 
5654
- const onlineMock = mockNavigatorOnLine(false)
5626
+ const onlineMock = mockOnlineManagerIsOnline(false)
5655
5627
 
5656
5628
  fireEvent.click(rendered.getByRole('button', { name: /invalidate/i }))
5657
5629
 
@@ -5681,7 +5653,7 @@ describe('useQuery', () => {
5681
5653
 
5682
5654
  describe('networkMode always', () => {
5683
5655
  it('always queries should start fetching even if you are offline', async () => {
5684
- const onlineMock = mockNavigatorOnLine(false)
5656
+ const onlineMock = mockOnlineManagerIsOnline(false)
5685
5657
 
5686
5658
  const key = queryKey()
5687
5659
  let count = 0
@@ -5721,7 +5693,7 @@ describe('useQuery', () => {
5721
5693
  })
5722
5694
 
5723
5695
  it('always queries should not pause retries', async () => {
5724
- const onlineMock = mockNavigatorOnLine(false)
5696
+ const onlineMock = mockOnlineManagerIsOnline(false)
5725
5697
 
5726
5698
  const key = queryKey()
5727
5699
  let count = 0
@@ -5767,7 +5739,7 @@ describe('useQuery', () => {
5767
5739
 
5768
5740
  describe('networkMode offlineFirst', () => {
5769
5741
  it('offlineFirst queries should start fetching if you are offline, but pause retries', async () => {
5770
- const onlineMock = mockNavigatorOnLine(false)
5742
+ const onlineMock = mockOnlineManagerIsOnline(false)
5771
5743
 
5772
5744
  const key = queryKey()
5773
5745
  let count = 0
@@ -5798,6 +5770,8 @@ describe('useQuery', () => {
5798
5770
 
5799
5771
  const rendered = renderWithClient(queryClient, <Page />)
5800
5772
 
5773
+ window.dispatchEvent(new Event('offline'))
5774
+
5801
5775
  await waitFor(() =>
5802
5776
  rendered.getByText(
5803
5777
  'status: pending, fetchStatus: paused, failureCount: 1',
@@ -5807,7 +5781,7 @@ describe('useQuery', () => {
5807
5781
 
5808
5782
  expect(count).toBe(1)
5809
5783
 
5810
- onlineMock.mockReturnValue(true)
5784
+ onlineMock.mockRestore()
5811
5785
  window.dispatchEvent(new Event('online'))
5812
5786
 
5813
5787
  await waitFor(() =>
@@ -5816,8 +5790,6 @@ describe('useQuery', () => {
5816
5790
  await waitFor(() => rendered.getByText('failureReason: failed3'))
5817
5791
 
5818
5792
  expect(count).toBe(3)
5819
-
5820
- onlineMock.mockRestore()
5821
5793
  })
5822
5794
  })
5823
5795
 
@@ -1,9 +1,11 @@
1
1
  import * as React from 'react'
2
2
  import { act, render } from '@testing-library/react'
3
+
3
4
  import * as utils from '@tanstack/query-core'
4
5
  import { vi } from 'vitest'
5
- import { QueryClient, QueryClientProvider } from '..'
6
+ import { QueryClient, QueryClientProvider, onlineManager } from '..'
6
7
  import type { QueryClientConfig } from '..'
8
+ import type { SpyInstance } from 'vitest'
7
9
 
8
10
  export function renderWithClient(
9
11
  client: QueryClient,
@@ -45,12 +47,16 @@ export function createQueryClient(config?: QueryClientConfig): QueryClient {
45
47
  return new QueryClient(config)
46
48
  }
47
49
 
48
- export function mockVisibilityState(value: DocumentVisibilityState) {
50
+ export function mockVisibilityState(
51
+ value: DocumentVisibilityState,
52
+ ): SpyInstance<[], DocumentVisibilityState> {
49
53
  return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
50
54
  }
51
55
 
52
- export function mockNavigatorOnLine(value: boolean) {
53
- return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
56
+ export function mockOnlineManagerIsOnline(
57
+ value: boolean,
58
+ ): SpyInstance<[], boolean> {
59
+ return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
54
60
  }
55
61
 
56
62
  let queryKeyCount = 0