Skip to content

Commit

Permalink
refactor: improve naming of validator
Browse files Browse the repository at this point in the history
  • Loading branch information
yamilmedina committed Jan 24, 2025
1 parent 9d5298e commit 43c537a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import kotlin.time.Duration.Companion.seconds
@HiltViewModel
class NewLoginViewModel @Inject constructor(
private val authServerConfigProvider: AuthServerConfigProvider,
private val emailOrSSOCodeValidator: EmailOrSSOCodeValidator,
private val validateEmailOrSSOCode: ValidateEmailOrSSOCodeUseCase,
private val savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val loginNavArgs: LoginNavArgs = savedStateHandle.navArgs()
Expand Down Expand Up @@ -88,7 +88,7 @@ class NewLoginViewModel @Inject constructor(
*/
fun onLoginStarted(onSuccess: () -> Unit) {
viewModelScope.launch {
if (emailOrSSOCodeValidator.validate(userIdentifierTextState.text.trim())) {
if (validateEmailOrSSOCode(userIdentifierTextState.text.trim())) {
updateLoginFlowState(LoginState.Loading)
delay(1.seconds) // TODO(ym): here the call to the use case should be done.
updateLoginFlowState(LoginState.Default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import javax.inject.Inject
* Validates the input for a SSO code or an email address valid format.
*/
@ViewModelScoped
class EmailOrSSOCodeValidator @Inject constructor(
class ValidateEmailOrSSOCodeUseCase @Inject constructor(
val validateEmail: ValidateEmailUseCase,
val validateSSOCode: ValidateSSOCodeUseCase
) {
/**
* Validates the input for a SSO code or an email address valid format.
*/
fun validate(input: CharSequence): Boolean {
operator fun invoke(input: CharSequence): Boolean {
return when {
input.startsWith(ValidateSSOCodeUseCase.SSO_CODE_WIRE_PREFIX) -> {
validateSSOCode(input.toString()) is ValidateSSOCodeResult.Valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class NewLoginViewModelTest {
@MockK
private lateinit var savedStateHandle: SavedStateHandle
val authServerConfigProvider: AuthServerConfigProvider = mockk()
val emailOrSSOCodeValidator: EmailOrSSOCodeValidator = mockk()
val validateEmailOrSSOCodeUseCase: ValidateEmailOrSSOCodeUseCase = mockk()

val onSuccess: () -> Unit = mockk()

Expand All @@ -66,12 +66,12 @@ class NewLoginViewModelTest {
}

fun withEmailOrSSOCodeValidatorReturning(result: Boolean = true) = apply {
every { emailOrSSOCodeValidator.validate(any()) } returns result
every { validateEmailOrSSOCodeUseCase(any()) } returns result
}

fun arrange() = this to NewLoginViewModel(
authServerConfigProvider,
emailOrSSOCodeValidator,
validateEmailOrSSOCodeUseCase,
savedStateHandle
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class EmailOrSSOCodeValidatorTest {
class ValidateEmailOrSSOCodeUseCaseTest {

@Test
fun `given an input, when is an email and valid, then return true`() {
val (arrangement, sut) = Arrangement()
.withValidateEmailUseCaseReturning(true)
.arrange()

val result = sut.validate("[email protected]")
val result = sut.invoke("[email protected]")

verify { arrangement.validateEmailUseCase(any()) }
verify(exactly = 0) { arrangement.validateSSOCodeUseCase(any()) }
Expand All @@ -30,7 +30,7 @@ class EmailOrSSOCodeValidatorTest {
.withValidateEmailUseCaseReturning(false)
.arrange()

val result = sut.validate("user@")
val result = sut.invoke("user@")

verify { arrangement.validateEmailUseCase(any()) }
verify(exactly = 0) { arrangement.validateSSOCodeUseCase(any()) }
Expand All @@ -44,7 +44,7 @@ class EmailOrSSOCodeValidatorTest {
.withValidateSSOCodeUseCaseReturning(ValidateSSOCodeResult.Valid(code))
.arrange()

val result = sut.validate("wire-$code")
val result = sut.invoke("wire-$code")

verify { arrangement.validateSSOCodeUseCase(any()) }
verify(exactly = 0) { arrangement.validateEmailUseCase(any()) }
Expand All @@ -58,7 +58,7 @@ class EmailOrSSOCodeValidatorTest {
.withValidateSSOCodeUseCaseReturning(ValidateSSOCodeResult.Invalid)
.arrange()

val result = sut.validate("wire-$code")
val result = sut.invoke("wire-$code")

verify { arrangement.validateSSOCodeUseCase(any()) }
verify(exactly = 0) { arrangement.validateEmailUseCase(any()) }
Expand All @@ -72,7 +72,7 @@ class EmailOrSSOCodeValidatorTest {
.withValidateEmailUseCaseReturning(false)
.arrange()

val result = sut.validate("nothing-valid")
val result = sut.invoke("nothing-valid")

verify(exactly = 1) { arrangement.validateEmailUseCase(any()) }
verify(exactly = 0) { arrangement.validateSSOCodeUseCase(any()) }
Expand All @@ -92,7 +92,7 @@ class EmailOrSSOCodeValidatorTest {
every { validateSSOCodeUseCase(any()) } returns result
}

fun arrange() = this to EmailOrSSOCodeValidator(validateEmailUseCase, validateSSOCodeUseCase)
fun arrange() = this to ValidateEmailOrSSOCodeUseCase(validateEmailUseCase, validateSSOCodeUseCase)
}

}

0 comments on commit 43c537a

Please sign in to comment.