-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93d2ab5
commit 2e10631
Showing
10 changed files
with
277 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
feature/signup/src/test/java/nl/jovmit/androiddevs/feature/signup/SignUpTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package nl.jovmit.androiddevs.feature.signup | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import com.google.common.truth.Truth.assertThat | ||
import nl.jovmit.androiddevs.domain.auth.InMemoryAuthRepository | ||
import nl.jovmit.androiddevs.domain.auth.data.User | ||
import nl.jovmit.androiddevs.feature.signup.state.SignUpScreenState | ||
import nl.jovmit.androiddevs.testutils.CoroutineTestExtension | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
@ExtendWith(CoroutineTestExtension::class) | ||
class SignUpTest { | ||
|
||
// - we don't want long running operations in the main thread | ||
// - state delivery in order | ||
// - contract test to make sure prod code is aligned with the fake | ||
|
||
private val validEmail = "[email protected]" | ||
private val validPassword = "ValidP@ssword1" | ||
|
||
private val authRepository = InMemoryAuthRepository() | ||
private val savedStateHandle = SavedStateHandle() | ||
|
||
@Test | ||
fun invalidEmail() { | ||
val email = "invalid email format" | ||
val viewModel = SignUpViewModel(savedStateHandle, authRepository) | ||
|
||
viewModel.signUp(email = email) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState( | ||
email = email, | ||
incorrectEmailFormat = true, | ||
incorrectPasswordFormat = true | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun invalidPassword() { | ||
val viewModel = SignUpViewModel(savedStateHandle, authRepository) | ||
|
||
viewModel.signUp(email = validEmail) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState(email = validEmail, incorrectPasswordFormat = true) | ||
) | ||
} | ||
|
||
@Test | ||
fun invalidEmailWithValidPassword() { | ||
val viewModel = SignUpViewModel(savedStateHandle, authRepository) | ||
|
||
viewModel.signUp(password = validPassword) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState(password = validPassword, incorrectEmailFormat = true) | ||
) | ||
} | ||
|
||
@Test | ||
fun signedUpSuccessfully() { | ||
val viewModel = SignUpViewModel(savedStateHandle, authRepository) | ||
|
||
viewModel.signUp(validEmail, validPassword) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState( | ||
email = validEmail, | ||
password = validPassword, | ||
isSignedUp = true | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun attemptToSignUpWithKnownEmail() { | ||
val newPassword = "another$validPassword" | ||
val repository = InMemoryAuthRepository( | ||
usersForPassword = mapOf(validPassword to listOf(User("userId", validEmail, ""))) | ||
) | ||
val viewModel = SignUpViewModel(savedStateHandle, repository) | ||
|
||
viewModel.signUp(validEmail, newPassword) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState( | ||
email = validEmail, | ||
password = newPassword, | ||
isExistingEmail = true | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun errorCreatingNewAccount() { | ||
val unavailableAuthRepository = InMemoryAuthRepository().apply { | ||
setUnavailable() | ||
} | ||
val viewModel = SignUpViewModel(savedStateHandle, unavailableAuthRepository) | ||
|
||
viewModel.signUp(validEmail, validPassword) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState( | ||
email = validEmail, | ||
password = validPassword, | ||
isBackendError = true | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun attemptToSignUpWhenOffline() { | ||
val offlineAuthRepository = InMemoryAuthRepository().apply { | ||
setOffline() | ||
} | ||
val viewModel = SignUpViewModel(savedStateHandle, offlineAuthRepository) | ||
|
||
viewModel.signUp(validEmail, validPassword) | ||
|
||
assertThat(viewModel.screenState.value).isEqualTo( | ||
SignUpScreenState( | ||
email = validEmail, | ||
password = validPassword, | ||
isOfflineError = true | ||
) | ||
) | ||
} | ||
|
||
private fun SignUpViewModel.signUp(email: String = "", password: String = "") { | ||
updateEmail(email) | ||
updatePassword(password) | ||
signUp() | ||
} | ||
} |
Oops, something went wrong.