Skip to content

Commit 0020719

Browse files
committed
useractions unit test - user data actions - issue #106
1 parent 7fa43a8 commit 0020719

File tree

1 file changed

+129
-5
lines changed

1 file changed

+129
-5
lines changed

SmartClothingApp/__test__/userActions.unit.test.js

+129-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { auth } from '../firebaseConfig.js';
22
import configureMockStore from 'redux-mock-store';
33
import thunk from 'redux-thunk';
4+
import { setDoc, getDoc, doc } from 'firebase/firestore';
45
import { firebaseErrorsMessages } from '../src/utils/firebaseErrorsMessages.js';
56
import { render, waitFor } from "@testing-library/react"
67
import flushPromises from 'flush-promises';
@@ -279,28 +280,151 @@ describe('Async User Actions', () => {
279280
});
280281
});
281282

283+
284+
285+
282286
// ### User Data Actions
283287

284288
describe('User Data Actions', () => {
285289
it('should call setDoc and dispatch UPDATE_USER_METRICS_DATA on successful user data update', async () => {
290+
const store = mockStore({});
291+
const userData = { metrics: 'some metrics data' };
286292

293+
// Mock the doc and setDoc functions
294+
const mockDoc = jest.fn();
295+
doc.mockReturnValue(mockDoc);
296+
setDoc.mockResolvedValue();
297+
298+
await store.dispatch(startUpdateUserData(userData));
299+
300+
// Wait for all promises to resolve
301+
await flushPromises();
302+
303+
const actions = store.getActions();
304+
305+
expect(setDoc).toHaveBeenCalledWith(
306+
expect.anything(),
307+
userData
308+
);
309+
expect(storeMetrics).toHaveBeenCalledWith(userData);
310+
expect(actions[0]).toEqual({
311+
type: UPDATE_USER_METRICS_DATA,
312+
payload: userData
313+
});
287314
});
288315

289-
it('should store user metrics data in local storage on successful user data update', async () => {});
316+
it('should store user metrics data in local storage on successful user data update', async () => {
317+
const store = mockStore({});
318+
const userData = { metrics: 'some metrics data' };
319+
320+
// Mock the doc and setDoc functions
321+
const mockDoc = jest.fn();
322+
doc.mockReturnValue(mockDoc);
323+
setDoc.mockResolvedValue();
324+
325+
await store.dispatch(startUpdateUserData(userData));
326+
327+
// Wait for all promises to resolve
328+
await flushPromises();
329+
330+
expect(storeMetrics).toHaveBeenCalledWith(userData);
331+
});
290332

291333
it('should log error message on user data update failure', async () => {
334+
const store = mockStore({});
335+
const userData = { metrics: 'some metrics data' };
336+
const error = new Error('Failed to update user data');
292337

338+
// Mock the doc and setDoc functions
339+
const mockDoc = jest.fn();
340+
doc.mockReturnValue(mockDoc);
341+
setDoc.mockRejectedValue(error);
342+
343+
await store.dispatch(startUpdateUserData(userData));
344+
345+
// Wait for all promises to resolve
346+
await flushPromises();
347+
348+
const actions = store.getActions();
349+
350+
expect(setDoc).toHaveBeenCalledWith(
351+
expect.anything(),
352+
userData
353+
);
354+
expect(storeMetrics).not.toHaveBeenCalled();
355+
expect(actions).toEqual([]);
293356
});
294357

295-
it('should call getDoc and dispatch UPDATE_USER_METRICS_DATA if user data exists', async () => {});
358+
it('should call getDoc and dispatch UPDATE_USER_METRICS_DATA if user data exists', async () => {
359+
const store = mockStore({});
360+
const userData = { metrics: 'some metrics data' };
361+
362+
// Mock the doc and getDoc functions
363+
const mockDoc = jest.fn();
364+
doc.mockReturnValue(mockDoc);
365+
getDoc.mockResolvedValue({
366+
exists: () => true,
367+
data: () => userData
368+
});
369+
370+
await store.dispatch(startLoadUserData());
296371

297-
it('should log a message if user data does not exist', async () => {});
372+
// Wait for all promises to resolve
373+
await flushPromises();
298374

299-
it('should log error message on user data load failure', async () => {
300-
375+
const actions = store.getActions();
376+
377+
expect(getDoc).toHaveBeenCalledWith(mockDoc);
378+
expect(actions[0]).toEqual({
379+
type: UPDATE_USER_METRICS_DATA,
380+
payload: userData
381+
});
301382
});
302383

384+
it('should log a message if user data does not exist', async () => {
385+
const store = mockStore({});
303386

387+
// Mock the doc and getDoc functions
388+
const mockDoc = jest.fn();
389+
doc.mockReturnValue(mockDoc);
390+
getDoc.mockResolvedValue({
391+
exists: () => false,
392+
});
393+
394+
// Spy on console.log
395+
const consoleLogSpy = jest.spyOn(console, 'log');
396+
397+
await store.dispatch(startLoadUserData());
398+
399+
// Wait for all promises to resolve
400+
await flushPromises();
401+
402+
expect(getDoc).toHaveBeenCalledWith(mockDoc);
403+
expect(consoleLogSpy).toHaveBeenCalledWith("User data doesn't exist in the database!");
404+
405+
// Clean up the spy
406+
consoleLogSpy.mockRestore();
407+
});
408+
409+
it('should log error message on user data load failure', async () => {
410+
const store = mockStore({});
411+
const error = new Error('Failed to load user data');
412+
413+
// Mock the doc and getDoc functions
414+
const mockDoc = jest.fn();
415+
doc.mockReturnValue(mockDoc);
416+
getDoc.mockRejectedValue(error);
417+
418+
await store.dispatch(startLoadUserData());
419+
420+
// Wait for all promises to resolve
421+
await flushPromises();
422+
423+
const actions = store.getActions();
424+
425+
expect(getDoc).toHaveBeenCalledWith(mockDoc);
426+
expect(actions).toEqual([]);
427+
});
304428
});
305429

306430
// ### Email and Password Actions

0 commit comments

Comments
 (0)