veza/apps/web/src/utils/apiToastHelper.test.ts
senke b48eee1270 [FE-TEST-004] test: Add unit tests for utilities
- Created comprehensive unit tests for date utilities
- Created comprehensive unit tests for format utilities
- Created comprehensive unit tests for URL utilities
- Created comprehensive unit tests for logger utility
- Created comprehensive unit tests for errorMessages utility
- Created comprehensive unit tests for sanitize utility
- Created comprehensive unit tests for apiErrorHandler utility
- Created comprehensive unit tests for apiToastHelper utility
- Created comprehensive unit tests for serviceErrorHandler utility
- Created comprehensive unit tests for timeoutHandler utility

All tests pass (163 tests). Covers all utility functions that were missing tests.

Phase: PHASE-5
Priority: P2
Progress: 241/267 (90.26%)
2025-12-25 17:09:51 +01:00

102 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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: '⚠️' });
});
});
});