diff --git a/tests/react/vanilla-utils/atomWithObservable.test.tsx b/tests/react/vanilla-utils/atomWithObservable.test.tsx index 326922244d..41ba04a452 100644 --- a/tests/react/vanilla-utils/atomWithObservable.test.tsx +++ b/tests/react/vanilla-utils/atomWithObservable.test.tsx @@ -1,6 +1,13 @@ import { Component, StrictMode, Suspense, useState } from 'react' import type { ReactElement, ReactNode } from 'react' -import { describe, expect, it } from '@jest/globals' +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from '@jest/globals' import { act, fireEvent, render, waitFor } from '@testing-library/react' import { BehaviorSubject, Observable, Subject, delay, of } from 'rxjs' import { fromValue, makeSubject, pipe, toObservable } from 'wonka' @@ -8,15 +15,13 @@ import { useAtom, useAtomValue, useSetAtom } from 'jotai/react' import { atom, createStore } from 'jotai/vanilla' import { atomWithObservable } from 'jotai/vanilla/utils' -// Avoid using fake timers for now: https://github.com/pmndrs/jotai/issues/1498 -const FAKE_TIMEOUT = 400 -// beforeEach(() => { -// jest.useFakeTimers() -// }) -// afterEach(() => { -// jest.runAllTimers() -// jest.useRealTimers() -// }) +beforeEach(() => { + jest.useFakeTimers() +}) +afterEach(() => { + jest.runAllTimers() + jest.useRealTimers() +}) class ErrorBoundary extends Component< { children: ReactNode }, @@ -127,7 +132,7 @@ it('writable count state without initial value', async () => { it('writable count state with delayed value', async () => { const subject = new Subject() const observableAtom = atomWithObservable(() => { - const observable = of(1).pipe(delay(10 * 1000 && FAKE_TIMEOUT / 2)) + const observable = of(1).pipe(delay(10 * 1000)) observable.subscribe((n) => subject.next(n)) return subject }) @@ -156,7 +161,7 @@ it('writable count state with delayed value', async () => { ) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('count: 1') fireEvent.click(getByText('button')) @@ -560,11 +565,11 @@ describe('error handling', () => { const base = get(baseAtom) if (base % 2 === 0) { const subject = new Subject() - const observable = of(1).pipe(delay(10 * 1000 && FAKE_TIMEOUT / 2)) + const observable = of(1).pipe(delay(10 * 1000)) observable.subscribe(() => subject.error(new Error('Test Error'))) return subject } - const observable = of(base).pipe(delay(10 * 1000 && FAKE_TIMEOUT / 2)) + const observable = of(base).pipe(delay(10 * 1000)) return observable }) @@ -600,22 +605,22 @@ describe('error handling', () => { ) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('errored') fireEvent.click(getByText('retry')) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('count: 1') fireEvent.click(getByText('next')) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('errored') fireEvent.click(getByText('retry')) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('count: 3') }) @@ -635,7 +640,7 @@ describe('error handling', () => { } else { subject.next({ data: count }) } - }, 10 * 1000 && FAKE_TIMEOUT / 2) + }, 10 * 1000) return subject }) return observableAtom @@ -689,22 +694,22 @@ describe('error handling', () => { ) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('errored') fireEvent.click(getByText('retry')) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('count: 1') fireEvent.click(getByText('refresh')) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('errored') fireEvent.click(getByText('retry')) await findByText('loading') - await new Promise((r) => setTimeout(r, FAKE_TIMEOUT)) // jest.runOnlyPendingTimers() + jest.runOnlyPendingTimers() await findByText('count: 3') }) })