1
+ /**
2
+ * Unit tests for app actions in the Smart Clothing App.
3
+ *
4
+ * This test file includes test cases for actions related to app-level state management,
5
+ * such as managing user metrics modal visibility, updating activity ring data, and
6
+ * updating heart rate and sleep data date ranges. These tests ensure that each action
7
+ * is dispatched correctly and that the appropriate payloads are passed to reducers.
8
+ *
9
+ * Mocks:
10
+ * - Firebase Firestore methods (getDoc, setDoc, etc.)
11
+ * - AsyncStorage (local storage mock)
12
+ * - Firebase Authentication methods (updateEmail, updateProfile, etc.)
13
+ *
14
+ * Global mocks and setup are done via jest to simulate database, authentication,
15
+ * and local storage services.
16
+ *
17
+ * Thunks for async actions are tested to verify proper dispatch behavior, with
18
+ * mock data and conditions ensuring each action triggers as expected.
19
+ *
20
+ * @file appActions.unit.test.js
21
+ *
22
+ * Credit: Carlos Figueroa (github @cfiguer055)
23
+ */
24
+
1
25
import configureMockStore from 'redux-mock-store' ;
2
26
import thunk from 'redux-thunk' ;
3
27
import {
@@ -16,11 +40,15 @@ import {
16
40
UPDATE_SLEEP_DATA_DATE_RANGE ,
17
41
} from '../../../src/actions/types.js' ;
18
42
19
- console . log ( userMetricsDataModalVisible ) ;
20
43
44
+
45
+
46
+
47
+ // Middleware and store setup
21
48
const middlewares = [ thunk ] ;
22
49
const mockStore = configureMockStore ( middlewares ) ;
23
50
51
+ // Mock Firebase dependencies and AsyncStorage
24
52
jest . mock ( '../../../src/utils/localStorage.js' , ( ) => ( {
25
53
AsyncStorage : jest . fn ( ) ,
26
54
} ) ) ;
@@ -57,8 +85,36 @@ jest.mock('firebase/auth', () => ({
57
85
58
86
59
87
88
+ /**
89
+ * Unit tests for app actions in Smart Clothing app.
90
+ *
91
+ * Test suite: User Metrics Actions
92
+ *
93
+ * This suite tests all the actions related to user metrics in the app. It verifies that actions
94
+ * like toggling the metrics modal visibility, updating activity rings, and updating date ranges
95
+ * for heart rate and sleep data behave as expected.
96
+ *
97
+ * This suite of tests validates the Redux actions for the app, including those that:
98
+ * - Modify the visibility of user metrics modals, update activity rings, and update
99
+ * date ranges for heart rate and sleep data.
100
+ * - It ensures that each action is dispatched correctly, and the reducers will be updated
101
+ * as expected.
102
+ *
103
+ * Mocks are set up for Firebase Firestore and AsyncStorage to simulate database interactions.
104
+ *
105
+ * @test {userMetricsDataModalVisible} Action to update user metrics modal visibility.
106
+ * @test {updateActivityRingsData} Action to update activity rings data.
107
+ * @test {updateHeartRateDateRangeData} Action to update heart rate data range.
108
+ */
60
109
describe ( 'User Metrics Actions' , ( ) => {
61
110
111
+ /**
112
+ * Test case: Should create an action to toggle user metrics data modal visibility
113
+ *
114
+ * This test validates that the `userMetricsDataModalVisible` action creator produces
115
+ * the correct action object when passed the visibility state and sign-up screen flag.
116
+ * @test {userMetricsDataModalVisible}
117
+ */
62
118
it ( 'should create an action to toggle user metrics data modal visibility' , ( ) => {
63
119
const expectedAction = {
64
120
type : USER_METRICS_DATA_MODAL_VISIBLE ,
@@ -70,6 +126,13 @@ describe('User Metrics Actions', () => {
70
126
expect ( userMetricsDataModalVisible ( true , true ) ) . toEqual ( expectedAction ) ;
71
127
} ) ;
72
128
129
+ /**
130
+ * Test case: Should create an action to update activity ring data for a specific day
131
+ *
132
+ * This test ensures that `updateActivityRingsData` generates an action with the correct
133
+ * payload, including the day of the week and the activity ring data (ring1, ring2, ring3).
134
+ * @test {updateActivityRingsData}
135
+ */
73
136
it ( 'should create an action to toggle activity ring data' , ( ) => {
74
137
const day = "Monday"
75
138
const ringData = { ring1 : '1.0' , ring2 : '1.0' , ring3 : '1.0' } ;
@@ -85,6 +148,13 @@ describe('User Metrics Actions', () => {
85
148
expect ( updateActivityRingsData ( day , ringData ) ) . toEqual ( expectedAction ) ;
86
149
} ) ;
87
150
151
+ /**
152
+ * Test case: Should create an action to update heart rate date range
153
+ *
154
+ * This test validates that the `updateHeartRateDateRangeData` action creator produces
155
+ * the correct action object with the start and end dates for the heart rate data.
156
+ * @test {updateHeartRateDateRangeData}
157
+ */
88
158
it ( 'should create an action to update heart rate date range' , ( ) => {
89
159
const startDate = '2024-01-01' ;
90
160
const endDate = '2024-01-07' ;
@@ -95,6 +165,13 @@ describe('User Metrics Actions', () => {
95
165
expect ( updateHeartRateDateRangeData ( startDate , endDate ) ) . toEqual ( expectedAction ) ;
96
166
} ) ;
97
167
168
+ /**
169
+ * Test case: Should create an action to update sleep data date range
170
+ *
171
+ * This test ensures that `updateSleepDataDateRangeData` correctly dispatches an action
172
+ * with the provided start and end dates for the sleep data.
173
+ * @test {updateSleepDataDateRangeData}
174
+ */
98
175
it ( 'should create an action to update sleep data date range' , ( ) => {
99
176
const startDate = '2024-01-01' ;
100
177
const endDate = '2024-01-07' ;
@@ -105,6 +182,14 @@ describe('User Metrics Actions', () => {
105
182
expect ( updateSleepDataDateRangeData ( startDate , endDate ) ) . toEqual ( expectedAction ) ;
106
183
} ) ;
107
184
185
+ /**
186
+ * Test case: Should dispatch actions to update activity rings data for each day of the week
187
+ *
188
+ * This test simulates the `updateActivityRings` thunk, which dispatches activity ring data
189
+ * for each day of the week. It ensures that all actions are dispatched with the correct
190
+ * data after the asynchronous operation completes.
191
+ * @test {updateActivityRings}
192
+ */
108
193
it ( 'should dispatch actions to update activity rings data' , async ( ) => {
109
194
const store = mockStore ( { } ) ;
110
195
const daysOfWeek = [
@@ -124,17 +209,27 @@ describe('User Metrics Actions', () => {
124
209
}
125
210
} ) ) ;
126
211
212
+ // Dispatch the thunk action
127
213
await store . dispatch ( updateActivityRings ( ) ) ;
128
214
const actions = store . getActions ( ) ;
129
215
216
+ // Verify that each action is dispatched correctly
130
217
expectedActions . forEach ( ( expectedAction , index ) => {
131
218
expect ( actions [ index ] ) . toEqual ( expectedAction ) ;
132
219
} ) ;
133
220
221
+ // Restore Math.random to its original implementation
134
222
Math . random . mockRestore ( ) ;
135
-
136
223
} , 10000 ) ;
137
224
225
+ /**
226
+ * Test case: Should dispatch actions to update heart rate data date range
227
+ *
228
+ * This test validates the `updateHeartRateDateRange` thunk, ensuring that the start and
229
+ * end dates for the heart rate data are correctly dispatched.
230
+ * @test {updateHeartRateDateRange}
231
+ */
232
+
138
233
it ( 'should dispatch actions to update heart rate data date range' , async ( ) => {
139
234
const store = mockStore ( { } ) ;
140
235
@@ -156,6 +251,13 @@ describe('User Metrics Actions', () => {
156
251
157
252
} , 10000 ) ;
158
253
254
+ /**
255
+ * Test case: Should dispatch actions to update sleep data date range
256
+ *
257
+ * This test ensures that `updateSleepDataDateRange` correctly dispatches the action
258
+ * with the provided start and end dates for the sleep data.
259
+ * @test {updateSleepDataDateRange}
260
+ */
159
261
it ( 'should dispatch actions to update sleep data date range' , async ( ) => {
160
262
const store = mockStore ( { } ) ;
161
263
0 commit comments