Im new in writing testcases, so please do not refrain from any suggestions.
Problem - How do i write testcase for (closures/ higher order functions) not the api calling closures excluding them, inorder to get 100% coverage, im stuck only in this part.
CODE -
init () {
var items:[ReturnPaymentsOptionsModel] = []
items.append(ReturnPaymentsOptionsModel(text: "Unexpected payment or \nunknown sender", isSelected: false))
items.append(ReturnPaymentsOptionsModel(text: "Incorrect currency", isSelected: false))
items.append(ReturnPaymentsOptionsModel(text: "Incorrect amount", isSelected: false))
items.append(ReturnPaymentsOptionsModel(text: "Duplicate", isSelected: false))
items.append(ReturnPaymentsOptionsModel(text: "Auction/Order cancelled", isSelected: false))
items.append(ReturnPaymentsOptionsModel(text: "Charges exceeds the principle amount", isSelected: false))
let observableToggleItems = Observable.just(items)
let tappedItemAction = Observable.combineLatest(observableToggleItems, itemTapped)
.map { argument -> [ReturnPaymentsOptionsModel] in // for this closure im stuck
let (items, indexPath) = argument
var modifiedItems = items
for index in modifiedItems.indices {
if index == indexPath.row {
modifiedItems[index].isSelected.toggle()
} else {
modifiedItems[index].isSelected = false
}
}
return modifiedItems
}.startWith(items)
didFetchToggleItems = Observable.merge(getToggleItems.withLatestFrom(observableToggleItems), tappedItemAction)
}
I'll assume you don't have issues with testing the Rx chain here, but with the closure itself.
Then it would be better if you extracted that entire closure to a function you can unit test directly, and then just call the function in the closure in the .map, something like:
function modifyItems(argument: ([Item], IndexPath)) -> [ITTReturnPaymentsOptionsModel] {
// Your closure here
}
And then something like:
let tappedItemAction = Observable.combineLatest(observableToggleItems, itemTapped)
.map { modifyItems($0) }
.startWith(items)
This will allow you to unit test the function separately from the Rx chain.