|
1 | 1 | import { auth } from '../firebaseConfig.js';
|
2 | 2 | import configureMockStore from 'redux-mock-store';
|
3 | 3 | import thunk from 'redux-thunk';
|
| 4 | +import { setDoc, getDoc, doc } from 'firebase/firestore'; |
4 | 5 | import { firebaseErrorsMessages } from '../src/utils/firebaseErrorsMessages.js';
|
5 | 6 | import { render, waitFor } from "@testing-library/react"
|
6 | 7 | import flushPromises from 'flush-promises';
|
@@ -279,28 +280,151 @@ describe('Async User Actions', () => {
|
279 | 280 | });
|
280 | 281 | });
|
281 | 282 |
|
| 283 | + |
| 284 | + |
| 285 | + |
282 | 286 | // ### User Data Actions
|
283 | 287 |
|
284 | 288 | describe('User Data Actions', () => {
|
285 | 289 | 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' }; |
286 | 292 |
|
| 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 | + }); |
287 | 314 | });
|
288 | 315 |
|
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 | + }); |
290 | 332 |
|
291 | 333 | 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'); |
292 | 337 |
|
| 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([]); |
293 | 356 | });
|
294 | 357 |
|
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()); |
296 | 371 |
|
297 |
| - it('should log a message if user data does not exist', async () => {}); |
| 372 | + // Wait for all promises to resolve |
| 373 | + await flushPromises(); |
298 | 374 |
|
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 | + }); |
301 | 382 | });
|
302 | 383 |
|
| 384 | + it('should log a message if user data does not exist', async () => { |
| 385 | + const store = mockStore({}); |
303 | 386 |
|
| 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 | + }); |
304 | 428 | });
|
305 | 429 |
|
306 | 430 | // ### Email and Password Actions
|
|
0 commit comments