|
| 1 | +import configureMockStore from 'redux-mock-store'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | +import { |
| 4 | + userMetricsDataModalVisible, |
| 5 | + updateActivityRings, |
| 6 | + updateHeartRateDateRangeData, |
| 7 | + updateSleepDataDateRangeData, |
| 8 | + } from '../src/actions/userActions.js'; |
| 9 | +import { |
| 10 | + USER_METRICS_DATA_MODAL_VISIBLE, |
| 11 | + UPDATE_ACTIVITY_RINGS_DATA, |
| 12 | + UPDATE_HEART_RATE_DATE_RANGE, |
| 13 | + UPDATE_SLEEP_DATA_DATE_RANGE, |
| 14 | +} from '../src/actions/types.js'; |
| 15 | + |
| 16 | +const middlewares = [thunk]; |
| 17 | +const mockStore = configureMockStore(middlewares); |
| 18 | + |
| 19 | +jest.mock('../src/utils/localStorage.js', () => ({ |
| 20 | + AsyncStorage: jest.fn(), |
| 21 | + })); |
| 22 | + jest.mock('firebase/firestore', () => ({ |
| 23 | + collection: jest.fn(), |
| 24 | + addDoc: jest.fn(), |
| 25 | + setDoc: jest.fn(), |
| 26 | + doc: jest.fn(), |
| 27 | + updateDoc: jest.fn(), |
| 28 | + getDoc: jest.fn(), |
| 29 | + updateEmail: jest.fn(), |
| 30 | + })); |
| 31 | + jest.mock('../firebaseConfig.js', () => ({ |
| 32 | + auth: { |
| 33 | + signOut: jest.fn(() => Promise.resolve()), |
| 34 | + currentUser: { |
| 35 | + displayName: 'John Doe', |
| 36 | + }, |
| 37 | + }, |
| 38 | + })); |
| 39 | + jest.mock('firebase/auth', () => ({ |
| 40 | + createUserWithEmailAndPassword: jest.fn(), |
| 41 | + updateEmail: jest.fn(), |
| 42 | + signInWithEmailAndPassword: jest.fn(), |
| 43 | + updateProfile: jest.fn(() => Promise.resolve()), // Mock the updateProfile function |
| 44 | + sendPasswordResetEmail: jest.fn(), |
| 45 | + auth: { |
| 46 | + updateEmail: jest.fn(), |
| 47 | + }, |
| 48 | + })); |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +describe('User Metrics Actions', () => { |
| 53 | + it('should create an action to toggle user metrics data modal visibility', () => { |
| 54 | + const expectedAction = { |
| 55 | + type: USER_METRICS_DATA_MODAL_VISIBLE, |
| 56 | + payload: { |
| 57 | + visibility: true, |
| 58 | + isFromSignUpScreen: true, |
| 59 | + }, |
| 60 | + }; |
| 61 | + expect(userMetricsDataModalVisible(true, true)).toEqual(expectedAction); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should dispatch actions to update activity rings data', async () => { |
| 65 | + const store = mockStore({}); |
| 66 | + const day = 'Monday'; |
| 67 | + const ringData = { ring1: '1.0', ring2: '1.5', ring3: '0.5' }; |
| 68 | + |
| 69 | + // Mock the random value generator if necessary or ensure predictable output for tests |
| 70 | + jest.spyOn(Math, 'random').mockReturnValue(0.5); |
| 71 | + |
| 72 | + const expectedActions = [ |
| 73 | + { |
| 74 | + type: UPDATE_ACTIVITY_RINGS_DATA, |
| 75 | + payload: { day, rings: ringData }, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + await store.dispatch(updateActivityRings()); |
| 80 | + expect(store.getActions()).toContainEqual(expectedActions[0]); |
| 81 | + |
| 82 | + // Restore the original implementation |
| 83 | + Math.random.mockRestore(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should create an action to update heart rate date range', () => { |
| 87 | + const startDate = '2024-01-01'; |
| 88 | + const endDate = '2024-01-07'; |
| 89 | + const expectedAction = { |
| 90 | + type: UPDATE_HEART_RATE_DATE_RANGE, |
| 91 | + payload: { startDate, endDate }, |
| 92 | + }; |
| 93 | + expect(updateHeartRateDateRangeData(startDate, endDate)).toEqual(expectedAction); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should create an action to update sleep data date range', () => { |
| 97 | + const startDate = '2024-01-01'; |
| 98 | + const endDate = '2024-01-07'; |
| 99 | + const expectedAction = { |
| 100 | + type: UPDATE_SLEEP_DATA_DATE_RANGE, |
| 101 | + payload: { startDate, endDate }, |
| 102 | + }; |
| 103 | + expect(updateSleepDataDateRangeData(startDate, endDate)).toEqual(expectedAction); |
| 104 | + }); |
| 105 | +}); |
0 commit comments