Skip to content

Commit

Permalink
use v5, add atomWithMutation, atomWithSuspense with existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kalijonn committed Nov 30, 2023
1 parent 8d3fbde commit c1a064b
Show file tree
Hide file tree
Showing 41 changed files with 672 additions and 2,906 deletions.
14 changes: 6 additions & 8 deletions __tests__/01_basic_spec.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
atomsWithInfiniteQuery,
atomsWithMutation,
atomsWithQuery,
atomsWithQueryAsync,
atomWithInfiniteQuery,
atomWithMutation,
atomWithQuery,
queryClientAtom,
} from '../src/index'

describe('basic spec', () => {
it('should export functions', () => {
expect(queryClientAtom).toBeDefined()
expect(atomsWithQuery).toBeDefined()
expect(atomsWithInfiniteQuery).toBeDefined()
expect(atomsWithMutation).toBeDefined()
expect(atomsWithQueryAsync).toBeDefined()
expect(atomWithQuery).toBeDefined()
expect(atomWithInfiniteQuery).toBeDefined()
expect(atomWithMutation).toBeDefined()
})
})
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import React, { useState } from 'react'
import { fireEvent, render } from '@testing-library/react'
import { useAtom } from 'jotai/react'
import { atomsWithMutation } from '../src/index'
import { atomWithMutation } from '../src/index'

it('atomsWithMutation should be refreshed on unmount (#2060)', async () => {
it('atomWithMutation should be refreshed on unmount (#2060)', async () => {
let resolve: (() => void) | undefined
const [, testAtom] = atomsWithMutation<number, number, number, number>(
() => ({
mutationKey: ['test-atom'],
mutationFn: async (a) => {
await new Promise<void>((r) => {
resolve = r
})
return a
},
})
)
const mutateAtom = atomWithMutation<number, number>(() => ({
mutationKey: ['test-atom'],
mutationFn: async (a) => {
console.log('mutation triggered')
await new Promise<void>((r) => {
resolve = r
})
return a
},
}))

function App() {
const [mount, setMount] = useState<boolean>(true)
Expand All @@ -29,11 +28,11 @@ it('atomsWithMutation should be refreshed on unmount (#2060)', async () => {
}

function TestView() {
const [state, mutate] = useAtom(testAtom)
const [{ mutate, isPending, status }] = useAtom(mutateAtom)
return (
<div>
<p>status: {state.status}</p>
<button disabled={state.isLoading} onClick={() => mutate([1])}>
<p>status: {status}</p>
<button disabled={isPending} onClick={() => mutate(1)}>
mutate
</button>
</div>
Expand All @@ -45,12 +44,10 @@ it('atomsWithMutation should be refreshed on unmount (#2060)', async () => {
await findByText('status: idle')

fireEvent.click(getByText('mutate'))
await findByText('status: loading')
await findByText('status: pending')
resolve?.()
await findByText('status: success')

fireEvent.click(getByText('mutate'))
await findByText('status: loading')
fireEvent.click(getByText('unmount'))
fireEvent.click(getByText('mount'))
await findByText('status: idle')
Expand Down
54 changes: 26 additions & 28 deletions __tests__/atomWithQuery_spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ it('query basic test', async () => {
}))
const Counter = () => {
const [countData] = useAtom(countAtom)
const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -75,9 +75,9 @@ it('query refetch', async () => {
},
}))
const Counter = () => {
const [{ data, isLoading, isError, refetch }] = useAtom(countAtom)
const [{ data, isPending, isError, refetch }] = useAtom(countAtom)

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -129,9 +129,9 @@ it('query no-loading with keepPreviousData', async () => {
}))
const Counter = () => {
const [countData] = useAtom(countAtom)
const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -195,14 +195,14 @@ it('query with enabled', async () => {
const Slug = () => {
const [slugQueryData] = useAtom(slugQueryAtom)

const { data, isLoading, isError, status, fetchStatus } = slugQueryData
const { data, isPending, isError, status, fetchStatus } = slugQueryData

//ref: https://tanstack.com/query/v4/docs/react/guides/dependent-queries
if (status === 'loading' && fetchStatus === 'idle') {
if (status === 'pending' && fetchStatus === 'idle') {
return <div>not enabled</div>
}

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -267,13 +267,13 @@ it('query with enabled 2', async () => {

const Slug = () => {
const [slugQueryAtomData] = useAtom(slugQueryAtom)
const { data, isError, isLoading, status, fetchStatus } = slugQueryAtomData
const { data, isError, isPending, status, fetchStatus } = slugQueryAtomData

if (status === 'loading' && fetchStatus === 'idle') {
if (status === 'pending' && fetchStatus === 'idle') {
return <div>not enabled</div>
}

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -351,9 +351,9 @@ it('query with enabled (#500)', async () => {
const Counter = () => {
const [countData] = useAtom(countAtom)

const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -416,9 +416,9 @@ it('query with initialData test', async () => {
}))
const Counter = () => {
const [countData] = useAtom(countAtom)
const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -464,9 +464,9 @@ it('query dependency test', async () => {

const Counter = () => {
const [countData] = useAtom(countAtom)
const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -519,9 +519,9 @@ it('query expected QueryCache test', async () => {
const Counter = () => {
const [countData] = useAtom(countAtom)

const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand All @@ -538,9 +538,7 @@ it('query expected QueryCache test', async () => {

const { findByText } = render(
<StrictMode>
<Suspense fallback="loading">
<Counter />
</Suspense>
<Counter />
</StrictMode>
)

Expand Down Expand Up @@ -598,7 +596,7 @@ describe('error handling', () => {
const [countData] = useAtom(countAtom)

if ('data' in countData) {
if (countData.isLoading) {
if (countData.isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -642,9 +640,9 @@ describe('error handling', () => {
const Counter = () => {
const [countData] = useAtom(countAtom)

const { data, isLoading, refetch } = countData
const { data, isPending, refetch } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down Expand Up @@ -715,9 +713,9 @@ it('renews the result when the query changes and a non stale cache is available'
const Counter = () => {
const setCurrentCount = useSetAtom(currentCountAtom)
const [countData] = useAtom(countAtom)
const { data, isLoading, isError } = countData
const { data, isPending, isError } = countData

if (isLoading) {
if (isPending) {
return <>loading</>
}

Expand Down
Loading

0 comments on commit c1a064b

Please sign in to comment.