Skip to content

test(solid-query/useIsFetching): switch to fake timers, replace 'waitFor' with 'vi.waitFor' and 'findByText' with 'getByText' #9377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions packages/solid-query/src/__tests__/useIsFetching.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { fireEvent, render } from '@solidjs/testing-library'
import { Show, createEffect, createRenderEffect, createSignal } from 'solid-js'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
Expand All @@ -12,6 +12,14 @@ import {
import { setActTimeout } from './utils'

describe('useIsFetching', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

// See https://github.com/tannerlinsley/react-query/issues/105
it('should update as queries start and stop fetching', async () => {
const queryCache = new QueryCache()
Expand Down Expand Up @@ -53,14 +61,14 @@ describe('useIsFetching', () => {
</QueryClientProvider>
))

await waitFor(() =>
await vi.waitFor(() =>
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument(),
)
fireEvent.click(rendered.getByRole('button', { name: /setReady/i }))
await waitFor(() =>
await vi.waitFor(() =>
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument(),
)
await waitFor(() =>
await vi.waitFor(() =>
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument(),
)
})
Expand Down Expand Up @@ -130,7 +138,7 @@ describe('useIsFetching', () => {
</QueryClientProvider>
))
// unlike react, Updating renderSecond wont cause a rerender for FirstQuery
await waitFor(() => expect(isFetchingArray).toEqual([0, 1, 2, 1, 0]))
await vi.waitFor(() => expect(isFetchingArray).toEqual([0, 1, 2, 1, 0]))
})

it('should be able to filter', async () => {
Expand Down Expand Up @@ -192,10 +200,10 @@ describe('useIsFetching', () => {
</QueryClientProvider>
))

await rendered.findByText('isFetching: 0')
await vi.waitFor(() => rendered.getByText('isFetching: 0'))
fireEvent.click(rendered.getByRole('button', { name: /setStarted/i }))
await rendered.findByText('isFetching: 1')
await rendered.findByText('isFetching: 0')
await vi.waitFor(() => rendered.getByText('isFetching: 1'))
await vi.waitFor(() => rendered.getByText('isFetching: 0'))
// at no point should we have isFetching: 2
expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
})
Expand Down Expand Up @@ -228,10 +236,10 @@ describe('useIsFetching', () => {
</QueryClientProvider>
))

await waitFor(() =>
await vi.waitFor(() =>
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument(),
)
await waitFor(() =>
await vi.waitFor(() =>
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument(),
)
})
Expand Down Expand Up @@ -263,7 +271,7 @@ describe('useIsFetching', () => {

const rendered = render(() => <Page></Page>)

await waitFor(() =>
await vi.waitFor(() =>
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument(),
)
})
Expand Down
Loading