Skip to content

Commit daa1251

Browse files
fix: lint and tests fix
1 parent 6ae166e commit daa1251

14 files changed

Lines changed: 160 additions & 80 deletions

File tree

src/common-components/tests/FormField.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import FormGroup from '../FormGroup';
1212
import PasswordField from '../PasswordField';
1313

1414
// Mock the useFieldValidations hook
15-
jest.mock('../../register/data/api.hook', () => ({
15+
jest.mock('../../register/data/apiHook', () => ({
1616
useFieldValidations: jest.fn(),
1717
}));
1818

src/login/data/apiHook.test.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,18 @@ describe('useLogin', () => {
9595
password: 'password123',
9696
};
9797
const mockErrorResponse = {
98-
email_or_username: ['This field is required'],
99-
password: ['Password is too weak'],
98+
errorCode: INVALID_FORM,
99+
context: {
100+
email_or_username: ['This field is required'],
101+
password: ['Password is too weak'],
102+
},
100103
};
101104
const mockCamelCasedResponse = {
102-
emailOrUsername: ['This field is required'],
103-
password: ['Password is too weak'],
105+
errorCode: INVALID_FORM,
106+
context: {
107+
emailOrUsername: ['This field is required'],
108+
password: ['Password is too weak'],
109+
},
104110
};
105111

106112
const mockError = {
@@ -110,10 +116,16 @@ describe('useLogin', () => {
110116
},
111117
};
112118

119+
// Mock onError callback to test formatted error
120+
const mockOnError = jest.fn();
121+
113122
mockLogin.mockRejectedValueOnce(mockError);
114-
mockCamelCaseObject.mockReturnValueOnce(mockCamelCasedResponse);
123+
mockCamelCaseObject.mockReturnValueOnce({
124+
status: 400,
125+
data: mockCamelCasedResponse,
126+
});
115127

116-
const { result } = renderHook(() => useLogin(), {
128+
const { result } = renderHook(() => useLogin({ onError: mockOnError }), {
117129
wrapper: createWrapper(),
118130
});
119131

@@ -124,11 +136,18 @@ describe('useLogin', () => {
124136
});
125137

126138
expect(mockLogin).toHaveBeenCalledWith(mockLoginData);
127-
expect(mockCamelCaseObject).toHaveBeenCalledWith(mockErrorResponse);
139+
expect(mockCamelCaseObject).toHaveBeenCalledWith({
140+
status: 400,
141+
data: mockErrorResponse,
142+
});
128143
expect(mockLogInfo).toHaveBeenCalledWith('Login failed with validation error', mockError);
129-
expect(result.current.error).toEqual({
130-
errorCode: INVALID_FORM,
131-
context: mockCamelCasedResponse,
144+
expect(mockOnError).toHaveBeenCalledWith({
145+
type: INVALID_FORM,
146+
context: {
147+
emailOrUsername: ['This field is required'],
148+
password: ['Password is too weak'],
149+
},
150+
count: 0,
132151
});
133152
});
134153

@@ -141,9 +160,12 @@ describe('useLogin', () => {
141160
const timeoutError = new Error('Request timeout');
142161
timeoutError.name = 'TimeoutError';
143162

163+
// Mock onError callback to test formatted error
164+
const mockOnError = jest.fn();
165+
144166
mockLogin.mockRejectedValueOnce(timeoutError);
145167

146-
const { result } = renderHook(() => useLogin(), {
168+
const { result } = renderHook(() => useLogin({ onError: mockOnError }), {
147169
wrapper: createWrapper(),
148170
});
149171

@@ -153,10 +175,11 @@ describe('useLogin', () => {
153175
expect(result.current.isError).toBe(true);
154176
});
155177

156-
expect(mockLogError).toHaveBeenCalledWith('Login failed with network error', timeoutError);
157-
expect(result.current.error).toEqual({
158-
errorCode: INTERNAL_SERVER_ERROR,
178+
expect(mockLogError).toHaveBeenCalledWith('Login failed', timeoutError);
179+
expect(mockOnError).toHaveBeenCalledWith({
180+
type: INTERNAL_SERVER_ERROR,
159181
context: {},
182+
count: 0,
160183
});
161184
});
162185

src/login/tests/LoginPage.test.jsx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,43 @@ describe('LoginPage', () => {
7979

8080
// Mock the login hook
8181
mockLoginMutate = jest.fn();
82-
useLogin.mockReturnValue({
82+
mockLoginMutate.mockRejected = false; // Reset flag
83+
const loginMutation = {
8384
mutate: mockLoginMutate,
8485
isPending: false,
85-
});
86+
};
87+
useLogin.mockImplementation((options) => ({
88+
...loginMutation,
89+
mutate: jest.fn().mockImplementation((data) => {
90+
// Call the mocked function for testing assertions
91+
mockLoginMutate(data);
92+
// Simulate can call success or error based on test needs
93+
if (options?.onSuccess && !mockLoginMutate.mockRejected) {
94+
options.onSuccess({ redirectUrl: 'https://test.com/dashboard' });
95+
}
96+
}),
97+
}));
8698

8799
// Mock the third party auth hook
88100
mockThirdPartyAuthMutate = jest.fn();
89-
useThirdPartyAuthHook.mockReturnValue({
101+
const thirdPartyAuthMutation = {
90102
mutate: mockThirdPartyAuthMutate,
91103
isPending: false,
92-
});
104+
};
105+
useThirdPartyAuthHook.mockImplementation((options) => ({
106+
...thirdPartyAuthMutation,
107+
mutate: jest.fn().mockImplementation((data) => {
108+
mockThirdPartyAuthMutate(data);
109+
if (options?.onSuccess) {
110+
// Simulate successful third party auth response
111+
options.onSuccess({
112+
thirdPartyAuthContext: {},
113+
fieldDescriptions: {},
114+
optionalFields: { fields: {}, extended_profile: [] },
115+
});
116+
}
117+
}),
118+
}));
93119

94120
// Mock the third party auth context
95121
mockThirdPartyAuthContext = {
@@ -128,7 +154,7 @@ describe('LoginPage', () => {
128154

129155
fireEvent.click(screen.getByRole('button', { name: /sign in/i }));
130156

131-
expect(mockLoginMutate).toHaveBeenCalledWith({ email_or_username: 'test', password: 'test-password' }, expect.any(Object));
157+
expect(mockLoginMutate).toHaveBeenCalledWith({ email_or_username: 'test', password: 'test-password' });
132158
});
133159

134160
it('should not call login mutation on empty form submission', () => {
@@ -373,10 +399,17 @@ describe('LoginPage', () => {
373399
// Login error handling is now managed by React Query hooks and context
374400
// We'll test that error messages appear when login fails
375401
it('should show error message when login fails', async () => {
376-
// Mock the login hook to return an error
377-
mockLoginMutate.mockImplementation((payload, { onError }) => {
378-
onError({ errorCode: INTERNAL_SERVER_ERROR, context: {} });
379-
});
402+
// Mock the login hook to simulate error
403+
mockLoginMutate.mockRejected = true;
404+
useLogin.mockImplementation((options) => ({
405+
mutate: jest.fn().mockImplementation((data) => {
406+
mockLoginMutate(data);
407+
if (options?.onError) {
408+
options.onError({ type: INTERNAL_SERVER_ERROR, context: {}, count: 0 });
409+
}
410+
}),
411+
isPending: false,
412+
}));
380413

381414
useLogin.mockReturnValue({
382415
mutate: mockLoginMutate,
@@ -449,9 +482,15 @@ describe('LoginPage', () => {
449482
// Login success and redirection is now handled by React Query hooks
450483
it('should handle successful login', () => {
451484
// Mock successful login
452-
mockLoginMutate.mockImplementation((payload, { onSuccess }) => {
453-
onSuccess({ success: true, redirectUrl: 'https://test.com/testing-dashboard/' });
454-
});
485+
useLogin.mockImplementation((options) => ({
486+
mutate: jest.fn().mockImplementation((data) => {
487+
mockLoginMutate(data);
488+
if (options?.onSuccess) {
489+
options.onSuccess({ success: true, redirectUrl: 'https://test.com/testing-dashboard/' });
490+
}
491+
}),
492+
isPending: false,
493+
}));
455494

456495
useLogin.mockReturnValue({
457496
mutate: mockLoginMutate,

src/progressive-profiling/data/apiHook.test.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ const createWrapper = () => {
3636
};
3737

3838
describe('useSaveUserProfile', () => {
39-
const mockSetLoading = jest.fn();
4039
const mockSetShowError = jest.fn();
4140
const mockSetSuccess = jest.fn();
4241
const mockSetSubmitState = jest.fn();
4342

4443
const mockContextValue = {
45-
setLoading: mockSetLoading,
4644
setShowError: mockSetShowError,
4745
setSuccess: mockSetSuccess,
4846
setSubmitState: mockSetSubmitState,
@@ -88,17 +86,12 @@ describe('useSaveUserProfile', () => {
8886
expect(result.current.isSuccess).toBe(true);
8987
});
9088

91-
// Check loading state was set during mutation
92-
expect(mockSetLoading).toHaveBeenCalledWith(true);
93-
9489
// Check API was called correctly
9590
expect(mockPatchAccount).toHaveBeenCalledWith(mockPayload.username, mockPayload.data);
9691

9792
// Check success state is set
98-
expect(mockSetLoading).toHaveBeenCalledWith(false);
9993
expect(mockSetSuccess).toHaveBeenCalledWith(true);
10094
expect(mockSetSubmitState).toHaveBeenCalledWith(COMPLETE_STATE);
101-
expect(result.current.data).toEqual(mockResponse);
10295
});
10396

10497
it('should handle API error and set error state', async () => {
@@ -120,14 +113,10 @@ describe('useSaveUserProfile', () => {
120113
expect(result.current.isError).toBe(true);
121114
});
122115

123-
// Check loading state was set during mutation
124-
expect(mockSetLoading).toHaveBeenCalledWith(true);
125-
126116
// Check API was called
127117
expect(mockPatchAccount).toHaveBeenCalledWith(mockPayload.username, mockPayload.data);
128118

129119
// Check error state is set
130-
expect(mockSetLoading).toHaveBeenCalledWith(false);
131120
expect(mockSetSubmitState).toHaveBeenCalledWith(DEFAULT_STATE);
132121
expect(result.current.error).toEqual(mockError);
133122
});
@@ -152,7 +141,6 @@ describe('useSaveUserProfile', () => {
152141
});
153142

154143
// Check error state is set
155-
expect(mockSetLoading).toHaveBeenCalledWith(false);
156144
expect(mockSetSubmitState).toHaveBeenCalledWith(DEFAULT_STATE);
157145
});
158146

@@ -183,7 +171,6 @@ describe('useSaveUserProfile', () => {
183171

184172
expect(mockPatchAccount).toHaveBeenCalledWith(mockPayload.username, mockPayload.data);
185173
expect(mockSetSuccess).toHaveBeenCalledWith(true);
186-
expect(result.current.data).toEqual(mockResponse);
187174
});
188175

189176
it('should handle network errors gracefully', async () => {
@@ -206,7 +193,6 @@ describe('useSaveUserProfile', () => {
206193
expect(result.current.isError).toBe(true);
207194
});
208195

209-
expect(mockSetLoading).toHaveBeenCalledWith(false);
210196
expect(mockSetSubmitState).toHaveBeenCalledWith(DEFAULT_STATE);
211197
});
212198

@@ -229,7 +215,7 @@ describe('useSaveUserProfile', () => {
229215
expect(result.current.isSuccess).toBe(true);
230216
});
231217

232-
expect(mockSetLoading).toHaveBeenCalledWith(true);
218+
expect(mockSetSuccess).toHaveBeenCalledWith(true);
233219

234220
jest.clearAllMocks();
235221
mockPatchAccount.mockResolvedValueOnce({ success: true });
@@ -241,9 +227,6 @@ describe('useSaveUserProfile', () => {
241227
expect(result.current.isSuccess).toBe(true);
242228
});
243229

244-
expect(mockSetLoading).toHaveBeenCalledWith(true);
245-
246-
expect(mockSetLoading).toHaveBeenCalledWith(false);
247230
expect(mockSetSuccess).toHaveBeenCalledWith(true);
248231
});
249232
});

0 commit comments

Comments
 (0)