Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.6 KB
Raw Blame
import { QueryClient } from '@tanstack/react-query'
import { createStore } from 'jotai'
import { describe, expect, it, onTestFinished, vi } from 'vite-plus/test'
import { atomWithResolvedSuspenseQuery } from './query-atoms'

const createQueryClient = () => {
  const client = new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
      },
    },
  })
  onTestFinished(() => client.clear())
  return client
}

describe('atomWithResolvedSuspenseQuery', () => {
  it('should return the resolved suspense query result when query data is cached', () => {
    const queryClient = createQueryClient()
    const queryKey = ['resolved-query']
    queryClient.setQueryData(queryKey, 'cached data')

    const queryAtom = atomWithResolvedSuspenseQuery(
      () => ({
        queryKey,
        queryFn: async () => 'fetched data',
      }),
      () => queryClient,
    )
    const store = createStore()

    const result = store.get(queryAtom)

    expect(result.data).toBe('cached data')
    expect(result.isSuccess).toBe(true)
  })

  it('should throw when the suspense query is still pending', () => {
    const queryClient = createQueryClient()
    const queryKey = ['pending-query']
    const queryFn = vi.fn(() => new Promise<string>(() => {}))

    const queryAtom = atomWithResolvedSuspenseQuery(
      () => ({
        queryKey,
        queryFn,
      }),
      () => queryClient,
    )
    const store = createStore()

    expect(() => store.get(queryAtom)).toThrow(
      'Suspense query must be resolved before reading: ["pending-query"]',
    )
    expect(queryFn).toHaveBeenCalledTimes(1)
  })
})