1
- package app.fyreplace.fyreplace.test.screens
1
+ package app.fyreplace.fyreplace.test.viewmodels
2
2
3
3
import androidx.lifecycle.SavedStateHandle
4
4
import app.fyreplace.fyreplace.R
@@ -16,7 +16,6 @@ import app.fyreplace.fyreplace.viewmodels.screens.RegisterViewModel
16
16
import kotlinx.coroutines.ExperimentalCoroutinesApi
17
17
import kotlinx.coroutines.flow.collect
18
18
import kotlinx.coroutines.launch
19
- import kotlinx.coroutines.test.TestScope
20
19
import kotlinx.coroutines.test.runCurrent
21
20
import kotlinx.coroutines.test.runTest
22
21
import org.junit.Assert.assertEquals
@@ -28,7 +27,7 @@ import org.junit.Test
28
27
class RegisterViewModelTests : TestsBase () {
29
28
@Test
30
29
fun `Username must have correct length` () = runTest {
31
- val (minLength, maxLength, viewModel) = makeViewModel(FakeEventBus () )
30
+ val (minLength, maxLength, viewModel) = makeViewModel()
32
31
viewModel.updateEmail(FakeUsersEndpointApi .GOOD_EMAIL )
33
32
viewModel.updateHasAcceptedTerms(true )
34
33
backgroundScope.launch { viewModel.canSubmit.collect() }
@@ -53,7 +52,7 @@ class RegisterViewModelTests : TestsBase() {
53
52
54
53
@Test
55
54
fun `Email must have correct length` () = runTest {
56
- val (minLength, maxLength, viewModel) = makeViewModel(FakeEventBus () )
55
+ val (minLength, maxLength, viewModel) = makeViewModel()
57
56
viewModel.updateUsername(FakeUsersEndpointApi .GOOD_USERNAME )
58
57
viewModel.updateHasAcceptedTerms(true )
59
58
backgroundScope.launch { viewModel.canSubmit.collect() }
@@ -78,7 +77,7 @@ class RegisterViewModelTests : TestsBase() {
78
77
79
78
@Test
80
79
fun `Email must have @` () = runTest {
81
- val (_, _, viewModel) = makeViewModel(FakeEventBus () )
80
+ val (_, _, viewModel) = makeViewModel()
82
81
viewModel.updateUsername(FakeUsersEndpointApi .GOOD_USERNAME )
83
82
viewModel.updateHasAcceptedTerms(true )
84
83
backgroundScope.launch { viewModel.canSubmit.collect() }
@@ -94,7 +93,7 @@ class RegisterViewModelTests : TestsBase() {
94
93
95
94
@Test
96
95
fun `Terms must be accepted` () = runTest {
97
- val (_, _, viewModel) = makeViewModel(FakeEventBus () )
96
+ val (_, _, viewModel) = makeViewModel()
98
97
viewModel.updateUsername(FakeUsersEndpointApi .GOOD_USERNAME )
99
98
viewModel.updateEmail(FakeUsersEndpointApi .GOOD_EMAIL )
100
99
backgroundScope.launch { viewModel.canSubmit.collect() }
@@ -170,13 +169,13 @@ class RegisterViewModelTests : TestsBase() {
170
169
runCurrent()
171
170
viewModel.submit()
172
171
runCurrent()
173
- assertEquals( 0 , eventBus.storedEvents.filterIsInstance<Event .Failure >().count ())
172
+ assertTrue( eventBus.storedEvents.filterIsInstance<Event .Failure >().isEmpty ())
174
173
assertTrue(viewModel.isWaitingForRandomCode.value)
175
174
}
176
175
177
176
@Test
178
177
fun `Random code must have correct length` () = runTest {
179
- val (minLength, _, viewModel) = makeViewModel(FakeEventBus () )
178
+ val (minLength, _, viewModel) = makeViewModel()
180
179
backgroundScope.launch { viewModel.canSubmit.collect() }
181
180
182
181
viewModel.updateUsername(FakeUsersEndpointApi .GOOD_USERNAME )
@@ -231,10 +230,10 @@ class RegisterViewModelTests : TestsBase() {
231
230
runCurrent()
232
231
viewModel.submit()
233
232
runCurrent()
234
- assertEquals( 0 , eventBus.storedEvents.filterIsInstance<Event .Failure >().count ())
233
+ assertTrue( eventBus.storedEvents.filterIsInstance<Event .Failure >().isEmpty ())
235
234
}
236
235
237
- private fun TestScope. makeViewModel (eventBus : EventBus ): Triple <Int , Int , RegisterViewModel > {
236
+ private fun makeViewModel (eventBus : EventBus = FakeEventBus () ): Triple <Int , Int , RegisterViewModel > {
238
237
val minLength = 5
239
238
val maxLength = 100
240
239
val resources = FakeResourceResolver (
@@ -254,7 +253,6 @@ class RegisterViewModelTests : TestsBase() {
254
253
secretsHandler = FakeSecretsHandler (),
255
254
apiResolver = FakeApiResolver ()
256
255
)
257
- runCurrent()
258
256
return Triple (minLength, maxLength, viewModel)
259
257
}
260
258
}
0 commit comments