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

119 lines
3.2 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 @/utils/toast (lazy wrapper around react-hot-toast)
// vi.mock is hoisted — factory must NOT reference outer variables
vi.mock('@/utils/toast', () => {
const fn = Object.assign(vi.fn(), {
success: vi.fn(),
error: vi.fn(),
loading: vi.fn(),
});
return { default: fn };
});
// Import the mocked module to get reference for assertions
import toast from '@/utils/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: '⚠️',
});
});
});
});