Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff912b9055 | ||
| ec32027bd4 | |||
|
|
59f773639b | ||
| dd2be5eecf | |||
|
|
a94bfbd3e9 | ||
| 338bbc9440 | |||
|
|
60aad04642 | ||
| 7f2aff9a24 | |||
|
|
689320e7d2 | ||
| e457bbf046 | |||
| 68cdbb6066 |
@@ -45,7 +45,18 @@
|
||||
"Bash(powershell.exe -Command \"echo ''List all podman containers'' | claude --print\")",
|
||||
"Bash(powershell.exe -Command \"echo ''List my repositories on gitea.projectium.com using gitea-projectium MCP'' | claude --print\")",
|
||||
"Bash(powershell.exe -Command \"echo ''List my repositories on gitea.projectium.com using gitea-projectium MCP'' | claude --print --allowedTools ''mcp__gitea-projectium__*''\")",
|
||||
"Bash(powershell.exe -Command \"echo ''Fetch the homepage of https://gitea.projectium.com and summarize it'' | claude --print --allowedTools ''mcp__fetch__*''\")"
|
||||
"Bash(powershell.exe -Command \"echo ''Fetch the homepage of https://gitea.projectium.com and summarize it'' | claude --print --allowedTools ''mcp__fetch__*''\")",
|
||||
"Bash(dir \"C:\\\\Users\\\\games3\\\\.claude\")",
|
||||
"Bash(dir:*)",
|
||||
"Bash(D:nodejsnpx.cmd -y @modelcontextprotocol/server-fetch --help)",
|
||||
"Bash(cmd /c \"dir /o-d C:\\\\Users\\\\games3\\\\.claude\\\\debug 2>nul | head -10\")",
|
||||
"mcp__memory__read_graph",
|
||||
"mcp__memory__create_entities",
|
||||
"mcp__memory__search_nodes",
|
||||
"mcp__memory__delete_entities",
|
||||
"mcp__sequential-thinking__sequentialthinking",
|
||||
"mcp__filesystem__list_directory",
|
||||
"mcp__filesystem__read_multiple_files"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.9.62",
|
||||
"version": "0.9.67",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "flyer-crawler",
|
||||
"version": "0.9.62",
|
||||
"version": "0.9.67",
|
||||
"dependencies": {
|
||||
"@bull-board/api": "^6.14.2",
|
||||
"@bull-board/express": "^6.14.2",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "flyer-crawler",
|
||||
"private": true,
|
||||
"version": "0.9.62",
|
||||
"version": "0.9.67",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "concurrently \"npm:start:dev\" \"vite\"",
|
||||
|
||||
128
src/hooks/mutations/useAddShoppingListItemMutation.test.tsx
Normal file
128
src/hooks/mutations/useAddShoppingListItemMutation.test.tsx
Normal file
@@ -0,0 +1,128 @@
|
||||
// src/hooks/mutations/useAddShoppingListItemMutation.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 { useAddShoppingListItemMutation } from './useAddShoppingListItemMutation';
|
||||
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('useAddShoppingListItemMutation', () => {
|
||||
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 add a master item to shopping list successfully', async () => {
|
||||
const mockResponse = { shopping_list_item_id: 1, master_item_id: 42 };
|
||||
mockedApiClient.addShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 1, item: { masterItemId: 42 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.addShoppingListItem).toHaveBeenCalledWith(1, { masterItemId: 42 });
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Item added to shopping list');
|
||||
});
|
||||
|
||||
it('should add a custom item to shopping list successfully', async () => {
|
||||
const mockResponse = { shopping_list_item_id: 2, custom_item_name: 'Special Milk' };
|
||||
mockedApiClient.addShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 1, item: { customItemName: 'Special Milk' } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.addShoppingListItem).toHaveBeenCalledWith(1, { customItemName: 'Special Milk' });
|
||||
});
|
||||
|
||||
it('should invalidate shopping-lists query on success', async () => {
|
||||
mockedApiClient.addShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({}),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useAddShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 1, item: { masterItemId: 42 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['shopping-lists'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.addShoppingListItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 400,
|
||||
json: () => Promise.resolve({ message: 'Item already exists' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 1, item: { masterItemId: 42 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Item already exists');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Item already exists');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.addShoppingListItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 1, item: { masterItemId: 42 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should handle network error', async () => {
|
||||
mockedApiClient.addShoppingListItem.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
const { result } = renderHook(() => useAddShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 1, item: { masterItemId: 42 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Network error');
|
||||
});
|
||||
});
|
||||
115
src/hooks/mutations/useAddWatchedItemMutation.test.tsx
Normal file
115
src/hooks/mutations/useAddWatchedItemMutation.test.tsx
Normal file
@@ -0,0 +1,115 @@
|
||||
// src/hooks/mutations/useAddWatchedItemMutation.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 { useAddWatchedItemMutation } from './useAddWatchedItemMutation';
|
||||
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('useAddWatchedItemMutation', () => {
|
||||
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 add a watched item successfully with category', async () => {
|
||||
const mockResponse = { id: 1, item_name: 'Milk', category: 'Dairy' };
|
||||
mockedApiClient.addWatchedItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemName: 'Milk', category: 'Dairy' });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.addWatchedItem).toHaveBeenCalledWith('Milk', 'Dairy');
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Item added to watched list');
|
||||
});
|
||||
|
||||
it('should add a watched item without category', async () => {
|
||||
const mockResponse = { id: 1, item_name: 'Bread' };
|
||||
mockedApiClient.addWatchedItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemName: 'Bread' });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.addWatchedItem).toHaveBeenCalledWith('Bread', '');
|
||||
});
|
||||
|
||||
it('should invalidate watched-items query on success', async () => {
|
||||
mockedApiClient.addWatchedItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ id: 1 }),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useAddWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemName: 'Eggs' });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['watched-items'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.addWatchedItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 409,
|
||||
json: () => Promise.resolve({ message: 'Item already watched' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemName: 'Milk' });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Item already watched');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Item already watched');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.addWatchedItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useAddWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemName: 'Cheese' });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
99
src/hooks/mutations/useCreateShoppingListMutation.test.tsx
Normal file
99
src/hooks/mutations/useCreateShoppingListMutation.test.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
// src/hooks/mutations/useCreateShoppingListMutation.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 { useCreateShoppingListMutation } from './useCreateShoppingListMutation';
|
||||
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('useCreateShoppingListMutation', () => {
|
||||
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 create a shopping list successfully', async () => {
|
||||
const mockResponse = { shopping_list_id: 1, name: 'Weekly Groceries' };
|
||||
mockedApiClient.createShoppingList.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ name: 'Weekly Groceries' });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.createShoppingList).toHaveBeenCalledWith('Weekly Groceries');
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Shopping list created');
|
||||
});
|
||||
|
||||
it('should invalidate shopping-lists query on success', async () => {
|
||||
mockedApiClient.createShoppingList.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ shopping_list_id: 1 }),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ name: 'Test List' });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['shopping-lists'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.createShoppingList.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 400,
|
||||
json: () => Promise.resolve({ message: 'List name already exists' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ name: 'Duplicate List' });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('List name already exists');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('List name already exists');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.createShoppingList.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCreateShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ name: 'Test' });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
99
src/hooks/mutations/useDeleteShoppingListMutation.test.tsx
Normal file
99
src/hooks/mutations/useDeleteShoppingListMutation.test.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
// src/hooks/mutations/useDeleteShoppingListMutation.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 { useDeleteShoppingListMutation } from './useDeleteShoppingListMutation';
|
||||
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('useDeleteShoppingListMutation', () => {
|
||||
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 delete a shopping list successfully', async () => {
|
||||
const mockResponse = { success: true };
|
||||
mockedApiClient.deleteShoppingList.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useDeleteShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 123 });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.deleteShoppingList).toHaveBeenCalledWith(123);
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Shopping list deleted');
|
||||
});
|
||||
|
||||
it('should invalidate shopping-lists query on success', async () => {
|
||||
mockedApiClient.deleteShoppingList.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ success: true }),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useDeleteShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 456 });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['shopping-lists'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.deleteShoppingList.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
json: () => Promise.resolve({ message: 'Shopping list not found' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useDeleteShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 999 });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Shopping list not found');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Shopping list not found');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.deleteShoppingList.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useDeleteShoppingListMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ listId: 123 });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
99
src/hooks/mutations/useRemoveWatchedItemMutation.test.tsx
Normal file
99
src/hooks/mutations/useRemoveWatchedItemMutation.test.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
// src/hooks/mutations/useRemoveWatchedItemMutation.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 { useRemoveWatchedItemMutation } from './useRemoveWatchedItemMutation';
|
||||
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('useRemoveWatchedItemMutation', () => {
|
||||
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 a watched item successfully', async () => {
|
||||
const mockResponse = { success: true };
|
||||
mockedApiClient.removeWatchedItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useRemoveWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ masterItemId: 123 });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.removeWatchedItem).toHaveBeenCalledWith(123);
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Item removed from watched list');
|
||||
});
|
||||
|
||||
it('should invalidate watched-items query on success', async () => {
|
||||
mockedApiClient.removeWatchedItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ success: true }),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useRemoveWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ masterItemId: 456 });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['watched-items'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.removeWatchedItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
json: () => Promise.resolve({ message: 'Watched item not found' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useRemoveWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ masterItemId: 999 });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Watched item not found');
|
||||
expect(mockedNotifications.notifyError).toHaveBeenCalledWith('Watched item not found');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.removeWatchedItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useRemoveWatchedItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ masterItemId: 123 });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
159
src/hooks/mutations/useUpdateShoppingListItemMutation.test.tsx
Normal file
159
src/hooks/mutations/useUpdateShoppingListItemMutation.test.tsx
Normal file
@@ -0,0 +1,159 @@
|
||||
// src/hooks/mutations/useUpdateShoppingListItemMutation.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 { useUpdateShoppingListItemMutation } from './useUpdateShoppingListItemMutation';
|
||||
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('useUpdateShoppingListItemMutation', () => {
|
||||
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 update a shopping list item successfully', async () => {
|
||||
const mockResponse = { id: 42, quantity: 3 };
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockResponse),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { quantity: 3 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.updateShoppingListItem).toHaveBeenCalledWith(42, { quantity: 3 });
|
||||
expect(mockedNotifications.notifySuccess).toHaveBeenCalledWith('Shopping list item updated');
|
||||
});
|
||||
|
||||
it('should update is_purchased status', async () => {
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ id: 42, is_purchased: true }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { is_purchased: true } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.updateShoppingListItem).toHaveBeenCalledWith(42, { is_purchased: true });
|
||||
});
|
||||
|
||||
it('should update custom_item_name', async () => {
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ id: 42, custom_item_name: 'Organic Milk' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { custom_item_name: 'Organic Milk' } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.updateShoppingListItem).toHaveBeenCalledWith(42, { custom_item_name: 'Organic Milk' });
|
||||
});
|
||||
|
||||
it('should update notes', async () => {
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ id: 42, notes: 'Get the 2% variety' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { notes: 'Get the 2% variety' } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.updateShoppingListItem).toHaveBeenCalledWith(42, { notes: 'Get the 2% variety' });
|
||||
});
|
||||
|
||||
it('should update multiple fields at once', async () => {
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ id: 42, quantity: 2, notes: 'Important' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { quantity: 2, notes: 'Important' } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.updateShoppingListItem).toHaveBeenCalledWith(42, { quantity: 2, notes: 'Important' });
|
||||
});
|
||||
|
||||
it('should invalidate shopping-lists query on success', async () => {
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ id: 42 }),
|
||||
} as Response);
|
||||
|
||||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries');
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { quantity: 5 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ queryKey: ['shopping-lists'] });
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.updateShoppingListItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
json: () => Promise.resolve({ message: 'Item not found' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 999, updates: { quantity: 1 } });
|
||||
|
||||
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.updateShoppingListItem.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useUpdateShoppingListItemMutation(), { wrapper });
|
||||
|
||||
result.current.mutate({ itemId: 42, updates: { quantity: 1 } });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
102
src/hooks/queries/useActivityLogQuery.test.tsx
Normal file
102
src/hooks/queries/useActivityLogQuery.test.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
// src/hooks/queries/useActivityLogQuery.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 { useActivityLogQuery } from './useActivityLogQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useActivityLogQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch activity log with default params', async () => {
|
||||
const mockActivityLog = [
|
||||
{ id: 1, action: 'user_login', timestamp: '2024-01-01T10:00:00Z' },
|
||||
{ id: 2, action: 'flyer_uploaded', timestamp: '2024-01-01T11:00:00Z' },
|
||||
];
|
||||
mockedApiClient.fetchActivityLog.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockActivityLog),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useActivityLogQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchActivityLog).toHaveBeenCalledWith(20, 0);
|
||||
expect(result.current.data).toEqual(mockActivityLog);
|
||||
});
|
||||
|
||||
it('should fetch activity log with custom limit and offset', async () => {
|
||||
const mockActivityLog = [{ id: 3, action: 'item_added', timestamp: '2024-01-01T12:00:00Z' }];
|
||||
mockedApiClient.fetchActivityLog.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockActivityLog),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useActivityLogQuery(10, 5), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchActivityLog).toHaveBeenCalledWith(10, 5);
|
||||
expect(result.current.data).toEqual(mockActivityLog);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchActivityLog.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 403,
|
||||
json: () => Promise.resolve({ message: 'Admin access required' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useActivityLogQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Admin access required');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchActivityLog.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useActivityLogQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no activity log entries', async () => {
|
||||
mockedApiClient.fetchActivityLog.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useActivityLogQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
78
src/hooks/queries/useApplicationStatsQuery.test.tsx
Normal file
78
src/hooks/queries/useApplicationStatsQuery.test.tsx
Normal file
@@ -0,0 +1,78 @@
|
||||
// src/hooks/queries/useApplicationStatsQuery.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 { useApplicationStatsQuery } from './useApplicationStatsQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useApplicationStatsQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch application stats successfully', async () => {
|
||||
const mockStats = {
|
||||
flyerCount: 150,
|
||||
userCount: 500,
|
||||
flyerItemCount: 5000,
|
||||
storeCount: 25,
|
||||
pendingCorrectionsCount: 10,
|
||||
recipeCount: 75,
|
||||
};
|
||||
mockedApiClient.getApplicationStats.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockStats),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useApplicationStatsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.getApplicationStats).toHaveBeenCalled();
|
||||
expect(result.current.data).toEqual(mockStats);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.getApplicationStats.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 403,
|
||||
json: () => Promise.resolve({ message: 'Admin access required' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useApplicationStatsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Admin access required');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.getApplicationStats.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useApplicationStatsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
});
|
||||
88
src/hooks/queries/useCategoriesQuery.test.tsx
Normal file
88
src/hooks/queries/useCategoriesQuery.test.tsx
Normal file
@@ -0,0 +1,88 @@
|
||||
// src/hooks/queries/useCategoriesQuery.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 { useCategoriesQuery } from './useCategoriesQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useCategoriesQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch categories successfully', async () => {
|
||||
const mockCategories = [
|
||||
{ category_id: 1, name: 'Dairy' },
|
||||
{ category_id: 2, name: 'Bakery' },
|
||||
{ category_id: 3, name: 'Produce' },
|
||||
];
|
||||
mockedApiClient.fetchCategories.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockCategories),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCategoriesQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchCategories).toHaveBeenCalled();
|
||||
expect(result.current.data).toEqual(mockCategories);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchCategories.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.resolve({ message: 'Database error' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCategoriesQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Database error');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchCategories.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCategoriesQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no categories', async () => {
|
||||
mockedApiClient.fetchCategories.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useCategoriesQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
111
src/hooks/queries/useFlyerItemsQuery.test.tsx
Normal file
111
src/hooks/queries/useFlyerItemsQuery.test.tsx
Normal file
@@ -0,0 +1,111 @@
|
||||
// src/hooks/queries/useFlyerItemsQuery.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 { useFlyerItemsQuery } from './useFlyerItemsQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useFlyerItemsQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch flyer items when flyerId is provided', async () => {
|
||||
const mockFlyerItems = [
|
||||
{ item_id: 1, name: 'Milk', price: 3.99, flyer_id: 42 },
|
||||
{ item_id: 2, name: 'Bread', price: 2.49, flyer_id: 42 },
|
||||
];
|
||||
mockedApiClient.fetchFlyerItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ items: mockFlyerItems }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItemsQuery(42), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchFlyerItems).toHaveBeenCalledWith(42);
|
||||
expect(result.current.data).toEqual(mockFlyerItems);
|
||||
});
|
||||
|
||||
it('should not fetch when flyerId is undefined', async () => {
|
||||
const { result } = renderHook(() => useFlyerItemsQuery(undefined), { wrapper });
|
||||
|
||||
// Wait a bit to ensure the query doesn't run
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
expect(mockedApiClient.fetchFlyerItems).not.toHaveBeenCalled();
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(result.current.isFetching).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchFlyerItems.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 404,
|
||||
json: () => Promise.resolve({ message: 'Flyer not found' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItemsQuery(999), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Flyer not found');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchFlyerItems.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItemsQuery(42), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array when API returns no items', async () => {
|
||||
mockedApiClient.fetchFlyerItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ items: [] }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItemsQuery(42), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle response without items property', async () => {
|
||||
mockedApiClient.fetchFlyerItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({}),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItemsQuery(42), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
102
src/hooks/queries/useFlyersQuery.test.tsx
Normal file
102
src/hooks/queries/useFlyersQuery.test.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
// src/hooks/queries/useFlyersQuery.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 { useFlyersQuery } from './useFlyersQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useFlyersQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch flyers successfully with default params', async () => {
|
||||
const mockFlyers = [
|
||||
{ flyer_id: 1, store_name: 'Store A', valid_from: '2024-01-01', valid_to: '2024-01-07' },
|
||||
{ flyer_id: 2, store_name: 'Store B', valid_from: '2024-01-01', valid_to: '2024-01-07' },
|
||||
];
|
||||
mockedApiClient.fetchFlyers.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockFlyers),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyersQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchFlyers).toHaveBeenCalledWith(20, 0);
|
||||
expect(result.current.data).toEqual(mockFlyers);
|
||||
});
|
||||
|
||||
it('should fetch flyers with custom limit and offset', async () => {
|
||||
const mockFlyers = [{ flyer_id: 3, store_name: 'Store C' }];
|
||||
mockedApiClient.fetchFlyers.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockFlyers),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyersQuery(10, 5), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchFlyers).toHaveBeenCalledWith(10, 5);
|
||||
expect(result.current.data).toEqual(mockFlyers);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchFlyers.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.resolve({ message: 'Server error' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyersQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Server error');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchFlyers.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyersQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no flyers', async () => {
|
||||
mockedApiClient.fetchFlyers.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useFlyersQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
88
src/hooks/queries/useMasterItemsQuery.test.tsx
Normal file
88
src/hooks/queries/useMasterItemsQuery.test.tsx
Normal file
@@ -0,0 +1,88 @@
|
||||
// src/hooks/queries/useMasterItemsQuery.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 { useMasterItemsQuery } from './useMasterItemsQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useMasterItemsQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch master items successfully', async () => {
|
||||
const mockMasterItems = [
|
||||
{ master_item_id: 1, name: 'Milk', category: 'Dairy' },
|
||||
{ master_item_id: 2, name: 'Bread', category: 'Bakery' },
|
||||
{ master_item_id: 3, name: 'Eggs', category: 'Dairy' },
|
||||
];
|
||||
mockedApiClient.fetchMasterItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockMasterItems),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useMasterItemsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchMasterItems).toHaveBeenCalled();
|
||||
expect(result.current.data).toEqual(mockMasterItems);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchMasterItems.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.resolve({ message: 'Database error' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useMasterItemsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Database error');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchMasterItems.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useMasterItemsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no master items', async () => {
|
||||
mockedApiClient.fetchMasterItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useMasterItemsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
98
src/hooks/queries/useShoppingListsQuery.test.tsx
Normal file
98
src/hooks/queries/useShoppingListsQuery.test.tsx
Normal file
@@ -0,0 +1,98 @@
|
||||
// src/hooks/queries/useShoppingListsQuery.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 { useShoppingListsQuery } from './useShoppingListsQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useShoppingListsQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch shopping lists when enabled', async () => {
|
||||
const mockShoppingLists = [
|
||||
{ shopping_list_id: 1, name: 'Weekly Groceries', items: [] },
|
||||
{ shopping_list_id: 2, name: 'Party Supplies', items: [] },
|
||||
];
|
||||
mockedApiClient.fetchShoppingLists.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockShoppingLists),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useShoppingListsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchShoppingLists).toHaveBeenCalled();
|
||||
expect(result.current.data).toEqual(mockShoppingLists);
|
||||
});
|
||||
|
||||
it('should not fetch shopping lists when disabled', async () => {
|
||||
const { result } = renderHook(() => useShoppingListsQuery(false), { wrapper });
|
||||
|
||||
// Wait a bit to ensure the query doesn't run
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
expect(mockedApiClient.fetchShoppingLists).not.toHaveBeenCalled();
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(result.current.isFetching).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchShoppingLists.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 401,
|
||||
json: () => Promise.resolve({ message: 'Unauthorized' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useShoppingListsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Unauthorized');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchShoppingLists.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useShoppingListsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no shopping lists', async () => {
|
||||
mockedApiClient.fetchShoppingLists.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useShoppingListsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
87
src/hooks/queries/useSuggestedCorrectionsQuery.test.tsx
Normal file
87
src/hooks/queries/useSuggestedCorrectionsQuery.test.tsx
Normal file
@@ -0,0 +1,87 @@
|
||||
// src/hooks/queries/useSuggestedCorrectionsQuery.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 { useSuggestedCorrectionsQuery } from './useSuggestedCorrectionsQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useSuggestedCorrectionsQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch suggested corrections successfully', async () => {
|
||||
const mockCorrections = [
|
||||
{ correction_id: 1, item_name: 'Milk', suggested_name: 'Whole Milk', status: 'pending' },
|
||||
{ correction_id: 2, item_name: 'Bread', suggested_name: 'White Bread', status: 'pending' },
|
||||
];
|
||||
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockCorrections),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.getSuggestedCorrections).toHaveBeenCalled();
|
||||
expect(result.current.data).toEqual(mockCorrections);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 403,
|
||||
json: () => Promise.resolve({ message: 'Admin access required' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Admin access required');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no corrections', async () => {
|
||||
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
98
src/hooks/queries/useWatchedItemsQuery.test.tsx
Normal file
98
src/hooks/queries/useWatchedItemsQuery.test.tsx
Normal file
@@ -0,0 +1,98 @@
|
||||
// src/hooks/queries/useWatchedItemsQuery.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 { useWatchedItemsQuery } from './useWatchedItemsQuery';
|
||||
import * as apiClient from '../../services/apiClient';
|
||||
|
||||
vi.mock('../../services/apiClient');
|
||||
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
|
||||
describe('useWatchedItemsQuery', () => {
|
||||
let queryClient: QueryClient;
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch watched items when enabled', async () => {
|
||||
const mockWatchedItems = [
|
||||
{ master_item_id: 1, name: 'Milk', category: 'Dairy' },
|
||||
{ master_item_id: 2, name: 'Bread', category: 'Bakery' },
|
||||
];
|
||||
mockedApiClient.fetchWatchedItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockWatchedItems),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useWatchedItemsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(mockedApiClient.fetchWatchedItems).toHaveBeenCalled();
|
||||
expect(result.current.data).toEqual(mockWatchedItems);
|
||||
});
|
||||
|
||||
it('should not fetch watched items when disabled', async () => {
|
||||
const { result } = renderHook(() => useWatchedItemsQuery(false), { wrapper });
|
||||
|
||||
// Wait a bit to ensure the query doesn't run
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
expect(mockedApiClient.fetchWatchedItems).not.toHaveBeenCalled();
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(result.current.isFetching).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle API error with error message', async () => {
|
||||
mockedApiClient.fetchWatchedItems.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 401,
|
||||
json: () => Promise.resolve({ message: 'Unauthorized' }),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useWatchedItemsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Unauthorized');
|
||||
});
|
||||
|
||||
it('should handle API error without message', async () => {
|
||||
mockedApiClient.fetchWatchedItems.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
json: () => Promise.reject(new Error('Parse error')),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useWatchedItemsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isError).toBe(true));
|
||||
|
||||
expect(result.current.error?.message).toBe('Request failed with status 500');
|
||||
});
|
||||
|
||||
it('should return empty array for no watched items', async () => {
|
||||
mockedApiClient.fetchWatchedItems.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
} as Response);
|
||||
|
||||
const { result } = renderHook(() => useWatchedItemsQuery(true), { wrapper });
|
||||
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
||||
|
||||
expect(result.current.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -2,14 +2,13 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { useFlyerItems } from './useFlyerItems';
|
||||
import { useApiOnMount } from './useApiOnMount';
|
||||
import * as apiClient from '../services/apiClient';
|
||||
import * as useFlyerItemsQueryModule from './queries/useFlyerItemsQuery';
|
||||
import { createMockFlyer, createMockFlyerItem } from '../tests/utils/mockFactories';
|
||||
|
||||
// Mock the underlying useApiOnMount hook to isolate the useFlyerItems hook's logic.
|
||||
vi.mock('./useApiOnMount');
|
||||
// Mock the underlying query hook to isolate the useFlyerItems hook's logic.
|
||||
vi.mock('./queries/useFlyerItemsQuery');
|
||||
|
||||
const mockedUseApiOnMount = vi.mocked(useApiOnMount);
|
||||
const mockedUseFlyerItemsQuery = vi.mocked(useFlyerItemsQueryModule.useFlyerItemsQuery);
|
||||
|
||||
describe('useFlyerItems Hook', () => {
|
||||
const mockFlyer = createMockFlyer({
|
||||
@@ -39,19 +38,16 @@ describe('useFlyerItems Hook', () => {
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
// Clear mock history before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return initial state and not call useApiOnMount when flyer is null', () => {
|
||||
// Arrange: Mock the return value of the inner hook.
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: false,
|
||||
it('should return initial state when flyer is null', () => {
|
||||
// Arrange: Mock the return value of the query hook.
|
||||
mockedUseFlyerItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as ReturnType<typeof useFlyerItemsQueryModule.useFlyerItemsQuery>);
|
||||
|
||||
// Act: Render the hook with a null flyer.
|
||||
const { result } = renderHook(() => useFlyerItems(null));
|
||||
@@ -60,57 +56,41 @@ describe('useFlyerItems Hook', () => {
|
||||
expect(result.current.flyerItems).toEqual([]);
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(result.current.error).toBeNull();
|
||||
// Assert: Check that useApiOnMount was called with `enabled: false`.
|
||||
expect(mockedUseApiOnMount).toHaveBeenCalledWith(
|
||||
expect.any(Function), // the wrapped fetcher function
|
||||
[null], // dependencies array
|
||||
{ enabled: false }, // options object
|
||||
undefined, // flyer_id
|
||||
);
|
||||
// Assert: Check that useFlyerItemsQuery was called with undefined flyerId.
|
||||
expect(mockedUseFlyerItemsQuery).toHaveBeenCalledWith(undefined);
|
||||
});
|
||||
|
||||
it('should call useApiOnMount with enabled: true when a flyer is provided', () => {
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: true,
|
||||
it('should call useFlyerItemsQuery with flyerId when a flyer is provided', () => {
|
||||
mockedUseFlyerItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as ReturnType<typeof useFlyerItemsQueryModule.useFlyerItemsQuery>);
|
||||
|
||||
renderHook(() => useFlyerItems(mockFlyer));
|
||||
|
||||
// Assert: Check that useApiOnMount was called with the correct parameters.
|
||||
expect(mockedUseApiOnMount).toHaveBeenCalledWith(
|
||||
expect.any(Function),
|
||||
[mockFlyer],
|
||||
{ enabled: true },
|
||||
mockFlyer.flyer_id,
|
||||
);
|
||||
// Assert: Check that useFlyerItemsQuery was called with the correct flyerId.
|
||||
expect(mockedUseFlyerItemsQuery).toHaveBeenCalledWith(123);
|
||||
});
|
||||
|
||||
it('should return isLoading: true when the inner hook is loading', () => {
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: true,
|
||||
it('should return isLoading: true when the query is loading', () => {
|
||||
mockedUseFlyerItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as ReturnType<typeof useFlyerItemsQueryModule.useFlyerItemsQuery>);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItems(mockFlyer));
|
||||
|
||||
expect(result.current.isLoading).toBe(true);
|
||||
});
|
||||
|
||||
it('should return flyerItems when the inner hook provides data', () => {
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: { items: mockFlyerItems },
|
||||
loading: false,
|
||||
it('should return flyerItems when the query provides data', () => {
|
||||
mockedUseFlyerItemsQuery.mockReturnValue({
|
||||
data: mockFlyerItems,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as ReturnType<typeof useFlyerItemsQueryModule.useFlyerItemsQuery>);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItems(mockFlyer));
|
||||
|
||||
@@ -119,15 +99,13 @@ describe('useFlyerItems Hook', () => {
|
||||
expect(result.current.error).toBeNull();
|
||||
});
|
||||
|
||||
it('should return an error when the inner hook returns an error', () => {
|
||||
it('should return an error when the query returns an error', () => {
|
||||
const mockError = new Error('Failed to fetch');
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: false,
|
||||
mockedUseFlyerItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: false,
|
||||
error: mockError,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as ReturnType<typeof useFlyerItemsQueryModule.useFlyerItemsQuery>);
|
||||
|
||||
const { result } = renderHook(() => useFlyerItems(mockFlyer));
|
||||
|
||||
@@ -135,46 +113,4 @@ describe('useFlyerItems Hook', () => {
|
||||
expect(result.current.flyerItems).toEqual([]);
|
||||
expect(result.current.error).toEqual(mockError);
|
||||
});
|
||||
|
||||
describe('wrappedFetcher behavior', () => {
|
||||
it('should reject if called with undefined flyerId', async () => {
|
||||
// We need to trigger the hook to get access to the internal wrappedFetcher
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
renderHook(() => useFlyerItems(mockFlyer));
|
||||
|
||||
// The first argument passed to useApiOnMount is the wrappedFetcher function
|
||||
const wrappedFetcher = mockedUseApiOnMount.mock.calls[0][0];
|
||||
|
||||
// Verify the fetcher rejects when no ID is passed (which shouldn't happen in normal flow due to 'enabled')
|
||||
await expect(wrappedFetcher(undefined)).rejects.toThrow(
|
||||
'Cannot fetch items for an undefined flyer ID.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should call apiClient.fetchFlyerItems when called with a valid ID', async () => {
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
renderHook(() => useFlyerItems(mockFlyer));
|
||||
|
||||
const wrappedFetcher = mockedUseApiOnMount.mock.calls[0][0];
|
||||
const mockResponse = new Response();
|
||||
const mockedApiClient = vi.mocked(apiClient);
|
||||
mockedApiClient.fetchFlyerItems.mockResolvedValue(mockResponse);
|
||||
const response = await wrappedFetcher(123);
|
||||
|
||||
expect(mockedApiClient.fetchFlyerItems).toHaveBeenCalledWith(123);
|
||||
expect(response).toBe(mockResponse);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,15 +4,15 @@ import { renderHook } from '@testing-library/react';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { useMasterItems } from './useMasterItems';
|
||||
import { MasterItemsProvider } from '../providers/MasterItemsProvider';
|
||||
import { useApiOnMount } from './useApiOnMount';
|
||||
import { useMasterItemsQuery } from './queries/useMasterItemsQuery';
|
||||
import type { MasterGroceryItem } from '../types';
|
||||
import { createMockMasterGroceryItem } from '../tests/utils/mockFactories';
|
||||
|
||||
// 1. Mock the useApiOnMount hook, which is the dependency of our provider.
|
||||
vi.mock('./useApiOnMount');
|
||||
// 1. Mock the useMasterItemsQuery hook, which is the dependency of our provider.
|
||||
vi.mock('./queries/useMasterItemsQuery');
|
||||
|
||||
// 2. Create a typed mock for type safety and autocompletion.
|
||||
const mockedUseApiOnMount = vi.mocked(useApiOnMount);
|
||||
const mockedUseMasterItemsQuery = vi.mocked(useMasterItemsQuery);
|
||||
|
||||
// 3. A simple wrapper component that renders our provider.
|
||||
// This is necessary because the useMasterItems hook needs to be a child of MasterItemsProvider.
|
||||
@@ -42,13 +42,11 @@ describe('useMasterItems Hook and MasterItemsProvider', () => {
|
||||
|
||||
it('should return the initial loading state correctly', () => {
|
||||
// Arrange: Configure the mocked hook to return a loading state.
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: true,
|
||||
mockedUseMasterItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as any);
|
||||
|
||||
// Act: Render the hook within the provider wrapper.
|
||||
const { result } = renderHook(() => useMasterItems(), { wrapper });
|
||||
@@ -75,13 +73,11 @@ describe('useMasterItems Hook and MasterItemsProvider', () => {
|
||||
category_name: 'Bakery',
|
||||
}),
|
||||
];
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
mockedUseMasterItemsQuery.mockReturnValue({
|
||||
data: mockItems,
|
||||
loading: false,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as any);
|
||||
|
||||
// Act
|
||||
const { result } = renderHook(() => useMasterItems(), { wrapper });
|
||||
@@ -95,13 +91,11 @@ describe('useMasterItems Hook and MasterItemsProvider', () => {
|
||||
it('should return an error state if the fetch fails', () => {
|
||||
// Arrange: Mock a failed data fetch.
|
||||
const mockError = new Error('Failed to fetch master items');
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: false,
|
||||
mockedUseMasterItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: false,
|
||||
error: mockError,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as any);
|
||||
|
||||
// Act
|
||||
const { result } = renderHook(() => useMasterItems(), { wrapper });
|
||||
|
||||
@@ -5,7 +5,8 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { useUserData } from './useUserData';
|
||||
import { useAuth } from './useAuth';
|
||||
import { UserDataProvider } from '../providers/UserDataProvider';
|
||||
import { useApiOnMount } from './useApiOnMount';
|
||||
import { useWatchedItemsQuery } from './queries/useWatchedItemsQuery';
|
||||
import { useShoppingListsQuery } from './queries/useShoppingListsQuery';
|
||||
import type { UserProfile } from '../types';
|
||||
import {
|
||||
createMockMasterGroceryItem,
|
||||
@@ -15,11 +16,13 @@ import {
|
||||
|
||||
// 1. Mock the hook's dependencies
|
||||
vi.mock('../hooks/useAuth');
|
||||
vi.mock('./useApiOnMount');
|
||||
vi.mock('./queries/useWatchedItemsQuery');
|
||||
vi.mock('./queries/useShoppingListsQuery');
|
||||
|
||||
// 2. Create typed mocks for type safety and autocompletion
|
||||
const mockedUseAuth = vi.mocked(useAuth);
|
||||
const mockedUseApiOnMount = vi.mocked(useApiOnMount);
|
||||
const mockedUseWatchedItemsQuery = vi.mocked(useWatchedItemsQuery);
|
||||
const mockedUseShoppingListsQuery = vi.mocked(useShoppingListsQuery);
|
||||
|
||||
// 3. A simple wrapper component that renders our provider.
|
||||
// This is necessary because the useUserData hook needs to be a child of UserDataProvider.
|
||||
@@ -71,13 +74,16 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
updateProfile: vi.fn(),
|
||||
});
|
||||
// Arrange: Mock the return value of the inner hooks.
|
||||
mockedUseApiOnMount.mockReturnValue({
|
||||
data: null,
|
||||
loading: false,
|
||||
mockedUseWatchedItemsQuery.mockReturnValue({
|
||||
data: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
} as any);
|
||||
mockedUseShoppingListsQuery.mockReturnValue({
|
||||
data: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
|
||||
// Act: Render the hook within the provider wrapper.
|
||||
const { result } = renderHook(() => useUserData(), { wrapper });
|
||||
@@ -87,10 +93,9 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
expect(result.current.watchedItems).toEqual([]);
|
||||
expect(result.current.shoppingLists).toEqual([]);
|
||||
expect(result.current.error).toBeNull();
|
||||
// Assert: Check that useApiOnMount was called with `enabled: false`.
|
||||
expect(mockedUseApiOnMount).toHaveBeenCalledWith(expect.any(Function), [null], {
|
||||
enabled: false,
|
||||
});
|
||||
// Assert: Check that queries were disabled (called with false)
|
||||
expect(mockedUseWatchedItemsQuery).toHaveBeenCalledWith(false);
|
||||
expect(mockedUseShoppingListsQuery).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it('should return loading state when user is authenticated and data is fetching', () => {
|
||||
@@ -104,21 +109,16 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
updateProfile: vi.fn(),
|
||||
});
|
||||
// Arrange: Mock one of the inner hooks to be in a loading state.
|
||||
mockedUseApiOnMount
|
||||
.mockReturnValueOnce({
|
||||
data: null,
|
||||
loading: true,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
}) // watched items
|
||||
.mockReturnValueOnce({
|
||||
data: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
}); // shopping lists
|
||||
mockedUseWatchedItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
} as any);
|
||||
mockedUseShoppingListsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
|
||||
// Act
|
||||
const { result } = renderHook(() => useUserData(), { wrapper });
|
||||
@@ -138,21 +138,16 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
updateProfile: vi.fn(),
|
||||
});
|
||||
// Arrange: Mock successful data fetches for both inner hooks.
|
||||
mockedUseApiOnMount
|
||||
.mockReturnValueOnce({
|
||||
data: mockWatchedItems,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
})
|
||||
.mockReturnValueOnce({
|
||||
data: mockShoppingLists,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
mockedUseWatchedItemsQuery.mockReturnValue({
|
||||
data: mockWatchedItems,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
mockedUseShoppingListsQuery.mockReturnValue({
|
||||
data: mockShoppingLists,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
|
||||
// Act
|
||||
const { result } = renderHook(() => useUserData(), { wrapper });
|
||||
@@ -178,55 +173,16 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
});
|
||||
const mockError = new Error('Failed to fetch watched items');
|
||||
|
||||
// Arrange: Mock the behavior persistently to handle re-renders.
|
||||
// We use mockImplementation to return based on call order in a loop or similar,
|
||||
// OR just use mockReturnValueOnce enough times.
|
||||
// Since we don't know exact render count, mockImplementation is safer if valid.
|
||||
// But simplified: assuming 2 hooks called per render.
|
||||
|
||||
// reset mocks to be sure
|
||||
mockedUseApiOnMount.mockReset();
|
||||
|
||||
// Define the sequence: 1st call (Watched) -> Error, 2nd call (Shopping) -> Success
|
||||
// We want this to persist for multiple renders.
|
||||
mockedUseApiOnMount.mockImplementation((_fn) => {
|
||||
// We can't easily distinguish based on 'fn' arg without inspecting it,
|
||||
// but we know the order is Watched then Shopping in the provider.
|
||||
// A simple toggle approach works if strict order is maintained.
|
||||
// However, stateless mocks are better.
|
||||
// Let's fallback to setting up "many" return values.
|
||||
return { data: null, loading: false, error: null, isRefetching: false, reset: vi.fn() };
|
||||
});
|
||||
|
||||
mockedUseApiOnMount
|
||||
.mockReturnValueOnce({
|
||||
data: null,
|
||||
loading: false,
|
||||
error: mockError,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
}) // 1st render: Watched
|
||||
.mockReturnValueOnce({
|
||||
data: mockShoppingLists,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
}) // 1st render: Shopping
|
||||
.mockReturnValueOnce({
|
||||
data: null,
|
||||
loading: false,
|
||||
error: mockError,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
}) // 2nd render: Watched
|
||||
.mockReturnValueOnce({
|
||||
data: mockShoppingLists,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
}); // 2nd render: Shopping
|
||||
mockedUseWatchedItemsQuery.mockReturnValue({
|
||||
data: undefined,
|
||||
isLoading: false,
|
||||
error: mockError,
|
||||
} as any);
|
||||
mockedUseShoppingListsQuery.mockReturnValue({
|
||||
data: mockShoppingLists,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
|
||||
// Act
|
||||
const { result } = renderHook(() => useUserData(), { wrapper });
|
||||
@@ -252,21 +208,16 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
logout: vi.fn(),
|
||||
updateProfile: vi.fn(),
|
||||
});
|
||||
mockedUseApiOnMount
|
||||
.mockReturnValueOnce({
|
||||
data: mockWatchedItems,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
})
|
||||
.mockReturnValueOnce({
|
||||
data: mockShoppingLists,
|
||||
loading: false,
|
||||
error: null,
|
||||
isRefetching: false,
|
||||
reset: vi.fn(),
|
||||
});
|
||||
mockedUseWatchedItemsQuery.mockReturnValue({
|
||||
data: mockWatchedItems,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
mockedUseShoppingListsQuery.mockReturnValue({
|
||||
data: mockShoppingLists,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
const { result, rerender } = renderHook(() => useUserData(), { wrapper });
|
||||
await waitFor(() => expect(result.current.watchedItems).not.toEqual([]));
|
||||
|
||||
@@ -279,6 +230,18 @@ describe('useUserData Hook and UserDataProvider', () => {
|
||||
logout: vi.fn(),
|
||||
updateProfile: vi.fn(),
|
||||
});
|
||||
// Update mocks to return empty data for the logged out state
|
||||
mockedUseWatchedItemsQuery.mockReturnValue({
|
||||
data: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
mockedUseShoppingListsQuery.mockReturnValue({
|
||||
data: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
} as any);
|
||||
|
||||
rerender();
|
||||
|
||||
// Assert: The data should now be cleared.
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
ValidationError,
|
||||
NotFoundError,
|
||||
} from '../services/db/errors.db';
|
||||
import { createMockRequest } from '../tests/utils/createMockRequest';
|
||||
import type { Logger } from 'pino';
|
||||
|
||||
// Create a mock logger that we can inject into requests and assert against.
|
||||
@@ -271,7 +272,7 @@ describe('errorHandler Middleware', () => {
|
||||
it('should call next(err) if headers have already been sent', () => {
|
||||
// Supertest doesn't easily allow simulating res.headersSent = true mid-request
|
||||
// We need to mock the express response object directly for this specific test.
|
||||
const mockRequestDirect: Partial<Request> = { path: '/headers-sent-error', method: 'GET' };
|
||||
const mockRequestDirect = createMockRequest({ path: '/headers-sent-error', method: 'GET' });
|
||||
const mockResponseDirect: Partial<Response> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { requireFileUpload } from './fileUpload.middleware';
|
||||
import { ValidationError } from '../services/db/errors.db';
|
||||
import { createMockRequest } from '../tests/utils/createMockRequest';
|
||||
|
||||
describe('requireFileUpload Middleware', () => {
|
||||
let mockRequest: Partial<Request>;
|
||||
@@ -11,7 +12,7 @@ describe('requireFileUpload Middleware', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset mocks before each test
|
||||
mockRequest = {};
|
||||
mockRequest = createMockRequest();
|
||||
mockResponse = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { Request, Response, NextFunction } from 'express';
|
||||
import { createUploadMiddleware, handleMulterError } from './multer.middleware';
|
||||
import { createMockUserProfile } from '../tests/utils/mockFactories';
|
||||
import { ValidationError } from '../services/db/errors.db';
|
||||
import { createMockRequest } from '../tests/utils/createMockRequest';
|
||||
|
||||
// 1. Hoist the mocks so they can be referenced inside vi.mock factories.
|
||||
const mocks = vi.hoisted(() => ({
|
||||
@@ -125,7 +126,7 @@ describe('createUploadMiddleware', () => {
|
||||
createUploadMiddleware({ storageType: 'avatar' });
|
||||
const storageOptions = vi.mocked(multer.diskStorage).mock.calls[0][0];
|
||||
const cb = vi.fn();
|
||||
const mockReq = { user: mockUser } as unknown as Request;
|
||||
const mockReq = createMockRequest({ user: mockUser });
|
||||
|
||||
storageOptions.filename!(mockReq, mockFile, cb);
|
||||
|
||||
@@ -138,7 +139,7 @@ describe('createUploadMiddleware', () => {
|
||||
createUploadMiddleware({ storageType: 'avatar' });
|
||||
const storageOptions = vi.mocked(multer.diskStorage).mock.calls[0][0];
|
||||
const cb = vi.fn();
|
||||
const mockReq = {} as Request; // No user on request
|
||||
const mockReq = createMockRequest(); // No user on request
|
||||
|
||||
storageOptions.filename!(mockReq, mockFile, cb);
|
||||
|
||||
@@ -153,7 +154,7 @@ describe('createUploadMiddleware', () => {
|
||||
createUploadMiddleware({ storageType: 'avatar' });
|
||||
const storageOptions = vi.mocked(multer.diskStorage).mock.calls[0][0];
|
||||
const cb = vi.fn();
|
||||
const mockReq = { user: mockUser } as unknown as Request;
|
||||
const mockReq = createMockRequest({ user: mockUser });
|
||||
|
||||
storageOptions.filename!(mockReq, mockFile, cb);
|
||||
|
||||
@@ -171,7 +172,7 @@ describe('createUploadMiddleware', () => {
|
||||
createUploadMiddleware({ storageType: 'flyer' });
|
||||
const storageOptions = vi.mocked(multer.diskStorage).mock.calls[0][0];
|
||||
const cb = vi.fn();
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
|
||||
storageOptions.filename!(mockReq, mockFlyerFile, cb);
|
||||
|
||||
@@ -191,7 +192,7 @@ describe('createUploadMiddleware', () => {
|
||||
createUploadMiddleware({ storageType: 'flyer' });
|
||||
const storageOptions = vi.mocked(multer.diskStorage).mock.calls[0][0];
|
||||
const cb = vi.fn();
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
|
||||
storageOptions.filename!(mockReq, mockFlyerFile, cb);
|
||||
|
||||
@@ -206,7 +207,7 @@ describe('createUploadMiddleware', () => {
|
||||
const cb = vi.fn();
|
||||
const mockImageFile = { mimetype: 'image/png' } as Express.Multer.File;
|
||||
|
||||
multerOptions!.fileFilter!({} as Request, mockImageFile, cb);
|
||||
multerOptions!.fileFilter!(createMockRequest(), mockImageFile, cb);
|
||||
|
||||
expect(cb).toHaveBeenCalledWith(null, true);
|
||||
});
|
||||
@@ -217,7 +218,7 @@ describe('createUploadMiddleware', () => {
|
||||
const cb = vi.fn();
|
||||
const mockTextFile = { mimetype: 'text/plain' } as Express.Multer.File;
|
||||
|
||||
multerOptions!.fileFilter!({} as Request, { ...mockTextFile, fieldname: 'test' }, cb);
|
||||
multerOptions!.fileFilter!(createMockRequest(), { ...mockTextFile, fieldname: 'test' }, cb);
|
||||
|
||||
const error = (cb as Mock).mock.calls[0][0];
|
||||
expect(error).toBeInstanceOf(ValidationError);
|
||||
@@ -232,7 +233,7 @@ describe('handleMulterError Middleware', () => {
|
||||
let mockNext: NextFunction;
|
||||
|
||||
beforeEach(() => {
|
||||
mockRequest = {};
|
||||
mockRequest = createMockRequest();
|
||||
mockResponse = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { validateRequest } from './validation.middleware';
|
||||
import { ValidationError } from '../services/db/errors.db';
|
||||
import { createMockRequest } from '../tests/utils/createMockRequest';
|
||||
|
||||
describe('validateRequest Middleware', () => {
|
||||
let mockRequest: Partial<Request>;
|
||||
@@ -16,11 +17,11 @@ describe('validateRequest Middleware', () => {
|
||||
// This more accurately mimics the behavior of Express's request objects
|
||||
// and prevents issues with inherited properties when the middleware
|
||||
// attempts to delete keys before merging validated data.
|
||||
mockRequest = {
|
||||
mockRequest = createMockRequest({
|
||||
params: Object.create(null),
|
||||
query: Object.create(null),
|
||||
body: {},
|
||||
};
|
||||
});
|
||||
mockResponse = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn(),
|
||||
|
||||
@@ -94,33 +94,17 @@ vi.mock('../services/emailService.server', () => ({
|
||||
import authRouter from './auth.routes';
|
||||
|
||||
import { UniqueConstraintError } from '../services/db/errors.db'; // Import actual class for instanceof checks
|
||||
import { createTestApp } from '../tests/utils/createTestApp';
|
||||
|
||||
// --- 4. App Setup ---
|
||||
// We need to inject cookie-parser BEFORE the router is mounted.
|
||||
// Since createTestApp mounts the router immediately, we pass middleware to it if supported,
|
||||
// or we construct the app manually here to ensure correct order.
|
||||
// Assuming createTestApp doesn't support pre-middleware injection easily, we will
|
||||
// create a standard express app here for full control, or modify createTestApp usage if possible.
|
||||
// Looking at createTestApp.ts (inferred), it likely doesn't take middleware.
|
||||
// Let's manually build the app for this test file to ensure cookieParser runs first.
|
||||
|
||||
import express from 'express';
|
||||
import { errorHandler } from '../middleware/errorHandler'; // Assuming this exists
|
||||
|
||||
const { mockLogger } = await import('../tests/utils/mockLogger');
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(cookieParser()); // Mount BEFORE router
|
||||
|
||||
// Middleware to inject the mock logger into req
|
||||
app.use((req, res, next) => {
|
||||
req.log = mockLogger;
|
||||
next();
|
||||
// --- 4. App Setup using createTestApp ---
|
||||
const app = createTestApp({
|
||||
router: authRouter,
|
||||
basePath: '/api/auth',
|
||||
// Inject cookieParser via the new middleware option
|
||||
middleware: [cookieParser()],
|
||||
});
|
||||
|
||||
app.use('/api/auth', authRouter);
|
||||
app.use(errorHandler); // Mount AFTER router
|
||||
const { mockLogger } = await import('../tests/utils/mockLogger');
|
||||
|
||||
// --- 5. Tests ---
|
||||
describe('Auth Routes (/api/auth)', () => {
|
||||
|
||||
@@ -56,6 +56,7 @@ import {
|
||||
createMockUserProfile,
|
||||
createMockUserWithPasswordHash,
|
||||
} from '../tests/utils/mockFactories';
|
||||
import { createMockRequest } from '../tests/utils/createMockRequest';
|
||||
|
||||
// Mock dependencies before importing the passport configuration
|
||||
vi.mock('../services/db/index.db', () => ({
|
||||
@@ -112,7 +113,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
describe('LocalStrategy (Isolated Callback Logic)', () => {
|
||||
// FIX: mockReq needs a 'log' property because the implementation uses req.log
|
||||
const mockReq = { ip: '127.0.0.1', log: logger } as unknown as Request;
|
||||
const mockReq = createMockRequest({ ip: '127.0.0.1' });
|
||||
const done = vi.fn();
|
||||
|
||||
it('should call done(null, user) on successful authentication', async () => {
|
||||
@@ -454,12 +455,12 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should call next() if user has "admin" role', () => {
|
||||
// Arrange
|
||||
const mockReq: Partial<Request> = {
|
||||
const mockReq = createMockRequest({
|
||||
user: createMockUserProfile({
|
||||
role: 'admin',
|
||||
user: { user_id: 'admin-id', email: 'admin@test.com' },
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
// Act
|
||||
isAdmin(mockReq as Request, mockRes as Response, mockNext);
|
||||
@@ -471,12 +472,12 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should call next with a ForbiddenError if user does not have "admin" role', () => {
|
||||
// Arrange
|
||||
const mockReq: Partial<Request> = {
|
||||
const mockReq = createMockRequest({
|
||||
user: createMockUserProfile({
|
||||
role: 'user',
|
||||
user: { user_id: 'user-id', email: 'user@test.com' },
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
// Act
|
||||
isAdmin(mockReq as Request, mockRes as Response, mockNext);
|
||||
@@ -488,7 +489,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should call next with a ForbiddenError if req.user is missing', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request; // No req.user
|
||||
const mockReq = createMockRequest(); // No req.user
|
||||
|
||||
// Act
|
||||
isAdmin(mockReq, mockRes as Response, mockNext);
|
||||
@@ -500,12 +501,12 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should log a warning when a non-admin user tries to access an admin route', () => {
|
||||
// Arrange
|
||||
const mockReq: Partial<Request> = {
|
||||
const mockReq = createMockRequest({
|
||||
user: createMockUserProfile({
|
||||
role: 'user',
|
||||
user: { user_id: 'user-id-123', email: 'user@test.com' },
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
// Act
|
||||
isAdmin(mockReq as Request, mockRes as Response, mockNext);
|
||||
@@ -516,7 +517,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should log a warning with "unknown" user when req.user is missing', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request; // No req.user
|
||||
const mockReq = createMockRequest(); // No req.user
|
||||
|
||||
// Act
|
||||
isAdmin(mockReq, mockRes as Response, mockNext);
|
||||
@@ -533,37 +534,37 @@ describe('Passport Configuration', () => {
|
||||
};
|
||||
|
||||
// Case 1: user is not an object (e.g., a string)
|
||||
const req1 = { user: 'not-an-object' } as unknown as Request;
|
||||
const req1 = createMockRequest({ user: 'not-an-object' } as any);
|
||||
isAdmin(req1, mockRes as Response, mockNext);
|
||||
expect(mockNext).toHaveBeenLastCalledWith(expect.any(ForbiddenError));
|
||||
expect(mockRes.status).not.toHaveBeenCalled();
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Case 2: user is null
|
||||
const req2 = { user: null } as unknown as Request;
|
||||
const req2 = createMockRequest({ user: null } as any);
|
||||
isAdmin(req2, mockRes as Response, mockNext);
|
||||
expect(mockNext).toHaveBeenLastCalledWith(expect.any(ForbiddenError));
|
||||
expect(mockRes.status).not.toHaveBeenCalled();
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Case 3: user object is missing 'user' property
|
||||
const req3 = { user: { role: 'admin' } } as unknown as Request;
|
||||
const req3 = createMockRequest({ user: { role: 'admin' } } as any);
|
||||
isAdmin(req3, mockRes as Response, mockNext);
|
||||
expect(mockNext).toHaveBeenLastCalledWith(expect.any(ForbiddenError));
|
||||
expect(mockRes.status).not.toHaveBeenCalled();
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Case 4: user.user is not an object
|
||||
const req4 = { user: { role: 'admin', user: 'not-an-object' } } as unknown as Request;
|
||||
const req4 = createMockRequest({ user: { role: 'admin', user: 'not-an-object' } } as any);
|
||||
isAdmin(req4, mockRes as Response, mockNext);
|
||||
expect(mockNext).toHaveBeenLastCalledWith(expect.any(ForbiddenError));
|
||||
expect(mockRes.status).not.toHaveBeenCalled();
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Case 5: user.user is missing 'user_id'
|
||||
const req5 = {
|
||||
user: { role: 'admin', user: { email: 'test@test.com' } },
|
||||
} as unknown as Request;
|
||||
const req5 = createMockRequest({
|
||||
user: { role: 'admin', user: { email: 'test@test.com' } } as any,
|
||||
});
|
||||
isAdmin(req5, mockRes as Response, mockNext);
|
||||
expect(mockNext).toHaveBeenLastCalledWith(expect.any(ForbiddenError));
|
||||
expect(mockRes.status).not.toHaveBeenCalled();
|
||||
@@ -575,12 +576,12 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should call next with a ForbiddenError if req.user is not a valid UserProfile object', () => {
|
||||
// Arrange
|
||||
const mockReq: Partial<Request> = {
|
||||
const mockReq = createMockRequest({
|
||||
// An object that is not a valid UserProfile (e.g., missing 'role')
|
||||
user: {
|
||||
user: { user_id: 'invalid-user-id' }, // Missing 'role' property
|
||||
} as unknown as UserProfile, // Cast to UserProfile to satisfy req.user type, but it's intentionally malformed
|
||||
};
|
||||
});
|
||||
|
||||
// Act
|
||||
isAdmin(mockReq as Request, mockRes as Response, mockNext);
|
||||
@@ -601,7 +602,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should populate req.user and call next() if authentication succeeds', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
const mockUser = createMockUserProfile({
|
||||
role: 'admin',
|
||||
user: { user_id: 'admin-id', email: 'admin@test.com' },
|
||||
@@ -621,7 +622,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should not populate req.user and still call next() if authentication fails', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
vi.mocked(passport.authenticate).mockImplementation(
|
||||
(_strategy, _options, callback) => () => callback?.(null, false, undefined),
|
||||
);
|
||||
@@ -634,7 +635,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should log info and call next() if authentication provides an info message', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
const mockInfo = { message: 'Token expired' };
|
||||
// Mock passport.authenticate to call its callback with an info object
|
||||
vi.mocked(passport.authenticate).mockImplementation(
|
||||
@@ -652,7 +653,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should log info and call next() if authentication provides an info Error object', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
const mockInfoError = new Error('Token is malformed');
|
||||
// Mock passport.authenticate to call its callback with an info object
|
||||
vi.mocked(passport.authenticate).mockImplementation(
|
||||
@@ -673,7 +674,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should log info.toString() if info object has no message property', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
const mockInfo = { custom: 'some info' };
|
||||
// Mock passport.authenticate to call its callback with a custom info object
|
||||
vi.mocked(passport.authenticate).mockImplementation(
|
||||
@@ -693,7 +694,7 @@ describe('Passport Configuration', () => {
|
||||
|
||||
it('should call next() and not populate user if passport returns an error', () => {
|
||||
// Arrange
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
const authError = new Error('Malformed token');
|
||||
// Mock passport.authenticate to call its callback with an error
|
||||
vi.mocked(passport.authenticate).mockImplementation(
|
||||
@@ -729,7 +730,7 @@ describe('Passport Configuration', () => {
|
||||
it('should attach a mock admin user to req when NODE_ENV is "test"', () => {
|
||||
// Arrange
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
|
||||
// Act
|
||||
mockAuth(mockReq, mockRes as Response, mockNext);
|
||||
@@ -743,7 +744,7 @@ describe('Passport Configuration', () => {
|
||||
it('should do nothing and call next() when NODE_ENV is not "test"', () => {
|
||||
// Arrange
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
const mockReq = {} as Request;
|
||||
const mockReq = createMockRequest();
|
||||
|
||||
// Act
|
||||
mockAuth(mockReq, mockRes as Response, mockNext);
|
||||
|
||||
168
src/schemas/flyer.schemas.test.ts
Normal file
168
src/schemas/flyer.schemas.test.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
// src/schemas/flyer.schemas.test.ts
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { flyerInsertSchema, flyerDbInsertSchema } from './flyer.schemas';
|
||||
|
||||
describe('flyerInsertSchema', () => {
|
||||
const validFlyer = {
|
||||
file_name: 'flyer.jpg',
|
||||
image_url: 'https://example.com/flyer.jpg',
|
||||
icon_url: 'https://example.com/icon.jpg',
|
||||
checksum: 'a'.repeat(64),
|
||||
store_name: 'Test Store',
|
||||
valid_from: '2023-01-01T00:00:00Z',
|
||||
valid_to: '2023-01-07T00:00:00Z',
|
||||
store_address: '123 Main St',
|
||||
status: 'processed',
|
||||
item_count: 10,
|
||||
uploaded_by: '123e4567-e89b-12d3-a456-426614174000',
|
||||
};
|
||||
|
||||
it('should validate a correct flyer object', () => {
|
||||
const result = flyerInsertSchema.safeParse(validFlyer);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail if file_name is missing or empty', () => {
|
||||
const invalid = { ...validFlyer, file_name: '' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues[0].message).toBe('File name is required');
|
||||
}
|
||||
});
|
||||
|
||||
it('should fail if image_url is invalid', () => {
|
||||
const invalid = { ...validFlyer, image_url: 'ftp://invalid.com' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues[0].message).toBe(
|
||||
'Flyer image URL must be a valid HTTP or HTTPS URL',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('should fail if icon_url is invalid', () => {
|
||||
const invalid = { ...validFlyer, icon_url: 'not-a-url' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should fail if checksum length is incorrect', () => {
|
||||
const invalid = { ...validFlyer, checksum: 'abc' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues[0].message).toBe('Checksum must be 64 characters');
|
||||
}
|
||||
});
|
||||
|
||||
it('should fail if checksum is not hex', () => {
|
||||
const invalid = { ...validFlyer, checksum: 'z'.repeat(64) };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues[0].message).toBe('Checksum must be a valid hexadecimal string');
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow null checksum', () => {
|
||||
const valid = { ...validFlyer, checksum: null };
|
||||
const result = flyerInsertSchema.safeParse(valid);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail if store_name is missing', () => {
|
||||
const invalid = { ...validFlyer, store_name: '' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should validate valid_from and valid_to as datetimes', () => {
|
||||
const invalid = { ...validFlyer, valid_from: 'not-a-date' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should allow null valid_from, valid_to, store_address', () => {
|
||||
const valid = {
|
||||
...validFlyer,
|
||||
valid_from: null,
|
||||
valid_to: null,
|
||||
store_address: null,
|
||||
};
|
||||
const result = flyerInsertSchema.safeParse(valid);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should validate status enum', () => {
|
||||
const invalid = { ...validFlyer, status: 'invalid_status' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should fail if item_count is negative', () => {
|
||||
const invalid = { ...validFlyer, item_count: -1 };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues[0].message).toBe('Item count must be non-negative');
|
||||
}
|
||||
});
|
||||
|
||||
it('should validate uploaded_by as UUID if present', () => {
|
||||
const invalid = { ...validFlyer, uploaded_by: 'not-a-uuid' };
|
||||
const result = flyerInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should allow null or undefined uploaded_by', () => {
|
||||
const validNull = { ...validFlyer, uploaded_by: null };
|
||||
expect(flyerInsertSchema.safeParse(validNull).success).toBe(true);
|
||||
|
||||
const validUndefined = { ...validFlyer, uploaded_by: undefined };
|
||||
expect(flyerInsertSchema.safeParse(validUndefined).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flyerDbInsertSchema', () => {
|
||||
const validDbFlyer = {
|
||||
file_name: 'flyer.jpg',
|
||||
image_url: 'https://example.com/flyer.jpg',
|
||||
icon_url: 'https://example.com/icon.jpg',
|
||||
checksum: 'a'.repeat(64),
|
||||
store_id: 1,
|
||||
valid_from: '2023-01-01T00:00:00Z',
|
||||
valid_to: '2023-01-07T00:00:00Z',
|
||||
store_address: '123 Main St',
|
||||
status: 'processed',
|
||||
item_count: 10,
|
||||
uploaded_by: '123e4567-e89b-12d3-a456-426614174000',
|
||||
};
|
||||
|
||||
it('should validate a correct DB flyer object', () => {
|
||||
const result = flyerDbInsertSchema.safeParse(validDbFlyer);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should fail if store_id is missing', () => {
|
||||
const { store_id, ...invalid } = validDbFlyer;
|
||||
const result = flyerDbInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should fail if store_id is not positive', () => {
|
||||
const invalid = { ...validDbFlyer, store_id: 0 };
|
||||
const result = flyerDbInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues[0].message).toBe('Store ID must be a positive integer');
|
||||
}
|
||||
});
|
||||
|
||||
it('should fail if store_id is not an integer', () => {
|
||||
const invalid = { ...validDbFlyer, store_id: 1.5 };
|
||||
const result = flyerDbInsertSchema.safeParse(invalid);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -244,8 +244,9 @@ describe('Flyer DB Service', () => {
|
||||
await expect(flyerRepo.insertFlyer(flyerData, mockLogger)).rejects.toThrow(
|
||||
CheckConstraintError,
|
||||
);
|
||||
// The implementation now generates a more detailed error message.
|
||||
await expect(flyerRepo.insertFlyer(flyerData, mockLogger)).rejects.toThrow(
|
||||
'Invalid URL format provided for image or icon.',
|
||||
"[URL_CHECK_FAIL] Invalid URL format. Image: 'https://example.com/not-a-url', Icon: 'null'",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
// src/services/logger.server.test.ts
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
// Unmock the module we are testing to override the global mock from setupFiles.
|
||||
vi.unmock('./logger.server');
|
||||
|
||||
// Mock pino before importing the logger
|
||||
const pinoMock = vi.fn(() => ({
|
||||
info: vi.fn(),
|
||||
@@ -25,14 +28,25 @@ describe('Server Logger', () => {
|
||||
it('should initialize pino with the correct level for production', async () => {
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
await import('./logger.server');
|
||||
expect(pinoMock).toHaveBeenCalledWith(expect.objectContaining({ level: 'info' }));
|
||||
expect(pinoMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ level: 'info', transport: undefined }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should initialize pino with pretty-print transport for development', async () => {
|
||||
vi.stubEnv('NODE_ENV', 'development');
|
||||
await import('./logger.server');
|
||||
expect(pinoMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ transport: expect.any(Object) }),
|
||||
expect.objectContaining({ level: 'debug', transport: expect.any(Object) }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should initialize pino with debug level and no transport for test', async () => {
|
||||
// This is the default for vitest, but we stub it for clarity.
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
await import('./logger.server');
|
||||
expect(pinoMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ level: 'debug', transport: undefined }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -59,17 +59,40 @@ vi.mock('../../services/storage/storageService', () => {
|
||||
};
|
||||
});
|
||||
|
||||
// FIX: Import the singleton instance directly to spy on it
|
||||
import { aiService } from '../../services/aiService.server';
|
||||
|
||||
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
|
||||
const { mockExtractCoreData } = vi.hoisted(() => ({
|
||||
mockExtractCoreData: vi.fn(),
|
||||
}));
|
||||
// CRITICAL: This mock function must be declared with vi.hoisted() to ensure it's available
|
||||
// at the module level BEFORE any imports are resolved.
|
||||
const { mockExtractCoreData } = vi.hoisted(() => {
|
||||
return {
|
||||
mockExtractCoreData: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
// CRITICAL: Mock the aiService module BEFORE any other imports that depend on it.
|
||||
// This ensures workers get the mocked version, not the real one.
|
||||
// We use a partial mock that only overrides extractCoreDataFromFlyerImage.
|
||||
vi.mock('../../services/aiService.server', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../../services/aiService.server')>();
|
||||
|
||||
// Create a proxy around the actual aiService that intercepts extractCoreDataFromFlyerImage
|
||||
const proxiedAiService = new Proxy(actual.aiService, {
|
||||
get(target, prop) {
|
||||
if (prop === 'extractCoreDataFromFlyerImage') {
|
||||
return mockExtractCoreData;
|
||||
}
|
||||
// For all other properties/methods, return the original
|
||||
return target[prop as keyof typeof target];
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...actual,
|
||||
aiService: proxiedAiService,
|
||||
};
|
||||
});
|
||||
|
||||
// Mock the connection DB service to intercept withTransaction.
|
||||
// This is crucial because FlyerPersistenceService imports directly from connection.db,
|
||||
@@ -99,9 +122,8 @@ describe('Flyer Processing Background Job Integration Test', () => {
|
||||
process.env.FRONTEND_URL = 'https://example.com';
|
||||
console.error('[TEST SETUP] FRONTEND_URL stubbed to:', process.env.FRONTEND_URL);
|
||||
|
||||
// FIX: Spy on the actual singleton instance. This ensures that when the worker
|
||||
// imports 'aiService', it gets the instance we are controlling here.
|
||||
vi.spyOn(aiService, 'extractCoreDataFromFlyerImage').mockImplementation(mockExtractCoreData);
|
||||
// NOTE: The aiService mock is now set up via vi.mock() at the module level (above).
|
||||
// This ensures workers get the mocked version when they import aiService.
|
||||
|
||||
// NEW: Import workers to start them IN-PROCESS.
|
||||
// This ensures they run in the same memory space as our mocks.
|
||||
|
||||
8
src/tests/setup/global.ts
Normal file
8
src/tests/setup/global.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { vi } from 'vitest';
|
||||
import { mockLogger } from '../utils/mockLogger';
|
||||
|
||||
// Globally mock the logger service so individual test files don't have to.
|
||||
// This ensures 'import { logger } from ...' always returns the mock.
|
||||
vi.mock('../../services/logger.server', () => ({
|
||||
logger: mockLogger,
|
||||
}));
|
||||
@@ -9,6 +9,29 @@ let server: Server;
|
||||
// This will hold the single database pool instance for the entire test run.
|
||||
let globalPool: ReturnType<typeof getPool> | null = null;
|
||||
|
||||
/**
|
||||
* Cleans all BullMQ queues to ensure no stale jobs from previous test runs.
|
||||
* This is critical because old jobs with outdated error messages can pollute test results.
|
||||
*/
|
||||
async function cleanAllQueues() {
|
||||
console.log(`[PID:${process.pid}] Cleaning all BullMQ queues...`);
|
||||
const { flyerQueue, cleanupQueue, emailQueue, analyticsQueue, weeklyAnalyticsQueue, tokenCleanupQueue } = await import('../../services/queues.server');
|
||||
|
||||
const queues = [flyerQueue, cleanupQueue, emailQueue, analyticsQueue, weeklyAnalyticsQueue, tokenCleanupQueue];
|
||||
|
||||
for (const queue of queues) {
|
||||
try {
|
||||
// obliterate() removes ALL data associated with the queue from Redis
|
||||
await queue.obliterate({ force: true });
|
||||
console.log(` ✅ Cleaned queue: ${queue.name}`);
|
||||
} catch (error) {
|
||||
// Log but don't fail - the queue might not exist yet
|
||||
console.log(` ⚠️ Could not clean queue ${queue.name}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
console.log(`✅ [PID:${process.pid}] All queues cleaned.`);
|
||||
}
|
||||
|
||||
export async function setup() {
|
||||
// Ensure we are in the correct environment for these tests.
|
||||
process.env.NODE_ENV = 'test';
|
||||
@@ -17,6 +40,10 @@ export async function setup() {
|
||||
|
||||
console.log(`\n--- [PID:${process.pid}] Running Integration Test GLOBAL Setup ---`);
|
||||
|
||||
// CRITICAL: Clean all queues BEFORE running any tests to remove stale jobs
|
||||
// from previous test runs that may have outdated error messages.
|
||||
await cleanAllQueues();
|
||||
|
||||
// The integration setup is now the single source of truth for preparing the test DB.
|
||||
// It runs the same seed script that `npm run db:reset:test` used.
|
||||
try {
|
||||
|
||||
9
src/tests/utils/createMockRequest.ts
Normal file
9
src/tests/utils/createMockRequest.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Request } from 'express';
|
||||
import { mockLogger } from './mockLogger';
|
||||
|
||||
export const createMockRequest = (overrides: Partial<Request> = {}): Request => {
|
||||
return {
|
||||
log: mockLogger,
|
||||
...overrides,
|
||||
} as unknown as Request;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
// src/tests/utils/createTestApp.ts
|
||||
import express, { type Router } from 'express';
|
||||
import express, { type Router, type RequestHandler } from 'express';
|
||||
import type { Logger } from 'pino';
|
||||
import { errorHandler } from '../../middleware/errorHandler';
|
||||
import { mockLogger } from './mockLogger';
|
||||
@@ -17,6 +17,7 @@ interface CreateAppOptions {
|
||||
router: Router;
|
||||
basePath: string;
|
||||
authenticatedUser?: UserProfile;
|
||||
middleware?: RequestHandler[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,10 +25,20 @@ interface CreateAppOptions {
|
||||
* It includes JSON parsing, a mock logger, an optional authenticated user,
|
||||
* the specified router, and the global error handler.
|
||||
*/
|
||||
export const createTestApp = ({ router, basePath, authenticatedUser }: CreateAppOptions) => {
|
||||
export const createTestApp = ({
|
||||
router,
|
||||
basePath,
|
||||
authenticatedUser,
|
||||
middleware = [],
|
||||
}: CreateAppOptions) => {
|
||||
const app = express();
|
||||
app.use(express.json({ strict: false }));
|
||||
|
||||
// Apply custom middleware (e.g. cookieParser)
|
||||
if (middleware.length > 0) {
|
||||
app.use(middleware);
|
||||
}
|
||||
|
||||
// Inject the mock logger and authenticated user into every request.
|
||||
app.use((req, res, next) => {
|
||||
req.log = mockLogger;
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
// src/utils/imageProcessor.test.ts
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import type { Logger } from 'pino';
|
||||
import path from 'path';
|
||||
|
||||
// --- Hoisted Mocks ---
|
||||
const mocks = vi.hoisted(() => {
|
||||
// Create a chainable mock for the sharp library
|
||||
const toFile = vi.fn().mockResolvedValue({ info: 'mocked' });
|
||||
|
||||
// Chain for generateFlyerIcon
|
||||
const webp = vi.fn(() => ({ toFile }));
|
||||
const resize = vi.fn(() => ({ webp }));
|
||||
const sharpInstance = { resize };
|
||||
|
||||
// Chain for processAndSaveImage
|
||||
const png = vi.fn(() => ({ toFile }));
|
||||
const jpeg = vi.fn(() => ({ png }));
|
||||
const withMetadata = vi.fn(() => ({ jpeg }));
|
||||
|
||||
const sharpInstance = { resize, withMetadata };
|
||||
|
||||
// Mock the sharp function and attach static properties required by the implementation
|
||||
const sharp = vi.fn(() => sharpInstance);
|
||||
@@ -18,6 +27,9 @@ const mocks = vi.hoisted(() => {
|
||||
sharp: sharp,
|
||||
resize,
|
||||
webp,
|
||||
withMetadata,
|
||||
jpeg,
|
||||
png,
|
||||
toFile,
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
@@ -54,7 +66,7 @@ const logger = createMockLogger();
|
||||
vi.mock('../services/logger.server', () => ({ logger }));
|
||||
|
||||
// --- Import the function to be tested ---
|
||||
import { generateFlyerIcon } from './imageProcessor';
|
||||
import { generateFlyerIcon, processAndSaveImage } from './imageProcessor';
|
||||
|
||||
describe('generateFlyerIcon', () => {
|
||||
beforeEach(() => {
|
||||
@@ -95,3 +107,48 @@ describe('generateFlyerIcon', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('processAndSaveImage', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Ensure toFile is in a resolved state
|
||||
mocks.toFile.mockResolvedValue({ info: 'mocked' });
|
||||
});
|
||||
|
||||
it('should process the image, strip metadata, and return the new filename', async () => {
|
||||
const sourcePath = '/tmp/upload/original.jpg';
|
||||
const destinationDir = '/var/www/images';
|
||||
const originalFileName = 'original.jpg';
|
||||
|
||||
const result = await processAndSaveImage(sourcePath, destinationDir, originalFileName, logger);
|
||||
|
||||
// Check that the destination directory was created
|
||||
expect(mocks.mkdir).toHaveBeenCalledWith(destinationDir, { recursive: true });
|
||||
|
||||
// Check that sharp was called with the correct source
|
||||
expect(mocks.sharp).toHaveBeenCalledWith(sourcePath, { failOn: 'none' });
|
||||
|
||||
// Check the processing chain
|
||||
expect(mocks.withMetadata).toHaveBeenCalledWith({});
|
||||
expect(mocks.jpeg).toHaveBeenCalledWith({ quality: 85, mozjpeg: true });
|
||||
expect(mocks.png).toHaveBeenCalledWith({ compressionLevel: 8, quality: 85 });
|
||||
expect(mocks.toFile).toHaveBeenCalledWith(expect.stringContaining(path.join(destinationDir, 'original-')));
|
||||
|
||||
// Check the returned filename format (original-timestamp.jpg)
|
||||
expect(result).toMatch(/^original-\d+\.jpg$/);
|
||||
});
|
||||
|
||||
it('should throw an error if sharp fails to process the image', async () => {
|
||||
const sharpError = new Error('Processing failed');
|
||||
mocks.toFile.mockRejectedValueOnce(sharpError);
|
||||
|
||||
await expect(
|
||||
processAndSaveImage('/path/img.jpg', '/dest', 'img.jpg', logger),
|
||||
).rejects.toThrow('Failed to process image img.jpg.');
|
||||
|
||||
expect(logger.error).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ err: sharpError, sourcePath: '/path/img.jpg' }),
|
||||
'An error occurred during image processing and saving.',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import type { Request } from 'express';
|
||||
import { createMockRequest } from '../tests/utils/createMockRequest';
|
||||
|
||||
describe('rateLimit utils', () => {
|
||||
beforeEach(() => {
|
||||
@@ -16,7 +16,7 @@ describe('rateLimit utils', () => {
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
const { shouldSkipRateLimit } = await import('./rateLimit');
|
||||
|
||||
const req = { headers: {} } as Request;
|
||||
const req = createMockRequest({ headers: {} });
|
||||
expect(shouldSkipRateLimit(req)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -24,7 +24,7 @@ describe('rateLimit utils', () => {
|
||||
vi.stubEnv('NODE_ENV', 'development');
|
||||
const { shouldSkipRateLimit } = await import('./rateLimit');
|
||||
|
||||
const req = { headers: {} } as Request;
|
||||
const req = createMockRequest({ headers: {} });
|
||||
expect(shouldSkipRateLimit(req)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ describe('rateLimit utils', () => {
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
const { shouldSkipRateLimit } = await import('./rateLimit');
|
||||
|
||||
const req = { headers: {} } as Request;
|
||||
const req = createMockRequest({ headers: {} });
|
||||
expect(shouldSkipRateLimit(req)).toBe(true);
|
||||
});
|
||||
|
||||
@@ -40,9 +40,9 @@ describe('rateLimit utils', () => {
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
const { shouldSkipRateLimit } = await import('./rateLimit');
|
||||
|
||||
const req = {
|
||||
const req = createMockRequest({
|
||||
headers: { 'x-test-rate-limit-enable': 'true' },
|
||||
} as unknown as Request;
|
||||
});
|
||||
expect(shouldSkipRateLimit(req)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -50,9 +50,9 @@ describe('rateLimit utils', () => {
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
const { shouldSkipRateLimit } = await import('./rateLimit');
|
||||
|
||||
const req = {
|
||||
const req = createMockRequest({
|
||||
headers: { 'x-test-rate-limit-enable': 'false' },
|
||||
} as unknown as Request;
|
||||
});
|
||||
expect(shouldSkipRateLimit(req)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,57 +20,100 @@ const createMockLogger = (): Logger =>
|
||||
|
||||
describe('serverUtils', () => {
|
||||
describe('getBaseUrl', () => {
|
||||
const originalEnv = process.env;
|
||||
let mockLogger: Logger;
|
||||
|
||||
// Store original env values to restore after tests
|
||||
const originalFrontendUrl = process.env.FRONTEND_URL;
|
||||
const originalBaseUrl = process.env.BASE_URL;
|
||||
const originalNodeEnv = process.env.NODE_ENV;
|
||||
const originalPort = process.env.PORT;
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset mocks and environment variables before each test for isolation
|
||||
vi.resetModules();
|
||||
process.env = { ...originalEnv };
|
||||
vi.unstubAllEnvs();
|
||||
// CRITICAL: Clear env vars that might be set globally (e.g., from vitest config)
|
||||
// vi.unstubAllEnvs() only removes vars set via vi.stubEnv(), not direct assignments
|
||||
delete process.env.FRONTEND_URL;
|
||||
delete process.env.BASE_URL;
|
||||
delete process.env.NODE_ENV;
|
||||
delete process.env.PORT;
|
||||
mockLogger = createMockLogger();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Restore original environment variables after each test
|
||||
process.env = originalEnv;
|
||||
vi.unstubAllEnvs();
|
||||
// Restore original values
|
||||
if (originalFrontendUrl !== undefined) process.env.FRONTEND_URL = originalFrontendUrl;
|
||||
else delete process.env.FRONTEND_URL;
|
||||
if (originalBaseUrl !== undefined) process.env.BASE_URL = originalBaseUrl;
|
||||
else delete process.env.BASE_URL;
|
||||
if (originalNodeEnv !== undefined) process.env.NODE_ENV = originalNodeEnv;
|
||||
else delete process.env.NODE_ENV;
|
||||
if (originalPort !== undefined) process.env.PORT = originalPort;
|
||||
else delete process.env.PORT;
|
||||
});
|
||||
|
||||
it('should use FRONTEND_URL if it is a valid URL', () => {
|
||||
process.env.FRONTEND_URL = 'https://valid.example.com';
|
||||
vi.stubEnv('FRONTEND_URL', 'https://valid.example.com');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('https://valid.example.com');
|
||||
expect(mockLogger.warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should trim a trailing slash from FRONTEND_URL', () => {
|
||||
process.env.FRONTEND_URL = 'https://valid.example.com/';
|
||||
vi.stubEnv('FRONTEND_URL', 'https://valid.example.com/');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('https://valid.example.com');
|
||||
});
|
||||
|
||||
it('should use BASE_URL if FRONTEND_URL is not set', () => {
|
||||
delete process.env.FRONTEND_URL;
|
||||
process.env.BASE_URL = 'https://base.example.com';
|
||||
vi.stubEnv('BASE_URL', 'https://base.example.com');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('https://base.example.com');
|
||||
expect(mockLogger.warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fall back to example.com with default port 3000 if no URL is provided', () => {
|
||||
delete process.env.FRONTEND_URL;
|
||||
delete process.env.BASE_URL;
|
||||
delete process.env.PORT;
|
||||
it('should fall back to localhost with default port 3000 in test environment', () => {
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('https://example.com:3000');
|
||||
expect(baseUrl).toBe('http://localhost:3000');
|
||||
expect(mockLogger.warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should log a warning and fall back if FRONTEND_URL is invalid (does not start with http)', () => {
|
||||
process.env.FRONTEND_URL = 'invalid.url.com';
|
||||
it('should fall back to example.com in non-test environment', () => {
|
||||
vi.stubEnv('NODE_ENV', 'development');
|
||||
vi.stubEnv('PORT', '4000');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('https://example.com:3000');
|
||||
expect(baseUrl).toBe('http://example.com:4000');
|
||||
expect(mockLogger.warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should log a warning and fall back to localhost if FRONTEND_URL is invalid in test env', () => {
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
vi.stubEnv('FRONTEND_URL', 'invalid.url.com');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('http://localhost:3000');
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith(
|
||||
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to default local URL: https://example.com:3000",
|
||||
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to: http://localhost:3000",
|
||||
);
|
||||
});
|
||||
|
||||
it('should log a warning and fall back to example.com if FRONTEND_URL is invalid in non-test env', () => {
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
vi.stubEnv('FRONTEND_URL', 'invalid.url.com');
|
||||
const baseUrl = getBaseUrl(mockLogger);
|
||||
expect(baseUrl).toBe('http://example.com:3000');
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith(
|
||||
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to: http://example.com:3000",
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an error if the final URL is invalid', () => {
|
||||
vi.stubEnv('FRONTEND_URL', 'http:invalid');
|
||||
expect(() => getBaseUrl(mockLogger)).toThrow(
|
||||
`[getBaseUrl] Generated URL 'http:invalid' does not match required pattern (must start with http:// or https://)`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,6 +46,7 @@ export default defineConfig({
|
||||
globalSetup: './src/tests/setup/global-setup.ts',
|
||||
// The globalApiMock MUST come first to ensure it's applied before other mocks that might depend on it.
|
||||
setupFiles: [
|
||||
'./src/tests/setup/global.ts',
|
||||
'./src/tests/setup/globalApiMock.ts',
|
||||
'./src/tests/setup/tests-setup-unit.ts',
|
||||
],
|
||||
|
||||
@@ -53,6 +53,7 @@ const finalConfig = mergeConfig(
|
||||
},
|
||||
// This setup script starts the backend server before tests run.
|
||||
globalSetup: './src/tests/setup/integration-global-setup.ts',
|
||||
setupFiles: ['./src/tests/setup/global.ts'],
|
||||
// The default timeout is 5000ms (5 seconds)
|
||||
testTimeout: 60000, // Increased timeout for server startup and API calls, especially AI services.
|
||||
hookTimeout: 60000,
|
||||
|
||||
Reference in New Issue
Block a user