Refactor: Improve test structure and mock implementations across multiple test files
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 7m21s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 7m21s
This commit is contained in:
@@ -32,13 +32,22 @@ describe('useWatchedItems Hook', () => {
|
||||
beforeEach(() => {
|
||||
// Reset all mocks before each test to ensure isolation
|
||||
vi.clearAllMocks();
|
||||
// Default mock for useApi to handle any number of calls/re-renders safely
|
||||
mockedUseApi.mockReturnValue({
|
||||
execute: vi.fn(),
|
||||
error: null,
|
||||
data: null,
|
||||
loading: false,
|
||||
isRefetching: false,
|
||||
reset: vi.fn()
|
||||
});
|
||||
|
||||
// Provide a default implementation for useApi
|
||||
// Specific overrides for the first render sequence:
|
||||
// 1st call = addWatchedItemApi, 2nd call = removeWatchedItemApi
|
||||
mockedUseApi
|
||||
.mockReturnValueOnce({ execute: mockAddWatchedItemApi, error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() })
|
||||
.mockReturnValueOnce({ execute: mockRemoveWatchedItemApi, error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() });
|
||||
|
||||
|
||||
// Provide a default implementation for the mocked hooks
|
||||
mockedUseAuth.mockReturnValue({
|
||||
user: mockUser,
|
||||
@@ -91,15 +100,23 @@ describe('useWatchedItems Hook', () => {
|
||||
});
|
||||
|
||||
it('should set an error message if the API call fails', async () => {
|
||||
// Re-mock useApi for this specific error test case
|
||||
mockedUseApi.mockReturnValueOnce({
|
||||
execute: mockAddWatchedItemApi,
|
||||
error: new Error('API Error'),
|
||||
data: null,
|
||||
loading: false,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
// Clear existing mocks to set a specific sequence for this test
|
||||
mockedUseApi.mockReset();
|
||||
|
||||
// Default fallback
|
||||
mockedUseApi.mockReturnValue({ execute: vi.fn(), error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() });
|
||||
|
||||
// Mock the first call (add) to return an error immediately
|
||||
mockedUseApi
|
||||
.mockReturnValueOnce({
|
||||
execute: mockAddWatchedItemApi,
|
||||
error: new Error('API Error'),
|
||||
data: null,
|
||||
loading: false,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
})
|
||||
.mockReturnValueOnce({ execute: mockRemoveWatchedItemApi, error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() });
|
||||
|
||||
const { result } = renderHook(() => useWatchedItems());
|
||||
|
||||
@@ -136,9 +153,16 @@ describe('useWatchedItems Hook', () => {
|
||||
});
|
||||
|
||||
it('should set an error message if the API call fails', async () => {
|
||||
// Re-mock useApi for this specific error test case
|
||||
mockedUseApi.mockReturnValueOnce({ execute: vi.fn(), error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() }) // for add
|
||||
.mockReturnValueOnce({ execute: vi.fn(), error: new Error('Deletion Failed'), data: null, loading: false, isRefetching: false, reset: vi.fn() }); // for remove
|
||||
// Clear existing mocks
|
||||
mockedUseApi.mockReset();
|
||||
|
||||
// Default fallback
|
||||
mockedUseApi.mockReturnValue({ execute: vi.fn(), error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() });
|
||||
|
||||
// Mock sequence: 1st (add) success, 2nd (remove) error
|
||||
mockedUseApi
|
||||
.mockReturnValueOnce({ execute: vi.fn(), error: null, data: null, loading: false, isRefetching: false, reset: vi.fn() })
|
||||
.mockReturnValueOnce({ execute: vi.fn(), error: new Error('Deletion Failed'), data: null, loading: false, isRefetching: false, reset: vi.fn() });
|
||||
|
||||
const { result } = renderHook(() => useWatchedItems());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user