Skip to content

Commit

Permalink
update tests to remove makeMutableAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
David Maskasky committed Oct 6, 2023
1 parent 59ca83d commit 456fbef
Showing 1 changed file with 11 additions and 41 deletions.
52 changes: 11 additions & 41 deletions __tests__/mutableAtom.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import React from 'react'
import { act, render, renderHook, waitFor } from '@testing-library/react'
import {
Provider,
atom,
createStore,
useAtom,
useAtomValue,
useSetAtom,
} from 'jotai'
import { atom, getDefaultStore, useAtom, useAtomValue, useSetAtom } from 'jotai'
import assert from 'minimalistic-assert'
import { makeMutableAtom, mutableAtom } from '../src/mutableAtom'
import { mutableAtom } from '../src/mutableAtom'
import type { ProxyState } from '../src/mutableAtom/types'

it('should be defined on initial render', async () => {
Expand Down Expand Up @@ -292,43 +285,20 @@ it('should reject writing to properties other than `value`', async () => {

it('should allow updating even when component is unmounted', async () => {
expect.assertions(2)
const store = createStore()
const countAtom = atom({ value: 0 })
let isMounted = false
countAtom.onMount = () => {
isMounted = true
}
const mutableCountAtom = makeMutableAtom(countAtom)
const store = getDefaultStore()
const mutableCountAtom = mutableAtom(0)

await act(() => store.get(mutableCountAtom).value++)

function useTest() {
useAtomValue(mutableCountAtom)
}
function wrapper({ children }: { children: React.ReactNode }) {
return <Provider store={store}>{children}</Provider>
return useAtomValue(mutableCountAtom)
}

const { unmount } = renderHook(useTest, { wrapper })
await waitFor(() => assert(isMounted))
const { result, unmount } = renderHook(useTest)
expect(result.current.value).toBe(1)
unmount()
expect(store.get(countAtom).value).toBe(0)
await act(async () => {
const countProxy = store.get(mutableCountAtom)
countProxy.value++
})
expect(store.get(countAtom).value).toBe(1)
})

it('should allow updating even when component has not mounted', async () => {
expect.assertions(2)
const store = createStore()
const countAtom = atom({ value: 0 })
const mutableCountAtom = makeMutableAtom(countAtom)
expect(store.get(countAtom).value).toBe(0)
await act(async () => {
const countProxy = store.get(mutableCountAtom)
countProxy.value++
})
expect(store.get(countAtom).value).toBe(1)
await act(() => store.get(mutableCountAtom).value++)
expect(store.get(mutableCountAtom).value).toBe(2)
})

it('should correctly handle updates via writable atom', async () => {
Expand Down

0 comments on commit 456fbef

Please sign in to comment.