From 8a6a6712e7554f110b5ef951f270d88fd010e040 Mon Sep 17 00:00:00 2001 From: Bertrand Yuan Date: Thu, 26 Mar 2026 00:02:16 +0800 Subject: add more tests --- src/lib/safe-action.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/lib/safe-action.test.ts (limited to 'src/lib/safe-action.test.ts') diff --git a/src/lib/safe-action.test.ts b/src/lib/safe-action.test.ts new file mode 100644 index 0000000..eee5679 --- /dev/null +++ b/src/lib/safe-action.test.ts @@ -0,0 +1,42 @@ +import { afterEach, describe, expect, test, vi } from 'vitest'; + +vi.mock('next-safe-action', () => ({ + DEFAULT_SERVER_ERROR_MESSAGE: 'Internal server error', + createSafeActionClient: vi.fn((options: unknown) => options), +})); + +describe('safe-action', () => { + afterEach(() => { + vi.restoreAllMocks(); + vi.resetModules(); + }); + + test('returns custom message for ActionError', async () => { + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const { ActionError, actionClient } = await import('./safe-action'); + const client = actionClient as unknown as { + handleServerError: (e: Error) => string; + }; + + const message = client.handleServerError(new ActionError('Custom error')); + + expect(message).toBe('Custom error'); + expect(errorSpy).toHaveBeenCalledWith( + 'Failed to execute action:', + 'Custom error', + ); + }); + + test('returns default message for unknown errors', async () => { + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + const { actionClient } = await import('./safe-action'); + const client = actionClient as unknown as { + handleServerError: (e: Error) => string; + }; + + const message = client.handleServerError(new Error('Unexpected')); + + expect(message).toBe('Internal server error'); + expect(errorSpy).toHaveBeenCalledWith('Failed to execute action:', 'Unexpected'); + }); +}); -- cgit v1.2.3