Repository: tanstack/query
File: packages/query-core/src/__tests__/queryClient.test.tsx (TypeScript)

Code before:
      queryClient.setQueryData(['key', 1], 1)
      queryClient.setQueryData(['key', 2], 2)
      const query1 = queryCache.find({ queryKey: ['key', 1] })!

      const result = queryClient.setQueriesData<number>(
        { predicate: (query) => query === query1 },
        (old) => old! + 5,
      )

      expect(result).toEqual([[['key', 1], 6]])
      expect(queryClient.getQueryData(['key', 1])).toBe(6)
      expect(queryClient.getQueryData(['key', 2])).toBe(2)
    })

    test('should not update non existing queries', () => {
      const result = queryClient.setQueriesData<string>(
        { queryKey: ['key'] },
        'data',
      )

      expect(result).toEqual([])
      expect(queryClient.getQueryData(['key'])).toBe(undefined)
    })
  })

  describe('isFetching', () => {
    test('should return length of fetching queries', async () => {
      const key1 = queryKey()
      const key2 = queryKey()

      expect(queryClient.isFetching()).toBe(0)
      queryClient.prefetchQuery({
        queryKey: key1,
        queryFn: () => sleep(10).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(1)
      queryClient.prefetchQuery({
        queryKey: key2,
        queryFn: () => sleep(5).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(2)
      await waitFor(() => expect(queryClient.isFetching()).toEqual(1))
      await waitFor(() => expect(queryClient.isFetching()).toEqual(0))
    })
  })

  describe('getQueryData', () => {
    test('should return the query data if the query is found', () => {
      const key = queryKey()
      queryClient.setQueryData([key, 'id'], 'bar')
      expect(queryClient.getQueryData([key, 'id'])).toBe('bar')

Review comment (suggestion):
```suggestion
        queryFn: () => sleep(30).then(() => 'data'),
```

Code after:
        { predicate: (query) => query === query1 },
        (old) => old! + 5,
      )

      expect(result).toEqual([[['key', 1], 6]])
      expect(queryClient.getQueryData(['key', 1])).toBe(6)
      expect(queryClient.getQueryData(['key', 2])).toBe(2)
    })

    test('should not update non existing queries', () => {
      const result = queryClient.setQueriesData<string>(
        { queryKey: ['key'] },
        'data',
      )

      expect(result).toEqual([])
      expect(queryClient.getQueryData(['key'])).toBe(undefined)
    })
  })

  describe('isFetching', () => {
    test('should return length of fetching queries', async () => {
      expect(queryClient.isFetching()).toBe(0)
      queryClient.prefetchQuery({
        queryKey: queryKey(),
        queryFn: () => sleep(10).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(1)
      queryClient.prefetchQuery({
        queryKey: queryKey(),
        queryFn: () => sleep(5).then(() => 'data'),
      })
      expect(queryClient.isFetching()).toBe(2)
      await vi.advanceTimersByTimeAsync(5)
      expect(queryClient.isFetching()).toEqual(1)
      await vi.advanceTimersByTimeAsync(5)
      expect(queryClient.isFetching()).toEqual(0)
    })
  })

  describe('getQueryData', () => {
    test('should return the query data if the query is found', () => {
      const key = queryKey()
      queryClient.setQueryData([key, 'id'], 'bar')
      expect(queryClient.getQueryData([key, 'id'])).toBe('bar')
    })

    test('should return undefined if the query is not found', () => {
      const key = queryKey()
      expect(queryClient.getQueryData(key)).toBeUndefined()
    })