Skip to content

Commit

Permalink
fix: Fix state machine to allow going back in steps (#736)
Browse files Browse the repository at this point in the history
Fix state machine to allow going back in steps

Co-authored-by: Boris Nikolic <[email protected]>
  • Loading branch information
borisprimer and BorisNikolic authored Nov 13, 2023
1 parent 31811fb commit 4ae5ab9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class MerchantHeadlessCheckoutNolPayViewController: UIViewController {
@objc func submitPhoneNumberTapped() {
guard let mobileNumber = linkMobileNumberTextField.text, !mobileNumber.isEmpty
else {
showAlert(title: "Error", message: "Please enter both country code and phone number.")
showAlert(title: "Error", message: "Please enter phone number.")
return
}

Expand Down Expand Up @@ -274,7 +274,7 @@ class MerchantHeadlessCheckoutNolPayViewController: UIViewController {
guard let mobileNumber = unlinkPhoneNumberTextField.text, !mobileNumber.isEmpty,
let card = selectedCardForUnlinking
else {
showAlert(title: "Error", message: "Please enter both country code and phone number.")
showAlert(title: "Error", message: "Please enter phone number.")
return
}

Expand Down Expand Up @@ -329,7 +329,7 @@ class MerchantHeadlessCheckoutNolPayViewController: UIViewController {
@objc func submitPaymentPhoneNumberButtonTapped() {
guard let phoneNumber = startPaymentPhoneNumberTextField.text, !phoneNumber.isEmpty
else {
showAlert(title: "Error", message: "Please enter both country code and phone number.")
showAlert(title: "Error", message: "Please enter phone number.")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,31 @@ final class NolPayLinkCardComponentTest: XCTestCase {
sut.start()
XCTAssertTrue(mockErrorDelegate.errorReceived is PrimerError)
}

func testUpdateCollectedDataWithPhoneData() {
// Given
let phoneData = NolPayLinkCollectableData.phoneData(mobileNumber: "1234567890")

// When
sut.updateCollectedData(collectableData: phoneData)

// Then
let expectedStep = String(describing: NolPayLinkCardStep.collectPhoneData(cardNumber: ""))
let actualStep = String(describing: sut.nextDataStep)
XCTAssertEqual(actualStep, expectedStep, "The nextDataStep should be .collectPhoneData after updating with phoneData")
}

func testUpdateCollectedDataWithOtpData() {
// Given
let otpData = NolPayLinkCollectableData.otpData(otpCode: "123456")

// When
sut.updateCollectedData(collectableData: otpData)

// Then
let expectedStep = String(describing: NolPayLinkCardStep.collectOtpData(phoneNumber: ""))
let actualStep = String(describing: sut.nextDataStep)
XCTAssertEqual(actualStep, expectedStep, "The nextDataStep should be .collectOtpData after updating with otpData")
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,17 @@ class NolPayPaymentComponentTests: XCTestCase {
XCTAssertTrue(mockErrorDelegate.errorReceived is PrimerError, "Expected error type to be PrimerError.")
}

func testUpdateCollectedDataWithPaymentData() {
// Given
let paymentData = NolPayPaymentCollectableData.paymentData(cardNumber: "1234567890123456", mobileNumber: "1234567890")

// When
sut.updateCollectedData(collectableData: paymentData)

// Then
let expectedStep = String(describing: NolPayPaymentStep.collectCardAndPhoneData)
let actualStep = String(describing: sut.nextDataStep)
XCTAssertEqual(actualStep, expectedStep, "The nextDataStep should be .collectCardAndPhoneData after updating with paymentData")
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,33 @@ final class NolPayUnlinkCardComponentTest: XCTestCase {

XCTAssertNotNil(mockErrorDelegate.errorReceived, "Expected an error when there's no card number.")
}

func testUpdateCollectedDataWithCardAndPhoneData() {
// Given
let mockCard = PrimerNolPaymentCard(cardNumber: "1234567890123456", expiredTime: "") // Replace with the correct initializer
let cardAndPhoneData = NolPayUnlinkCollectableData.cardAndPhoneData(nolPaymentCard: mockCard, mobileNumber: "+1234567890")

// When
sut.updateCollectedData(collectableData: cardAndPhoneData)

// Then
let expectedStep = String(describing: NolPayUnlinkDataStep.collectCardAndPhoneData)
let actualStep = String(describing: sut.nextDataStep)
XCTAssertEqual(actualStep, expectedStep, "The nextDataStep should be .collectCardAndPhoneData after updating with cardAndPhoneData")
}

func testUpdateCollectedDataWithOtpData() {
// Given
let otpData = NolPayUnlinkCollectableData.otpData(otpCode: "123456")

// When
sut.updateCollectedData(collectableData: otpData)

// Then
let expectedStep = String(describing: NolPayUnlinkDataStep.collectOtpData)
let actualStep = String(describing: sut.nextDataStep)
XCTAssertEqual(actualStep, expectedStep, "The nextDataStep should be .collectOtpData after updating with otpData")
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public class NolPayLinkCardComponent: PrimerHeadlessCollectDataComponent {

switch collectableData {
case .phoneData(let mobileNumber):
nextDataStep = .collectPhoneData(cardNumber: self.cardNumber ?? "")
self.mobileNumber = mobileNumber
case .otpData(let otpCode):
nextDataStep = .collectOtpData(phoneNumber: self.mobileNumber ?? "")
self.otpCode = otpCode
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class NolPayPaymentComponent: PrimerHeadlessCollectDataComponent {
public func updateCollectedData(collectableData: NolPayPaymentCollectableData) {
switch collectableData {
case let .paymentData(cardNumber, mobileNumber):
nextDataStep = .collectCardAndPhoneData
self.cardNumber = cardNumber
self.mobileNumber = mobileNumber
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public class NolPayUnlinkCardComponent: PrimerHeadlessCollectDataComponent {

case .cardAndPhoneData(nolPaymentCard: let nolPaymentCard,
mobileNumber: let mobileNumber):

nextDataStep = .collectCardAndPhoneData
cardNumber = nolPaymentCard.cardNumber
self.mobileNumber = mobileNumber

case .otpData(otpCode: let otpCode):
nextDataStep = .collectOtpData
self.otpCode = otpCode
}

Expand Down

0 comments on commit 4ae5ab9

Please sign in to comment.