2025-12-25 16:09:51 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Tests for API Toast Helper Utility
|
|
|
|
|
|
* FE-TEST-004: Test API toast helper utility functions
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
|
|
import { InternalAxiosRequestConfig } from 'axios';
|
|
|
|
|
|
import {
|
|
|
|
|
|
withSuccessToast,
|
|
|
|
|
|
withoutErrorToast,
|
|
|
|
|
|
showSuccessToast,
|
|
|
|
|
|
showErrorToast,
|
|
|
|
|
|
showInfoToast,
|
|
|
|
|
|
showWarningToast,
|
|
|
|
|
|
} from './apiToastHelper';
|
|
|
|
|
|
|
|
|
|
|
|
// Mock react-hot-toast
|
|
|
|
|
|
vi.mock('react-hot-toast', () => ({
|
|
|
|
|
|
default: {
|
|
|
|
|
|
success: vi.fn(),
|
|
|
|
|
|
error: vi.fn(),
|
|
|
|
|
|
loading: vi.fn(),
|
|
|
|
|
|
},
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
import toast from 'react-hot-toast';
|
|
|
|
|
|
|
|
|
|
|
|
describe('apiToastHelper utilities', () => {
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('withSuccessToast', () => {
|
|
|
|
|
|
it('should add success toast flag to config', () => {
|
|
|
|
|
|
const config: InternalAxiosRequestConfig = {
|
|
|
|
|
|
url: '/api/test',
|
|
|
|
|
|
method: 'get',
|
|
|
|
|
|
};
|
|
|
|
|
|
const result = withSuccessToast(config);
|
|
|
|
|
|
expect((result as any)._showSuccessToast).toBe(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should add custom success message', () => {
|
|
|
|
|
|
const config: InternalAxiosRequestConfig = {
|
|
|
|
|
|
url: '/api/test',
|
|
|
|
|
|
method: 'get',
|
|
|
|
|
|
};
|
|
|
|
|
|
const result = withSuccessToast(config, 'Custom success');
|
|
|
|
|
|
expect((result as any)._successMessage).toBe('Custom success');
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('withoutErrorToast', () => {
|
|
|
|
|
|
it('should add disable toast flag to config', () => {
|
|
|
|
|
|
const config: InternalAxiosRequestConfig = {
|
|
|
|
|
|
url: '/api/test',
|
|
|
|
|
|
method: 'get',
|
|
|
|
|
|
};
|
|
|
|
|
|
const result = withoutErrorToast(config);
|
|
|
|
|
|
expect((result as any)._disableToast).toBe(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('showSuccessToast', () => {
|
|
|
|
|
|
it('should call toast.success', () => {
|
|
|
|
|
|
showSuccessToast('Success message');
|
2026-01-13 18:47:57 +00:00
|
|
|
|
expect(toast.success).toHaveBeenCalledWith('Success message', {
|
|
|
|
|
|
duration: undefined,
|
|
|
|
|
|
});
|
2025-12-25 16:09:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call toast.success with duration', () => {
|
|
|
|
|
|
showSuccessToast('Success message', 5000);
|
2026-01-13 18:47:57 +00:00
|
|
|
|
expect(toast.success).toHaveBeenCalledWith('Success message', {
|
|
|
|
|
|
duration: 5000,
|
|
|
|
|
|
});
|
2025-12-25 16:09:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('showErrorToast', () => {
|
|
|
|
|
|
it('should call toast.error', () => {
|
|
|
|
|
|
showErrorToast('Error message');
|
2026-01-13 18:47:57 +00:00
|
|
|
|
expect(toast.error).toHaveBeenCalledWith('Error message', {
|
|
|
|
|
|
duration: undefined,
|
|
|
|
|
|
});
|
2025-12-25 16:09:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call toast.error with duration', () => {
|
|
|
|
|
|
showErrorToast('Error message', 5000);
|
2026-01-13 18:47:57 +00:00
|
|
|
|
expect(toast.error).toHaveBeenCalledWith('Error message', {
|
|
|
|
|
|
duration: 5000,
|
|
|
|
|
|
});
|
2025-12-25 16:09:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('showInfoToast', () => {
|
|
|
|
|
|
it('should call toast with info icon', () => {
|
|
|
|
|
|
showInfoToast('Info message');
|
2026-01-13 18:47:57 +00:00
|
|
|
|
expect(toast).toHaveBeenCalledWith('Info message', {
|
|
|
|
|
|
duration: undefined,
|
|
|
|
|
|
icon: 'ℹ️',
|
|
|
|
|
|
});
|
2025-12-25 16:09:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('showWarningToast', () => {
|
|
|
|
|
|
it('should call toast with warning icon', () => {
|
|
|
|
|
|
showWarningToast('Warning message');
|
2026-01-13 18:47:57 +00:00
|
|
|
|
expect(toast).toHaveBeenCalledWith('Warning message', {
|
|
|
|
|
|
duration: undefined,
|
|
|
|
|
|
icon: '⚠️',
|
|
|
|
|
|
});
|
2025-12-25 16:09:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|