veza/apps/web/src/utils/apiToastHelper.test.ts

103 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* 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');
expect(toast.success).toHaveBeenCalledWith('Success message', { duration: undefined });
});
it('should call toast.success with duration', () => {
showSuccessToast('Success message', 5000);
expect(toast.success).toHaveBeenCalledWith('Success message', { duration: 5000 });
});
});
describe('showErrorToast', () => {
it('should call toast.error', () => {
showErrorToast('Error message');
expect(toast.error).toHaveBeenCalledWith('Error message', { duration: undefined });
});
it('should call toast.error with duration', () => {
showErrorToast('Error message', 5000);
expect(toast.error).toHaveBeenCalledWith('Error message', { duration: 5000 });
});
});
describe('showInfoToast', () => {
it('should call toast with info icon', () => {
showInfoToast('Info message');
expect(toast).toHaveBeenCalledWith('Info message', { duration: undefined, icon: '' });
});
});
describe('showWarningToast', () => {
it('should call toast with warning icon', () => {
showWarningToast('Warning message');
expect(toast).toHaveBeenCalledWith('Warning message', { duration: undefined, icon: '⚠️' });
});
});
});