brand new unit tests finally
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 2m15s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 2m15s
This commit is contained in:
@@ -29,12 +29,16 @@ describe('VoiceLabPage', () => {
|
||||
});
|
||||
|
||||
// Mock the global Audio constructor
|
||||
const AudioMock = vi.fn().mockImplementation(function (this: any, url: string) {
|
||||
const AudioMock = vi.fn().mockImplementation((url) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[TEST DEBUG] New Audio instance created with URL:', url);
|
||||
// Ensure the play mock is connected
|
||||
return {
|
||||
play: mockAudioPlay,
|
||||
}
|
||||
play: () => {
|
||||
console.log('[TEST DEBUG] Audio.play() invoked on instance');
|
||||
return mockAudioPlay();
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.stubGlobal('Audio', AudioMock);
|
||||
|
||||
@@ -34,6 +34,8 @@ vi.mock('./logger.server', () => ({
|
||||
|
||||
describe('Email Service (Server)', () => {
|
||||
beforeEach(async () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[TEST SETUP] Setting up Email Service mocks');
|
||||
vi.clearAllMocks();
|
||||
// Reset to default successful implementation
|
||||
mockSendMail.mockImplementation((mailOptions) => {
|
||||
|
||||
@@ -1,13 +1,24 @@
|
||||
// src/services/notificationService.test.ts
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import toast from 'react-hot-toast'; // Import the mocked module
|
||||
import { notifySuccess, notifyError } from './notificationService';
|
||||
|
||||
// Use vi.hoisted to ensure we share the exact same mock references between the factory and the test
|
||||
const mocks = vi.hoisted(() => {
|
||||
return {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
toastFn: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
// Mock react-hot-toast
|
||||
vi.mock('react-hot-toast', () => {
|
||||
const toastMock = vi.fn() as any;
|
||||
toastMock.success = vi.fn();
|
||||
toastMock.error = vi.fn();
|
||||
const toastMock = mocks.toastFn;
|
||||
// Attach the spies to the default function
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(toastMock as any).success = mocks.success;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(toastMock as any).error = mocks.error;
|
||||
|
||||
return {
|
||||
default: toastMock,
|
||||
@@ -17,24 +28,28 @@ vi.mock('react-hot-toast', () => {
|
||||
|
||||
describe('notificationService', () => {
|
||||
beforeEach(() => {
|
||||
// Clear mock history before each test to ensure isolation.
|
||||
// vi.clearAllMocks(); // This is often too broad. Resetting specific mocks is safer.
|
||||
vi.mocked(toast.success).mockClear();
|
||||
vi.mocked(toast.error).mockClear();
|
||||
vi.clearAllMocks();
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[TEST SETUP] Mocks cleared');
|
||||
// Implement console logs for the mocks to verify calls
|
||||
mocks.success.mockImplementation((msg) => console.log('[MOCK CALL] toast.success:', msg));
|
||||
mocks.error.mockImplementation((msg) => console.log('[MOCK CALL] toast.error:', msg));
|
||||
});
|
||||
|
||||
describe('notifySuccess', () => {
|
||||
it('should call toast.success with the correct message and options', () => {
|
||||
const message = 'Operation completed successfully!';
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[TEST] calling notifySuccess');
|
||||
notifySuccess(message);
|
||||
|
||||
expect(toast.success).toHaveBeenCalledTimes(1);
|
||||
expect(toast.success).toHaveBeenCalledWith(
|
||||
expect(mocks.success).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.success).toHaveBeenCalledWith(
|
||||
message,
|
||||
expect.objectContaining({
|
||||
style: expect.any(Object), // Check that common styles are passed
|
||||
style: expect.any(Object),
|
||||
iconTheme: {
|
||||
primary: '#10B981', // Check for the specific success color
|
||||
primary: '#10B981',
|
||||
secondary: '#fff',
|
||||
},
|
||||
})
|
||||
@@ -45,15 +60,17 @@ describe('notificationService', () => {
|
||||
describe('notifyError', () => {
|
||||
it('should call toast.error with the correct message and options', () => {
|
||||
const message = 'An unexpected error occurred.';
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[TEST] calling notifyError');
|
||||
notifyError(message);
|
||||
|
||||
expect(toast.error).toHaveBeenCalledTimes(1);
|
||||
expect(toast.error).toHaveBeenCalledWith(
|
||||
expect(mocks.error).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.error).toHaveBeenCalledWith(
|
||||
message,
|
||||
expect.objectContaining({
|
||||
style: expect.any(Object), // Check that common styles are passed
|
||||
style: expect.any(Object),
|
||||
iconTheme: {
|
||||
primary: '#EF4444', // Check for the specific error color
|
||||
primary: '#EF4444',
|
||||
secondary: '#fff',
|
||||
},
|
||||
})
|
||||
|
||||
@@ -20,6 +20,11 @@ const commonToastOptions: ToastOptions = {
|
||||
* @param message The message to display.
|
||||
*/
|
||||
export const notifySuccess = (message: string) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[NotificationService] notifySuccess called with:', message);
|
||||
if (!toast) console.error('[NotificationService] Error: toast object is undefined');
|
||||
else if (!toast.success) console.error('[NotificationService] Error: toast.success is undefined');
|
||||
|
||||
// We merge the common options with the specific icon theme for success toasts.
|
||||
toast.success(message, {
|
||||
...commonToastOptions,
|
||||
@@ -35,6 +40,11 @@ export const notifySuccess = (message: string) => {
|
||||
* @param message The message to display.
|
||||
*/
|
||||
export const notifyError = (message: string) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[NotificationService] notifyError called with:', message);
|
||||
if (!toast) console.error('[NotificationService] Error: toast object is undefined');
|
||||
else if (!toast.error) console.error('[NotificationService] Error: toast.error is undefined');
|
||||
|
||||
// We merge the common options with the specific icon theme for error toasts.
|
||||
toast.error(message, {
|
||||
...commonToastOptions,
|
||||
|
||||
Reference in New Issue
Block a user