diff --git a/__tests__/mutableAtom.test.tsx b/__tests__/mutableAtom.test.tsx index 171538e..fb84cff 100644 --- a/__tests__/mutableAtom.test.tsx +++ b/__tests__/mutableAtom.test.tsx @@ -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 () => { @@ -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 {children} + 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 () => {