-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesViewModelTest.kt
More file actions
369 lines (315 loc) · 16.3 KB
/
Copy pathNotesViewModelTest.kt
File metadata and controls
369 lines (315 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.thewhitewings.pouch.presentation
import com.thewhitewings.pouch.feature_note.domain.model.Note
import com.thewhitewings.pouch.feature_note.domain.repository.OfflineNotesRepository
import com.thewhitewings.pouch.feature_note.domain.util.SortOption
import com.thewhitewings.pouch.feature_note.presentation.notes.NotesUiState
import com.thewhitewings.pouch.feature_note.presentation.notes.NotesViewModel
import com.thewhitewings.pouch.feature_note.util.Zone
import com.thewhitewings.pouch.rules.MainDispatcherRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.kotlin.times
import org.mockito.kotlin.whenever
@OptIn(ExperimentalCoroutinesApi::class)
class NotesViewModelTest {
// Rule to set main dispatcher to a test coroutine dispatcher
@get:Rule
val mainDispatcherRule = MainDispatcherRule()
// Mocks and ViewModel
private lateinit var viewModel: NotesViewModel
private lateinit var notesRepository: OfflineNotesRepository
// Test dispatcher for coroutines
private val testDispatcher = UnconfinedTestDispatcher()
private val initialZone = Zone.CREATIVE
private val initialSortOption = SortOption.NEWEST_FIRST
private val mockInitialNotesList = listOf(
Note(1, "Test Note1", "Test Body1", "2024 1"),
Note(2, "Test Note2", "Test Body2", "2024 2"),
Note(3, "Test Note3", "Test Body3", "2024 3")
)
/**
* Set up the test environment before each test.
*/
@Before
fun setUp() {
// Mocking the repository
notesRepository = mock(OfflineNotesRepository::class.java)
// Mocking the repository methods that are needed
// because they are being observed by the view model.
// Their observation starts on the view model initialization
whenever(notesRepository.getSortOptionStream(initialZone))
.thenReturn(flowOf(initialSortOption))
whenever(notesRepository.getAllNotesStream(initialSortOption))
.thenReturn(flowOf(mockInitialNotesList))
// Initialize ViewModel before each test, with a test dispatcher
viewModel = NotesViewModel(notesRepository, testDispatcher)
}
/**
* Test that zone changes are collected and sort option is updated correctly.
* Happy path test for [NotesViewModel.collectZoneAndCollectSortOption]
*/
@Test
fun `When zone changes, update sort option correctly`() = runTest {
// Given: the initial zone is CREATIVE, and the sort option is NEWEST_FIRST
val expectedZone = Zone.BOX_OF_MYSTERIES
val expectedMockSortOption = SortOption.OLDEST_FIRST
val expectedMockNotesList = mockInitialNotesList.reversed()
whenever(notesRepository.getSortOptionStream(expectedZone))
.thenReturn(flowOf(expectedMockSortOption))
whenever(notesRepository.getAllNotesStream(expectedMockSortOption))
.thenReturn(flowOf(expectedMockNotesList))
// Act: Update the zone and trigger the function to collect the sort option
viewModel.toggleZone()
assertEquals(expectedZone, viewModel.uiState.value.zone)
// Assert: Verify that the repository's getSortOptionStream is called with the new zone
verify(notesRepository).getSortOptionStream(expectedZone)
// Assert: Ensure that the sort option is correctly updated in the UI state
assertEquals(
expectedMockSortOption,
viewModel.uiState.value.sortOption
)
}
/**
* Test that sort option changes are collected and notes list is updated correctly.
* Happy path for [NotesViewModel.collectSortOptionSearchQueryZoneAndCollectNotesList]
*/
@Test
fun `When sort option changes and search query is empty, update notes list correctly`() =
runTest {
// Given: the initial sort option is NEWEST_FIRST, and search query is empty
val mockSortOption = SortOption.OLDEST_FIRST
val expectedMockNotesList = mockInitialNotesList.reversed()
whenever(notesRepository.getAllNotesStream(mockSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSortOptionStateForTesting(mockSortOption.id)
assertEquals(mockSortOption, viewModel.uiState.value.sortOption)
verify(notesRepository).getSortOptionStream(initialZone)
verify(notesRepository).getAllNotesStream(mockSortOption)
assertEquals(expectedMockNotesList, viewModel.uiState.value.notesList)
}
/**
* Test that sort option changes are collected and notes list is updated correctly.
* Happy path for [NotesViewModel.collectSortOptionSearchQueryZoneAndCollectNotesList]
*/
@Test
fun `When sort option changes and search query is Not empty, update notes list correctly`() =
runTest {
// Given: the initial sort option is NEWEST_FIRST, and search query is empty
val mockSortOption = SortOption.OLDEST_FIRST
val mockSearchQuery = "Test"
val expectedMockNotesList = mockInitialNotesList.reversed()
whenever(notesRepository.getAllNotesStream(mockSortOption))
.thenReturn(flowOf(expectedMockNotesList))
whenever(notesRepository.searchNotesStream(mockSearchQuery, mockSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSortOptionStateForTesting(mockSortOption.id)
assertEquals(mockSortOption, viewModel.uiState.value.sortOption)
verify(notesRepository).getSortOptionStream(initialZone)
viewModel.updateSearchQuery(mockSearchQuery)
assertEquals(mockSearchQuery, viewModel.uiState.value.searchQuery)
verify(notesRepository).searchNotesStream(mockSearchQuery, mockSortOption)
assertEquals(expectedMockNotesList, viewModel.uiState.value.notesList)
}
/**
* Test that search query changes are collected and notes list is updated correctly.
* Happy path for [NotesViewModel.collectSortOptionSearchQueryZoneAndCollectNotesList]
*/
@Test
fun `When search query changes and search query is Not empty, update notes list correctly`() =
runTest {
// Given: the initial search query is empty
val mockSearchQuery = "Test"
val expectedMockNotesList = mockInitialNotesList.reversed()
whenever(notesRepository.searchNotesStream(mockSearchQuery, initialSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSearchQuery(mockSearchQuery)
assertEquals(mockSearchQuery, viewModel.uiState.value.searchQuery)
verify(notesRepository).searchNotesStream(mockSearchQuery, initialSortOption)
assertEquals(expectedMockNotesList, viewModel.uiState.value.notesList)
}
/**
* Test that search query changes are collected and notes list is updated correctly.
* Happy path for [NotesViewModel.collectSortOptionSearchQueryZoneAndCollectNotesList]
*/
@Test
fun `When search query changes and search query is empty, update notes list correctly`() =
runTest {
// Given: the initial search query is empty
val expectedMockNotesListFirst = mockInitialNotesList.reversed()
val expectedMockNotesListLast = mockInitialNotesList
val mockSearchQueryFirstChange = "Test"
val mockSearchQueryLastChange = ""
whenever(
notesRepository.searchNotesStream(
mockSearchQueryFirstChange,
initialSortOption
)
)
.thenReturn(flowOf(expectedMockNotesListFirst))
whenever(notesRepository.getAllNotesStream(initialSortOption))
.thenReturn(flowOf(expectedMockNotesListLast))
viewModel.updateSearchQuery(mockSearchQueryFirstChange)
assertEquals(mockSearchQueryFirstChange, viewModel.uiState.value.searchQuery)
verify(notesRepository).searchNotesStream(mockSearchQueryFirstChange, initialSortOption)
assertEquals(expectedMockNotesListFirst, viewModel.uiState.value.notesList)
viewModel.updateSearchQuery(mockSearchQueryLastChange)
assertEquals(mockSearchQueryLastChange, viewModel.uiState.value.searchQuery)
verify(notesRepository, times(2)).getAllNotesStream(initialSortOption)
assertEquals(expectedMockNotesListLast, viewModel.uiState.value.notesList)
}
/**
* Test that zone changes are collected and notes list is updated correctly.
* Happy path for [NotesViewModel.collectSortOptionSearchQueryZoneAndCollectNotesList]
*/
@Test
fun `When zone changes and search query is empty, update notes list correctly`() =
runTest {
// Given: the initial zone is CREATIVE, and the sort option is NEWEST_FIRST
val expectedZone = Zone.BOX_OF_MYSTERIES
val expectedMockNotesList = mockInitialNotesList.reversed()
whenever(notesRepository.getSortOptionStream(expectedZone))
.thenReturn(flowOf(initialSortOption))
whenever(notesRepository.getAllNotesStream(initialSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.toggleZone()
assertEquals(expectedZone, viewModel.uiState.value.zone)
verify(notesRepository).getSortOptionStream(expectedZone)
verify(notesRepository, times(2)).getAllNotesStream(initialSortOption)
assertEquals(expectedMockNotesList, viewModel.uiState.value.notesList)
}
/**
* Test that zone changes are collected and notes list is updated correctly.
* Happy path for [NotesViewModel.collectSortOptionSearchQueryZoneAndCollectNotesList]
*/
@Test
fun `When zone changes and search query is Not empty, update notes list correctly`() = runTest {
// Given: the initial zone is CREATIVE, search query is empty, and the sort option is NEWEST_FIRST
val expectedZone = Zone.BOX_OF_MYSTERIES
val mockSearchQuery = "Test"
val expectedMockNotesList = mockInitialNotesList.reversed()
whenever(notesRepository.searchNotesStream(mockSearchQuery, initialSortOption))
.thenReturn(flowOf(expectedMockNotesList))
whenever(notesRepository.getSortOptionStream(expectedZone))
.thenReturn(flowOf(initialSortOption))
whenever(notesRepository.getAllNotesStream(initialSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSearchQuery(mockSearchQuery)
assertEquals(mockSearchQuery, viewModel.uiState.value.searchQuery)
verify(notesRepository).searchNotesStream(mockSearchQuery, initialSortOption)
viewModel.toggleZone()
assertEquals(expectedZone, viewModel.uiState.value.zone)
verify(notesRepository).getSortOptionStream(expectedZone)
verify(notesRepository, times(3)).getAllNotesStream(initialSortOption)
assertEquals(expectedMockNotesList, viewModel.uiState.value.notesList)
}
/**
* Test that after [NotesViewModel] is initialized, the state of [NotesUiState.showAnimations] is updated correctly.
* Happy path for [NotesViewModel.updateShowAnimationsStateDelayed]
*/
@Test
fun `When the view model is initialized, showAnimations is updated correctly`() = runTest {
// Given: the initial showAnimations state is true
val expectedShowAnimations = false
testDispatcher.scheduler.advanceUntilIdle()
assertEquals(expectedShowAnimations, viewModel.uiState.value.showAnimations)
}
/**
* Test that [NotesViewModel.updateSearchQuery] updates the search query state correctly.
* Happy path for [NotesViewModel.updateSearchQuery]
*/
@Test
fun `When updating search query, update search query state correctly`() =
runTest {
// Given: the initial search query is empty
val mockSearchQuery = "Test"
val expectedMockNotesList = mockInitialNotesList.reversed()
/*
Updating the search query will trigger the collecting of the flow of
notesRepository.searchNotesStream in
collectSortOptionSearchQueryZoneAndCollectNotesList function.
*/
whenever(notesRepository.searchNotesStream(mockSearchQuery, initialSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSearchQuery(mockSearchQuery)
assertEquals(mockSearchQuery, viewModel.uiState.value.searchQuery)
}
/**
* Test that [NotesViewModel.updateSortOption] saves the sort option with [notesRepository.saveSortOption].
* Happy path for [NotesViewModel.updateSortOption]
*/
@Test
fun `When updating sort option, save the sort option`() = runTest {
// Given: the initial sort option is NEWEST_FIRST, and search query is empty
val mockSortOption = SortOption.OLDEST_FIRST
val expectedMockNotesList = mockInitialNotesList.reversed()
/*
Updating the sort option with updateSortOption will trigger the collecting
of the flow of notesRepository.getSortOptionStream(zone) in
collectZoneAndCollectSortOption function and the collecting in
collectSortOptionSearchQueryZoneAndCollectNotesList function.
*/
whenever(notesRepository.getAllNotesStream(mockSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSortOption(mockSortOption.id)
verify(notesRepository).saveSortOption(mockSortOption, initialZone)
verify(notesRepository).getSortOptionStream(initialZone)
}
/**
* Test that [NotesViewModel.updateSortOptionStateForTesting] updates the sort option state correctly.
* Happy path for [NotesViewModel.updateSortOptionStateForTesting]
*/
@Test
fun `When updating sort option, update sort option state correctly`() = runTest {
// Given: the initial sort option is NEWEST_FIRST, and search query is empty
val mockSortOption = SortOption.OLDEST_FIRST
val expectedMockNotesList = mockInitialNotesList.reversed()
/*
Updating the sort option with updateSortOptionStateForTesting will trigger the collecting
of the flow of notesRepository.getSortOptionStream(zone) in
collectZoneAndCollectSortOption function.
*/
whenever(notesRepository.getAllNotesStream(mockSortOption))
.thenReturn(flowOf(expectedMockNotesList))
viewModel.updateSortOptionStateForTesting(mockSortOption.id)
assertEquals(mockSortOption, viewModel.uiState.value.sortOption)
verify(notesRepository).getSortOptionStream(initialZone)
}
/**
* Test that [NotesViewModel.deleteNote] interacts with the repository to delete a note.
* Happy path for [NotesViewModel.deleteNote]
*/
@Test
fun `When deleteNote is called, call deleteNote on notesRepository`() = runTest {
val mockNote = Note(1, "Test Note", "Test Body", "2024 1")
viewModel.deleteNote(mockNote)
verify(notesRepository).deleteNote(mockNote)
}
/**
* Test that [NotesViewModel.toggleZone] toggles the zone and updates the UI state correctly.
* Happy path for [NotesViewModel.toggleZone]
*/
@Test
fun `When toggleZone is called, zone changes and ui state updates correctly`() = runTest {
// Given: the initial zone is CREATIVE
whenever(notesRepository.getSortOptionStream(Zone.BOX_OF_MYSTERIES))
.thenReturn(flowOf(initialSortOption))
whenever(notesRepository.getAllNotesStream(initialSortOption))
.thenReturn(flowOf(mockInitialNotesList))
viewModel.toggleZone()
verify(notesRepository).toggleZone()
assertEquals(Zone.BOX_OF_MYSTERIES, viewModel.uiState.value.zone)
assertEquals("", viewModel.uiState.value.searchQuery)
assertEquals(true, viewModel.uiState.value.showAnimations)
viewModel.toggleZone()
verify(notesRepository, times(2)).toggleZone()
assertEquals(Zone.CREATIVE, viewModel.uiState.value.zone)
}
}