Skip to content

Commit

Permalink
chore(tests): update references to tests to point to local jotai inst…
Browse files Browse the repository at this point in the history
…ance (#60)
  • Loading branch information
dmaskasky authored Sep 24, 2024
1 parent fd5c82c commit 66164b3
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 17 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,33 @@ describe('ScopeProvider', () => {
expect(container.querySelector(base)).toBe(null)
})
})

it('computed atom mounts once for the unscoped and once for the scoped', () => {
const baseAtom = atom(0)
const deriveAtom = atom(
(get) => get(baseAtom),
() => {},
)
const onUnmount = jest.fn()
const onMount = jest.fn(() => onUnmount)
deriveAtom.onMount = onMount
function Component() {
return useAtomValue(deriveAtom)
}
function App() {
return (
<>
<Component />
<Component />
<ScopeProvider atoms={[baseAtom]}>
<Component />
<Component />
</ScopeProvider>
</>
)
}
const { unmount } = render(<App />)
expect(onMount).toHaveBeenCalledTimes(2)
unmount()
expect(onUnmount).toHaveBeenCalledTimes(2)
})
File renamed without changes.
29 changes: 29 additions & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { fireEvent } from '@testing-library/react'
import { Store } from 'src/ScopeProvider/types'
import { INTERNAL_DevStoreRev4, INTERNAL_PrdStore } from 'jotai/vanilla/store'

function getElements(container: HTMLElement, querySelectors: string[]): Element[] {
return querySelectors.map((querySelector) => {
Expand All @@ -21,3 +23,30 @@ export function clickButton(container: HTMLElement, querySelector: string) {
}
fireEvent.click(button)
}

export function getDevStore(store: Store): INTERNAL_PrdStore & INTERNAL_DevStoreRev4 {
if (!isDevStore(store)) {
throw new Error('Store is not a dev store')
}
return store
}

export function isDevStore(store: Store): store is INTERNAL_PrdStore & INTERNAL_DevStoreRev4 {
return (
'dev4_get_internal_weak_map' in store &&
'dev4_get_mounted_atoms' in store &&
'dev4_restore_atoms' in store
)
}

export function assertIsDevStore(
store: Store,
): asserts store is INTERNAL_PrdStore & INTERNAL_DevStoreRev4 {
if (!isDevStore(store)) {
throw new Error('Store is not a dev store')
}
}

export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"jest": {
"testEnvironment": "jsdom",
"preset": "ts-jest/presets/js-with-ts",
"testPathIgnorePatterns": [
"utils.ts"
"testMatch": [
"**/__tests__/**/*.test.*"
]
},
"keywords": [
Expand All @@ -59,6 +59,7 @@
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.5.1",
"@types/jest": "^29.5.6",
"@types/minimalistic-assert": "^1.0.3",
"@types/node": "^20.8.7",
"@types/react": "^18.2.31",
"@types/react-dom": "^18.2.14",
Expand All @@ -75,8 +76,8 @@
"html-webpack-plugin": "^5.5.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jotai": "2.9.2",
"microbundle": "^0.15.1",
"minimalistic-assert": "^1.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"react": "^18.2.0",
Expand All @@ -90,7 +91,7 @@
"webpack-dev-server": "^4.15.1"
},
"peerDependencies": {
"jotai": ">=2.9.2",
"jotai": ">=2.9.3",
"react": ">=17.0.0"
}
}
31 changes: 18 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66164b3

Please sign in to comment.