Skip to content

Commit

Permalink
Fix issues with BankComponentTests
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewc committed Apr 19, 2024
1 parent 63f908a commit 6ef8879
Showing 1 changed file with 58 additions and 42 deletions.
100 changes: 58 additions & 42 deletions Debug App/Tests/Unit Tests/Primer/Ideal/BankComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,21 @@ import XCTest

final class BankComponentTests: XCTestCase {

var banks: [IssuingBank] = []
var steps: [BanksStep] = []
let expectationTimeout = 5.0

var validationErrors: [String] = []
var validationStatuses: [String] = []
var webRedirectComponent: WebRedirectComponent?

var mockSteppableDelegate: MockSteppableDelegate!

override func setUp() {
super.setUp()
cleanup()
mockSteppableDelegate = MockSteppableDelegate()
}

override func tearDown() {
super.tearDown()
cleanup()
}

func cleanup() {
webRedirectComponent = nil
banks.removeAll()
steps.removeAll()
mockSteppableDelegate = nil
validationErrors.removeAll()
validationStatuses.removeAll()
}
Expand Down Expand Up @@ -72,18 +67,18 @@ final class BankComponentTests: XCTestCase {
redirectExpectation.fulfill()
return self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.stepDelegate = mockSteppableDelegate
XCTAssertNil(bankComponent.bankId)
bankComponent.start()
let banksRetrievedExpectation = expectation(description: "banks_retrieved")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
XCTAssertEqual(self.banks.map { $0.name }, mockModel.mockBanks.map { $0.name })
mockSteppableDelegate.onReceiveBanks = { banks in
XCTAssertEqual(banks.map { $0.name }, mockModel.mockBanks.map { $0.name })
bankComponent.updateCollectedData(collectableData: BanksCollectableData.bankId(bankId: bankId))
XCTAssertEqual(bankComponent.bankId, bankId)
bankComponent.submit()
banksRetrievedExpectation.fulfill()
}
waitForExpectations(timeout: 10)
waitForExpectations(timeout: self.expectationTimeout)
XCTAssertNotNil(webRedirectComponent)
}

Expand All @@ -92,43 +87,57 @@ final class BankComponentTests: XCTestCase {
let bankComponent = DefaultBanksComponent(paymentMethodType: .adyenIDeal, tokenizationProvidingModel: mockModel) {
self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.stepDelegate = mockSteppableDelegate
bankComponent.start()
let expectation = expectation(description: "banks_retrieved")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
XCTAssertEqual(self.banks.map { $0.name }, mockModel.mockBanks.map { $0.name })
XCTAssertEqual(self.banks.map { $0.id }, mockModel.mockBanks.map { $0.id })
XCTAssertEqual(self.steps, [.loading, .banksRetrieved(banks: mockModel.mockBanks.map { IssuingBank(bank: $0) })])
mockSteppableDelegate.onReceiveBanks = { banks in
XCTAssertEqual(banks.map { $0.name }, mockModel.mockBanks.map { $0.name })
XCTAssertEqual(banks.map { $0.id }, mockModel.mockBanks.map { $0.id })
XCTAssertEqual(self.mockSteppableDelegate.steps, [.loading, .banksRetrieved(banks: mockModel.mockBanks.map { IssuingBank(bank: $0) })])
expectation.fulfill()
}
waitForExpectations(timeout: 10)
waitForExpectations(timeout: self.expectationTimeout)
}

func testFilterBanks() {
let mockModel = MockBankSelectorTokenizationModel(paymentMethodType: .adyenIDeal)
let bankComponent = DefaultBanksComponent(paymentMethodType: .adyenIDeal, tokenizationProvidingModel: mockModel) {
self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.start()
bankComponent.stepDelegate = mockSteppableDelegate

let firstBanksResult = mockModel.mockBanks.map { IssuingBank(bank: $0) }
let secondBanksResult = mockModel.mockBanks.filter {
$0.name == MockBankSelectorTokenizationModel.bankNameToBeFiltered
}.map { IssuingBank(bank: $0) }

let expectation = expectation(description: "banks_filtered")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
XCTAssertEqual(self.banks.map { $0.name }, mockModel.mockBanks.map { $0.name })
mockSteppableDelegate.onReceiveBanks = { banks in
XCTAssertEqual(banks.map { $0.name }, mockModel.mockBanks.map { $0.name })
self.mockSteppableDelegate.onReceiveBanks = { banks in
XCTAssertTrue(mockModel.didCallFilter)
XCTAssertEqual(banks.map { $0.name }, [MockBankSelectorTokenizationModel.bankNameToBeFiltered])
XCTAssertEqual(self.mockSteppableDelegate.steps, [
.loading,
.banksRetrieved(banks: firstBanksResult),
.banksRetrieved(banks: secondBanksResult)
])
expectation.fulfill()
}
bankComponent.updateCollectedData(collectableData: BanksCollectableData.bankFilterText(text: "filter_query"))
XCTAssertTrue(mockModel.didCallFilter)
XCTAssertEqual(self.banks.map { $0.name }, [MockBankSelectorTokenizationModel.bankNameToBeFiltered])
expectation.fulfill()
XCTAssertEqual(self.steps, [.loading, .banksRetrieved(banks: mockModel.mockBanks.map { IssuingBank(bank: $0) }), .banksRetrieved(banks: mockModel.mockBanks.filter { $0.name == MockBankSelectorTokenizationModel.bankNameToBeFiltered }.map { IssuingBank(bank: $0) })])
}
waitForExpectations(timeout: 10)

bankComponent.start()

waitForExpectations(timeout: self.expectationTimeout)
}

func testValidationNoBanksAtSelection() {
let mockModel = MockBankSelectorTokenizationModel(paymentMethodType: .adyenIDeal)
let bankComponent = DefaultBanksComponent(paymentMethodType: .adyenIDeal, tokenizationProvidingModel: mockModel) {
self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.stepDelegate = mockSteppableDelegate
bankComponent.validationDelegate = self
bankComponent.updateCollectedData(collectableData: BanksCollectableData.bankId(bankId: "mock_id"))
XCTAssertEqual(validationStatuses, ["validating", "invalid"])
Expand All @@ -140,7 +149,7 @@ final class BankComponentTests: XCTestCase {
let bankComponent = DefaultBanksComponent(paymentMethodType: .adyenIDeal, tokenizationProvidingModel: mockModel) {
self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.stepDelegate = mockSteppableDelegate
bankComponent.validationDelegate = self
bankComponent.updateCollectedData(collectableData: BanksCollectableData.bankFilterText(text: "mock_query"))
XCTAssertEqual(validationStatuses, ["validating", "invalid"])
Expand All @@ -152,43 +161,49 @@ final class BankComponentTests: XCTestCase {
let bankComponent = DefaultBanksComponent(paymentMethodType: .adyenIDeal, tokenizationProvidingModel: mockModel) {
self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.stepDelegate = mockSteppableDelegate
bankComponent.validationDelegate = self
bankComponent.stepDelegate = self
bankComponent.start()
let expectation = expectation(description: "banks_retrieved")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
mockSteppableDelegate.onReceiveBanks = { _ in
bankComponent.updateCollectedData(collectableData: BanksCollectableData.bankId(bankId: "0"))
XCTAssertEqual(self.validationStatuses, ["validating", "valid"])
XCTAssertTrue(self.validationErrors.isEmpty)
expectation.fulfill()
}
waitForExpectations(timeout: 10)

}
waitForExpectations(timeout: self.expectationTimeout)
}

func testValidationForInvalidBankId() {
let mockModel = MockBankSelectorTokenizationModel(paymentMethodType: .adyenIDeal)
let bankComponent = DefaultBanksComponent(paymentMethodType: .adyenIDeal, tokenizationProvidingModel: mockModel) {
self.webRedirectComponent(tokenizationModelDelegate: mockModel)
}
bankComponent.stepDelegate = self
bankComponent.stepDelegate = mockSteppableDelegate
bankComponent.validationDelegate = self
bankComponent.stepDelegate = self
bankComponent.start()
let expectation = expectation(description: "banks_retrieved")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
mockSteppableDelegate.onReceiveBanks = { _ in
bankComponent.updateCollectedData(collectableData: BanksCollectableData.bankId(bankId: "mock_bank_id"))
XCTAssertEqual(self.validationStatuses, ["validating", "invalid"])
XCTAssertEqual(self.validationErrors, ["Please provide a valid bank id"])
expectation.fulfill()
}
waitForExpectations(timeout: 10)
waitForExpectations(timeout: self.expectationTimeout)

}
}

extension BankComponentTests: PrimerHeadlessSteppableDelegate {
class MockSteppableDelegate: PrimerHeadlessSteppableDelegate {

var banks: [IssuingBank] = []
var steps: [BanksStep] = []

var onReceiveStep: ((PrimerHeadlessStep) -> Void)?

var onReceiveBanks: (([IssuingBank]) -> Void)?

func didReceiveStep(step: PrimerHeadlessStep) {
guard let step = step as? BanksStep else {
return
Expand All @@ -197,6 +212,7 @@ extension BankComponentTests: PrimerHeadlessSteppableDelegate {
switch step {
case .loading: break
case .banksRetrieved(banks: let banks):
self.onReceiveBanks?(banks)
self.banks = banks
}
}
Expand Down

0 comments on commit 6ef8879

Please sign in to comment.