Skip to content

Commit

Permalink
Update unit tests for usState support
Browse files Browse the repository at this point in the history
Had to do a bit of type casting in the unit tests to get around the
`Nothing?` type of PinDetails giving the compiler ambiguous overloading
but that wasn't too big an issue.

It's worth noting that making ElementState generic did break the tests
(i.e. it was not reverse compatibile) because the tests explicitly
referenced the types `ElementState<PanDetails>` and
`StatefulElementListner<PanDetails>` callback. Referencing these types
in this way is not necessary and it's like that GoPuff did not do this
in which case it would not even be a breaking change for them.
  • Loading branch information
devinmorgan committed Dec 1, 2023
1 parent 8d68770 commit df8523d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class ElementStateManagerTest {
fun testGetState() {
val manager = PanElementStateManager.forEmptyInput()
val state = manager.getState()
assertThat(state.isFocused).isEqualTo(INITIAL_ELEMENT_STATE.isFocused)
assertThat(state.isBlurred).isEqualTo(INITIAL_ELEMENT_STATE.isBlurred)
assertThat(state.isEmpty).isEqualTo(INITIAL_ELEMENT_STATE.isEmpty)
assertThat(state.isValid).isEqualTo(INITIAL_ELEMENT_STATE.isValid)
assertThat(state.isComplete).isEqualTo(INITIAL_ELEMENT_STATE.isComplete)
assertThat(state.validationError).isEqualTo(INITIAL_ELEMENT_STATE.validationError)
assertThat(state.isFocused).isEqualTo(INITIAL_PAN_ELEMENT_STATE.isFocused)
assertThat(state.isBlurred).isEqualTo(INITIAL_PAN_ELEMENT_STATE.isBlurred)
assertThat(state.isEmpty).isEqualTo(INITIAL_PAN_ELEMENT_STATE.isEmpty)
assertThat(state.isValid).isEqualTo(INITIAL_PAN_ELEMENT_STATE.isValid)
assertThat(state.isComplete).isEqualTo(INITIAL_PAN_ELEMENT_STATE.isComplete)
assertThat(state.validationError).isEqualTo(INITIAL_PAN_ELEMENT_STATE.validationError)
assertThat(state.details).isEqualTo(INITIAL_PAN_ELEMENT_STATE.details)
}

@Test
Expand Down Expand Up @@ -68,17 +69,27 @@ class ElementStateManagerTest {
state = manager.getState()
assertThat(state.isFocused).isTrue
assertThat(state.isBlurred).isFalse
assertThat(state.isEmpty).isEqualTo(INITIAL_ELEMENT_STATE.isEmpty)
assertThat(state.isValid).isEqualTo(INITIAL_ELEMENT_STATE.isValid)
assertThat(state.isComplete).isEqualTo(INITIAL_ELEMENT_STATE.isComplete)
assertThat(state.isEmpty).isEqualTo(INITIAL_PIN_ELEMENT_STATE.isEmpty)
assertThat(state.isValid).isEqualTo(INITIAL_PIN_ELEMENT_STATE.isValid)
assertThat(state.isComplete).isEqualTo(INITIAL_PIN_ELEMENT_STATE.isComplete)

// cast to Any? to avoid ambiguous overload issue that
// is unique to PinDetails because it's an alias for `Nothing?`
assertThat(state.details as Any?).isEqualTo(INITIAL_PIN_ELEMENT_STATE.details)


// unfocus
manager.changeFocus(false)
state = manager.getState()
assertThat(state.isFocused).isFalse
assertThat(state.isBlurred).isTrue
assertThat(state.isEmpty).isEqualTo(INITIAL_ELEMENT_STATE.isEmpty)
assertThat(state.isValid).isEqualTo(INITIAL_ELEMENT_STATE.isValid)
assertThat(state.isComplete).isEqualTo(INITIAL_ELEMENT_STATE.isComplete)
assertThat(state.isEmpty).isEqualTo(INITIAL_PIN_ELEMENT_STATE.isEmpty)
assertThat(state.isValid).isEqualTo(INITIAL_PIN_ELEMENT_STATE.isValid)
assertThat(state.isComplete).isEqualTo(INITIAL_PIN_ELEMENT_STATE.isComplete)

// cast to Any? to avoid ambiguous overload issue that
// is unique to PinDetails because it's an alias for `Nothing?`
assertThat(state.details as Any?).isEqualTo(INITIAL_PIN_ELEMENT_STATE.details)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.joinforage.forage.android.core.element.IncompleteEbtPanError
import com.joinforage.forage.android.core.element.InvalidEbtPanError
import com.joinforage.forage.android.core.element.StatefulElementListener
import com.joinforage.forage.android.core.element.TooLongEbtPanError
import com.joinforage.forage.android.model.USState
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

Expand All @@ -18,6 +19,7 @@ class StrictForEmptyInputTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isNull()
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -34,6 +36,7 @@ class StrictForEmptyInputTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isEqualTo(IncompleteEbtPanError)
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -46,6 +49,7 @@ class StrictForEmptyInputTest {
assertThat(state.isValid).isFalse
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isEqualTo(InvalidEbtPanError)
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -58,6 +62,7 @@ class StrictForEmptyInputTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isEqualTo(IncompleteEbtPanError)
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo(USState.MAINE)))
}

@Test
Expand All @@ -70,6 +75,7 @@ class StrictForEmptyInputTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.validationError).isNull()
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo(USState.MAINE)))
}

@Test
Expand All @@ -85,6 +91,7 @@ class StrictForEmptyInputTest {
assertThat(state.isValid).isFalse
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isEqualTo(TooLongEbtPanError)
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo(USState.MAINE)))
}
}

Expand All @@ -99,6 +106,7 @@ class DEV_ONLY_IntegrationTests {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.validationError).isNull()
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo(USState.MAINE)))
}

@Test
Expand All @@ -110,6 +118,7 @@ class DEV_ONLY_IntegrationTests {

assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -121,6 +130,7 @@ class DEV_ONLY_IntegrationTests {

assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -132,6 +142,7 @@ class DEV_ONLY_IntegrationTests {

assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -143,6 +154,7 @@ class DEV_ONLY_IntegrationTests {

assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -154,6 +166,7 @@ class DEV_ONLY_IntegrationTests {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isNull()
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}

@Test
Expand All @@ -165,6 +178,7 @@ class DEV_ONLY_IntegrationTests {
assertThat(state.isValid).isFalse
assertThat(state.isComplete).isFalse
assertThat(state.validationError).isEqualTo(InvalidEbtPanError)
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo()))
}
}

Expand Down Expand Up @@ -259,8 +273,8 @@ class PanHandleChangeEventTest {
@Test
fun `valid card 16-digit card number passes correct state to callback`() {
val manager = PanElementStateManager.forEmptyInput()
var state: ElementState = manager.getState()
val callback: StatefulElementListener = { newState -> state = newState }
var state: ElementState<PanDetails> = manager.getState()
val callback: StatefulElementListener<PanDetails> = { newState -> state = newState }
manager.setOnChangeEventListener(callback)
manager.handleChangeEvent("5076807890123456")

Expand All @@ -272,15 +286,16 @@ class PanHandleChangeEventTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.validationError).isNull()
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo(USState.ALABAMA)))
}

@Test
fun `setting multiple callbacks only invokes most recently set callback`() {
val manager = PanElementStateManager.forEmptyInput()
var callbackAInvoked = false
var callbackBInvoked = false
val callbackA: StatefulElementListener = { callbackAInvoked = true }
val callbackB: StatefulElementListener = { callbackBInvoked = true }
val callbackA: StatefulElementListener<PanDetails> = { callbackAInvoked = true }
val callbackB: StatefulElementListener<PanDetails> = { callbackBInvoked = true }

manager.setOnChangeEventListener(callbackA)
manager.setOnChangeEventListener(callbackB)
Expand All @@ -294,8 +309,8 @@ class PanHandleChangeEventTest {
@Test
fun `strips all non-digit characters before processing`() {
val manager = PanElementStateManager.forEmptyInput()
var state: ElementState = manager.getState()
val callback: StatefulElementListener = { newState -> state = newState }
var state: ElementState<PanDetails> = manager.getState()
val callback: StatefulElementListener<PanDetails> = { newState -> state = newState }

val validStringContaminatedByOtherChars = "!@# $%^ &*()_+<>? abcd5076807890123456"
manager.setOnChangeEventListener(callback)
Expand All @@ -305,5 +320,6 @@ class PanHandleChangeEventTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.validationError).isNull()
assertThat(state.details).isEqualTo(PanDetails(DerivedCardInfo(USState.ALABAMA)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SetPinIsCompleteTest {
assertThat(state.isComplete).isTrue
assertThat(state.isValid).isTrue
assertThat(state.validationError).isNull()
assertThat(state.details as Any?).isNull()
}

@Test
Expand All @@ -48,6 +49,10 @@ class SetPinIsCompleteTest {
assertThat(state.isComplete).isFalse
assertThat(state.isValid).isFalse
assertThat(state.validationError).isEqualTo(IncompleteEbtPinError)

// cast to Any? to avoid ambiguous overload issue that
// is unique to PinDetails because it's an alias for `Nothing?`
assertThat(state.details as Any?).isNull()
}
}

Expand All @@ -65,8 +70,8 @@ class PinHandleChangeEventTest {
@Test
fun `completed pin passes correct state to callback`() {
val manager = PinElementStateManager.forEmptyInput()
var state: ElementState = manager.getState()
val callback: StatefulElementListener = { newState -> state = newState }
var state: ElementState<PinDetails> = manager.getState()
val callback: StatefulElementListener<PinDetails> = { newState -> state = newState }
manager.setOnChangeEventListener(callback)
manager.handleChangeEvent(isComplete = true, isEmpty = false)

Expand All @@ -78,15 +83,19 @@ class PinHandleChangeEventTest {
assertThat(state.isValid).isTrue
assertThat(state.isComplete).isTrue
assertThat(state.validationError).isNull()

// cast to Any? to avoid ambiguous overload issue that
// is unique to PinDetails because it's an alias for `Nothing?`
assertThat(state.details as Any?).isNull()
}

@Test
fun `setting multiple callbacks only invokes most recently set callback`() {
val manager = PinElementStateManager.forEmptyInput()
var callbackAInvoked = false
var callbackBInvoked = false
val callbackA: StatefulElementListener = { callbackAInvoked = true }
val callbackB: StatefulElementListener = { callbackBInvoked = true }
val callbackA: StatefulElementListener<PinDetails> = { callbackAInvoked = true }
val callbackB: StatefulElementListener<PinDetails> = { callbackBInvoked = true }

manager.setOnChangeEventListener(callbackA)
manager.setOnChangeEventListener(callbackB)
Expand Down

0 comments on commit df8523d

Please sign in to comment.