fixt(import): update cancel functions to accept session UUID and modify related tests
This commit is contained in:
@@ -208,7 +208,7 @@ describe('useImport', () => {
|
||||
await result.current.cancel()
|
||||
})
|
||||
|
||||
expect(api.cancelImport).toHaveBeenCalled()
|
||||
expect(api.cancelImport).toHaveBeenCalledWith('session-3')
|
||||
await waitFor(() => {
|
||||
expect(result.current.session).toBeNull()
|
||||
})
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { renderHook, act, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import React from 'react'
|
||||
import { useJSONImport } from '../useJSONImport'
|
||||
import * as api from '../../api/jsonImport'
|
||||
|
||||
vi.mock('../../api/jsonImport', () => ({
|
||||
uploadJSONExport: vi.fn(),
|
||||
commitJSONImport: vi.fn(),
|
||||
cancelJSONImport: vi.fn(),
|
||||
}))
|
||||
|
||||
const createWrapper = () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
})
|
||||
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('useJSONImport', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('passes active session UUID to cancelJSONImport', async () => {
|
||||
const sessionId = 'json-session-123'
|
||||
vi.mocked(api.uploadJSONExport).mockResolvedValue({
|
||||
session: {
|
||||
id: sessionId,
|
||||
state: 'reviewing',
|
||||
source: 'json',
|
||||
},
|
||||
preview: {
|
||||
hosts: [],
|
||||
conflicts: [],
|
||||
errors: [],
|
||||
},
|
||||
conflict_details: {},
|
||||
})
|
||||
vi.mocked(api.cancelJSONImport).mockResolvedValue(undefined)
|
||||
|
||||
const { result } = renderHook(() => useJSONImport(), { wrapper: createWrapper() })
|
||||
|
||||
await act(async () => {
|
||||
await result.current.upload('{}')
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.sessionId).toBe(sessionId)
|
||||
})
|
||||
|
||||
await act(async () => {
|
||||
await result.current.cancel()
|
||||
})
|
||||
|
||||
expect(api.cancelJSONImport).toHaveBeenCalledWith(sessionId)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.sessionId).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { renderHook, act, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import React from 'react'
|
||||
import { useNPMImport } from '../useNPMImport'
|
||||
import * as api from '../../api/npmImport'
|
||||
|
||||
vi.mock('../../api/npmImport', () => ({
|
||||
uploadNPMExport: vi.fn(),
|
||||
commitNPMImport: vi.fn(),
|
||||
cancelNPMImport: vi.fn(),
|
||||
}))
|
||||
|
||||
const createWrapper = () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
})
|
||||
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('useNPMImport', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('passes active session UUID to cancelNPMImport', async () => {
|
||||
const sessionId = 'npm-session-123'
|
||||
vi.mocked(api.uploadNPMExport).mockResolvedValue({
|
||||
session: {
|
||||
id: sessionId,
|
||||
state: 'reviewing',
|
||||
source: 'npm',
|
||||
},
|
||||
preview: {
|
||||
hosts: [],
|
||||
conflicts: [],
|
||||
errors: [],
|
||||
},
|
||||
conflict_details: {},
|
||||
})
|
||||
vi.mocked(api.cancelNPMImport).mockResolvedValue(undefined)
|
||||
|
||||
const { result } = renderHook(() => useNPMImport(), { wrapper: createWrapper() })
|
||||
|
||||
await act(async () => {
|
||||
await result.current.upload('{}')
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.sessionId).toBe(sessionId)
|
||||
})
|
||||
|
||||
await act(async () => {
|
||||
await result.current.cancel()
|
||||
})
|
||||
|
||||
expect(api.cancelNPMImport).toHaveBeenCalledWith(sessionId)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.sessionId).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -77,7 +77,11 @@ export function useImport() {
|
||||
});
|
||||
|
||||
const cancelMutation = useMutation({
|
||||
mutationFn: () => cancelImport(),
|
||||
mutationFn: () => {
|
||||
const sessionId = uploadPreview?.session?.id || statusQuery.data?.session?.id;
|
||||
if (!sessionId) throw new Error('No active session');
|
||||
return cancelImport(sessionId);
|
||||
},
|
||||
onSuccess: () => {
|
||||
// Clear upload preview and remove query cache
|
||||
setUploadPreview(null);
|
||||
|
||||
@@ -46,7 +46,10 @@ export function useJSONImport() {
|
||||
});
|
||||
|
||||
const cancelMutation = useMutation({
|
||||
mutationFn: cancelJSONImport,
|
||||
mutationFn: () => {
|
||||
if (!sessionId) throw new Error('No active session');
|
||||
return cancelJSONImport(sessionId);
|
||||
},
|
||||
onSuccess: () => {
|
||||
setPreview(null);
|
||||
setSessionId(null);
|
||||
|
||||
@@ -46,7 +46,10 @@ export function useNPMImport() {
|
||||
});
|
||||
|
||||
const cancelMutation = useMutation({
|
||||
mutationFn: cancelNPMImport,
|
||||
mutationFn: () => {
|
||||
if (!sessionId) throw new Error('No active session');
|
||||
return cancelNPMImport(sessionId);
|
||||
},
|
||||
onSuccess: () => {
|
||||
setPreview(null);
|
||||
setSessionId(null);
|
||||
|
||||
Reference in New Issue
Block a user