Skip to content

Commit af39987

Browse files
committed
migrate errorHandler tests from Jest to Vitest #2758
1 parent 13f16b9 commit af39987

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed

src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('Testing Organization Dashboard Screen', () => {
147147
vi.mocked(useParams).mockReturnValue({ orgId: 'orgId' });
148148
renderOrganizationDashboard(link1);
149149

150-
// Wait for initial load
150+
// First wait for the dashboard to fully load
151151
await waitFor(() => {
152152
expect(screen.getByText(t.upcomingEvents)).toBeInTheDocument();
153153
});
@@ -160,12 +160,8 @@ describe('Testing Organization Dashboard Screen', () => {
160160
expect(screen.getByText(t.events)).toBeInTheDocument();
161161
expect(screen.getByText(t.blockedUsers)).toBeInTheDocument();
162162

163-
// Upcoming events - Using more flexible matcher
164-
await waitFor(() => {
165-
expect(
166-
screen.getByText(/Event 1/i, { exact: false }),
167-
).toBeInTheDocument();
168-
});
163+
// Upcoming events - Use a more flexible matcher
164+
expect(screen.getByText(/Event 1/i, { exact: false })).toBeInTheDocument();
169165

170166
// Latest posts
171167
expect(screen.getByText(t.latestPosts)).toBeInTheDocument();

src/utils/errorHandler.spec.tsx

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
type TFunction = (key: string, options?: Record<string, unknown>) => string;
2+
3+
import { errorHandler } from './errorHandler';
4+
import { toast } from 'react-toastify';
5+
import { describe, it, expect, vi } from 'vitest';
6+
7+
vi.mock('react-toastify', () => ({
8+
toast: {
9+
error: vi.fn(),
10+
},
11+
}));
12+
13+
describe('Test if errorHandler is working properly', () => {
14+
const t: TFunction = (key: string) => key;
15+
const tErrors: TFunction = (
16+
key: string,
17+
options?: Record<string, unknown>,
18+
) => {
19+
if (options) {
20+
console.log(`options are passed, but the function returns only ${key}`);
21+
}
22+
return key;
23+
};
24+
25+
beforeEach(() => {
26+
vi.clearAllMocks();
27+
});
28+
29+
it('should call toast.error with the correct message if error message is "Failed to fetch"', async () => {
30+
const error = new Error('Failed to fetch');
31+
errorHandler(t, error);
32+
33+
expect(toast.error).toHaveBeenCalledWith(tErrors('talawaApiUnavailable'));
34+
});
35+
36+
it('should call toast.error with the correct message if error message contains this substring "Value is not a valid phone number"', () => {
37+
const error = new Error('This value is not a valid phone number');
38+
errorHandler(t, error);
39+
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber'));
40+
});
41+
42+
test.each([
43+
['EducationGrade', 'invalidEducationGrade'],
44+
['EmploymentStatus', 'invalidEmploymentStatus'],
45+
['MaritalStatus', 'invalidMaritalStatus'],
46+
])('should handle invalid %s error', (field, expectedKey) => {
47+
const error = new Error(`This value does not exist in "${field}"`);
48+
errorHandler(t, error);
49+
expect(toast.error).toHaveBeenCalledWith(tErrors(expectedKey));
50+
});
51+
52+
it('should call toast.error with the correct message if error message contains this substring "status code 400"', () => {
53+
const error = new Error('Server responded with status code 400');
54+
errorHandler(t, error);
55+
56+
expect(toast.error).toHaveBeenCalledWith(tErrors('error400'));
57+
});
58+
59+
it('should handle error messages with different cases', () => {
60+
errorHandler(t, new Error('VALUE IS NOT A VALID PHONE NUMBER'));
61+
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidPhoneNumber'));
62+
63+
errorHandler(t, new Error('This Value Does Not Exist in "EducationGrade"'));
64+
expect(toast.error).toHaveBeenCalledWith(tErrors('invalidEducationGrade'));
65+
});
66+
67+
it('should call toast.error with the error message if it is an instance of error but have not matched any error message patterns', () => {
68+
const error = new Error('Bandhan sent an error message');
69+
errorHandler(t, error);
70+
expect(toast.error).toHaveBeenCalledWith(error.message);
71+
});
72+
73+
it('should handle different types for the first parameter while still showing error messages', () => {
74+
errorHandler(undefined, new Error('Some error'));
75+
expect(toast.error).toHaveBeenCalled();
76+
77+
errorHandler(null, new Error('Some error'));
78+
expect(toast.error).toHaveBeenCalled();
79+
80+
errorHandler({}, new Error('Some error'));
81+
expect(toast.error).toHaveBeenCalled();
82+
});
83+
84+
it('should handle non-null but non-Error objects for the error parameter', () => {
85+
errorHandler(t, { message: 'Error message in object' });
86+
expect(toast.error).toHaveBeenCalledWith(
87+
tErrors('unknownError', { msg: { message: 'Error message in object' } }),
88+
);
89+
90+
errorHandler(t, 'Direct error message');
91+
expect(toast.error).toHaveBeenCalledWith(
92+
tErrors('unknownError', { msg: 'Direct error message' }),
93+
);
94+
});
95+
96+
it('should call toast.error with the error message if error object is falsy', () => {
97+
const error = null;
98+
errorHandler(t, error);
99+
100+
expect(toast.error).toHaveBeenCalledWith(
101+
tErrors('unknownError', { msg: error }),
102+
);
103+
});
104+
});

0 commit comments

Comments
 (0)