Skip to content

Commit

Permalink
prepare test apparatus
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Aug 24, 2024
1 parent 7be4d0d commit fafa45c
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 126 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.swp
node_modules
/dist
/jotai
17 changes: 3 additions & 14 deletions __tests__/atomEffect.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from 'react'
import { act, render, renderHook, waitFor } from '@testing-library/react'
import { Provider, useAtom, useAtomValue, useSetAtom } from 'jotai/react'
import { type Atom, atom, createStore, getDefaultStore } from 'jotai/vanilla'
import { atom, createStore, getDefaultStore } from 'jotai/vanilla'
import { atomEffect } from '../src/atomEffect'
import {
ErrorBoundary,
Expand All @@ -11,8 +11,6 @@ import {
incrementLetter,
} from './test-utils'

type AnyAtom = Atom<any>

it('should run the effect on vanilla store', async () => {
const store = getDefaultStore()
const countAtom = atom(0)
Expand Down Expand Up @@ -325,12 +323,10 @@ it('should allow asynchronous recursion with microtask delay with set.recurse',
let runCount = 0
const watchedAtom = atom(0)
watchedAtom.debugLabel = 'watchedAtom' // remove
let done = false
const effectAtom = atomEffect((get, { recurse }) => {
const value = get(watchedAtom)
runCount++
if (value >= 3) {
done = true
return
}
Promise.resolve().then(() => {
Expand All @@ -339,8 +335,6 @@ it('should allow asynchronous recursion with microtask delay with set.recurse',
})
const store = getDefaultStore()
store.sub(effectAtom, () => void 0)
// await waitFor(() => assert(done))
done
await delay(500)
expect(store.get(watchedAtom)).toBe(3)
expect(runCount).toBe(4)
Expand Down Expand Up @@ -865,13 +859,8 @@ it('should not infinite loop with nested atomEffects', async () => {

await waitFor(() => assert(!!metrics.runCount1))

function dev_get_mounted_atoms() {
if ('dev_get_mounted_atoms' in store) {
return (store.dev_get_mounted_atoms as () => IterableIterator<AnyAtom>)()
}
return []
}
const atomSet = new Set(dev_get_mounted_atoms())
if (!('dev4_get_mounted_atoms' in store)) return
const atomSet = new Set(store.dev4_get_mounted_atoms())
expect({
countAtom: atomSet.has(countAtom),
effectAtom: atomSet.has(effectAtom),
Expand Down
31 changes: 31 additions & 0 deletions __tests__/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ export function assert(value: boolean, message?: string): asserts value {
}
}

export function waitFor(
condition: () => boolean,
options?: { interval?: number; timeout?: number }
): Promise<void>
export function waitFor(
condition: () => void,
options?: { interval?: number; timeout?: number }
): Promise<void>
export function waitFor(
condition: () => boolean | void,
{ interval = 10, timeout = 1000 } = {}
) {
return new Promise<void>((resolve, reject) => {
const intervalId = setInterval(() => {
try {
if (condition() !== false) {
clearInterval(intervalId)
clearTimeout(timeoutId)
resolve()
}
} catch {
// ignore
}
}, interval)
const timeoutId = setTimeout(() => {
clearInterval(intervalId)
reject(new Error('timeout'))
}, timeout)
})
}

type ErrorBoundaryState = {
hasError: boolean
}
Expand Down
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@
"preset": "ts-jest/presets/js-with-ts",
"testMatch": [
"**/__tests__/**/*.test.ts?(x)"
]
],
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"tsconfig": "tsconfig.test.json"
}
]
}
},
"keywords": [
"jotai",
Expand All @@ -54,8 +62,8 @@
"@types/node": "^20.8.3",
"@types/react": "^18.2.25",
"@types/react-dom": "^18.2.11",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"@typescript-eslint/eslint-plugin": "^8.2.0",
"@typescript-eslint/parser": "^8.2.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
Expand All @@ -67,7 +75,7 @@
"html-webpack-plugin": "^5.5.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jotai": "2.6.3",
"jotai": "2.9.3",
"microbundle": "^0.15.1",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
Expand All @@ -76,12 +84,12 @@
"react-error-boundary": "^4.0.11",
"ts-jest": "^29.1.1",
"ts-loader": "^9.4.4",
"typescript": "^5.2.2",
"typescript": "5.5.4",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
},
"peerDependencies": {
"jotai": ">=2.5.0 <2.9.0"
"jotai": ">=2.5.0"
}
}
Loading

0 comments on commit fafa45c

Please sign in to comment.