You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-metadata.spec.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { DataType } from '@/app/components/datasets/metadata/types'
  2. import { act, renderHook } from '@testing-library/react'
  3. import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
  4. import { useBatchUpdateDocMetadata } from '@/service/knowledge/use-metadata'
  5. import { useDocumentListKey } from './use-document'
  6. // Mock the post function to avoid real network requests
  7. jest.mock('@/service/base', () => ({
  8. post: jest.fn().mockResolvedValue({ success: true }),
  9. }))
  10. const NAME_SPACE = 'dataset-metadata'
  11. describe('useBatchUpdateDocMetadata', () => {
  12. let queryClient: QueryClient
  13. beforeEach(() => {
  14. // Create a fresh QueryClient before each test
  15. queryClient = new QueryClient()
  16. })
  17. // Wrapper for React Query context
  18. const wrapper = ({ children }: { children: React.ReactNode }) => (
  19. <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  20. )
  21. it('should correctly invalidate dataset and document caches', async () => {
  22. const { result } = renderHook(() => useBatchUpdateDocMetadata(), { wrapper })
  23. // Spy on queryClient.invalidateQueries
  24. const invalidateSpy = jest.spyOn(queryClient, 'invalidateQueries')
  25. // Correct payload type: each document has its own metadata_list array
  26. const payload = {
  27. dataset_id: 'dataset-1',
  28. metadata_list: [
  29. {
  30. document_id: 'doc-1',
  31. metadata_list: [
  32. { key: 'title-1', id: '01', name: 'name-1', type: DataType.string, value: 'new title 01' },
  33. ],
  34. },
  35. {
  36. document_id: 'doc-2',
  37. metadata_list: [
  38. { key: 'title-2', id: '02', name: 'name-1', type: DataType.string, value: 'new title 02' },
  39. ],
  40. },
  41. ],
  42. }
  43. // Execute the mutation
  44. await act(async () => {
  45. await result.current.mutateAsync(payload)
  46. })
  47. // Expect invalidateQueries to have been called exactly 5 times
  48. expect(invalidateSpy).toHaveBeenCalledTimes(5)
  49. // Dataset cache invalidation
  50. expect(invalidateSpy).toHaveBeenNthCalledWith(1, {
  51. queryKey: [NAME_SPACE, 'dataset', 'dataset-1'],
  52. })
  53. // Document list cache invalidation
  54. expect(invalidateSpy).toHaveBeenNthCalledWith(2, {
  55. queryKey: [NAME_SPACE, 'document', 'dataset-1'],
  56. })
  57. // useDocumentListKey cache invalidation
  58. expect(invalidateSpy).toHaveBeenNthCalledWith(3, {
  59. queryKey: [...useDocumentListKey, 'dataset-1'],
  60. })
  61. // Single document cache invalidation
  62. expect(invalidateSpy.mock.calls.slice(3)).toEqual(
  63. expect.arrayContaining([
  64. [{ queryKey: [NAME_SPACE, 'document', 'dataset-1', 'doc-1'] }],
  65. [{ queryKey: [NAME_SPACE, 'document', 'dataset-1', 'doc-2'] }],
  66. ]),
  67. )
  68. })
  69. })