Skip to content

Commit

Permalink
Revert "fix(tests): avoid using fake timers for now (pmndrs#1676)"
Browse files Browse the repository at this point in the history
This reverts commit e774947.
  • Loading branch information
manakuro committed Apr 9, 2023
1 parent 031bc0b commit f3924fd
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions tests/react/vanilla-utils/atomWithObservable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
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'
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 },
Expand Down Expand Up @@ -127,7 +132,7 @@ it('writable count state without initial value', async () => {
it('writable count state with delayed value', async () => {
const subject = new Subject<number>()
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
})
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -560,11 +565,11 @@ describe('error handling', () => {
const base = get(baseAtom)
if (base % 2 === 0) {
const subject = new Subject<number>()
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
})

Expand Down Expand Up @@ -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')
})

Expand All @@ -635,7 +640,7 @@ describe('error handling', () => {
} else {
subject.next({ data: count })
}
}, 10 * 1000 && FAKE_TIMEOUT / 2)
}, 10 * 1000)
return subject
})
return observableAtom
Expand Down Expand Up @@ -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')
})
})
Expand Down

0 comments on commit f3924fd

Please sign in to comment.