integration test fixes - claude for the win?
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 1m0s
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 1m0s
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// src/hooks/mutations/useRemoveShoppingListItemMutation.test.tsx
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import type { ReactNode } from 'react';
|
||||
import { useRemoveShoppingListItemMutation } from './useRemoveShoppingListItemMutation';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
import * as notificationService from '../../services/notificationService';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
vi.mock('../../services/notificationService');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
const mockedNotifications = vi.mocked(notificationService);
|
||||
|
||||
describe('useRemoveShoppingListItemMutation', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove an item from shopping list successfully', async () => {
|
||||
const mockResponse = { success: true };
|
||||
mockedApiClient.removeShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useRemoveShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42 });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.removeShoppingListItem).toHaveBeenCalledWith(42);
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Item removed from shopping list');
|
||||
});
|
||||
|
||||
it('should invalidate shopping-lists query on success', async () => {
|
||||
mockedApiClient.removeShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ success: true }),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useRemoveShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 100 });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['shopping-lists'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.removeShoppingListItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
json: () => Promise.resolve({ message: 'Item not found' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useRemoveShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 999 });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Item not found');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Item not found');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.removeShoppingListItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useRemoveShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42 });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user