Skip to content

Instantly share code, notes, and snippets.

@DhavalDobariya86
Last active June 9, 2020 21:11
Show Gist options
  • Save DhavalDobariya86/857f2d1b6068dda5fdb08cc5874f67c1 to your computer and use it in GitHub Desktop.
Save DhavalDobariya86/857f2d1b6068dda5fdb08cc5874f67c1 to your computer and use it in GitHub Desktop.
MVVM-C ViewModel Unit Tests
import XCTest
@testable import MVVMCDemo
class CountryListViewModelTests: XCTestCase {
func testEndpoint() {
let requestManager = LocalRequestManager()
let sut = CountryListViewModel(requestManager: requestManager)
sut.searchFor(keyword: "test")
XCTAssertEqual(requestManager.endPoint?.urlPath, .name)
XCTAssertEqual(requestManager.endPoint?.pathComponent, "test")
XCTAssertEqual(requestManager.endPoint?.httpMethod, .get)
}
func testLoading() {
let requestManager = LocalRequestManager()
let sut = CountryListViewModel(requestManager: requestManager)
let expectation = XCTestExpectation(description: "didChangeState() for CountryListViewModel should be called.")
var changedStates: [(isLoading: Bool, loadingMessage: String?)] = []
sut.didChangeState = { (isLoading, loadingMessage) in
changedStates.append((isLoading, loadingMessage))
if !isLoading && loadingMessage == nil {
expectation.fulfill()
}
}
sut.searchFor(keyword: "test")
wait(for: [expectation], timeout: 2)
let expectedStates = [(isLoading: true, loadingMessage:"Loading countries..."),
(isLoading: false, loadingMessage: nil)]
XCTAssertEqual(changedStates.map{ $0.isLoading }, expectedStates.map{ $0.isLoading })
XCTAssertEqual(changedStates.map{ $0.loadingMessage }, expectedStates.map{ $0.loadingMessage })
}
func testLoadingErrorDelegate() {
let requestManager = LocalRequestManager()
let sut = CountryListViewModel(requestManager: requestManager)
let expectation = XCTestExpectation(description: "didFailLoading() for CountryListViewModel should be called.")
let loadingErrorDelegate = LoadingErrorDelegateStub()
loadingErrorDelegate.didFailLoadingCompletion = {
expectation.fulfill()
}
sut.loadingErrorDelegate = loadingErrorDelegate
sut.searchFor(keyword: "invalid search string")
wait(for: [expectation], timeout: 2)
}
func testCountryListDatasource() {
let requestManager = LocalRequestManager()
let sut = CountryListViewModel(requestManager: requestManager)
let expectation = XCTestExpectation(description: "didUpdate() for CountryListViewModel should be called.")
sut.didUpdate = {
expectation.fulfill()
let expectedRowCount = 11
XCTAssertEqual(sut.screenTitle, "Countries")
XCTAssertEqual(sut.numberOfSections, 1)
XCTAssertEqual(sut.numberOfRows(in: 0), expectedRowCount)
let range = 0..<expectedRowCount
range.forEach { (row) in
XCTAssertNotNil(sut.country(at: IndexPath(row: row, section: 0)))
_ = sut.itemForRow(at: IndexPath(row: row, section: 0))
}
}
XCTAssertEqual(sut.screenTitle, "Countries")
XCTAssertEqual(sut.numberOfSections, 1)
XCTAssertEqual(sut.numberOfRows(in: 0), 0)
sut.searchFor(keyword: "")
wait(for: [expectation], timeout: 2)
}
func testCountryListDatasourceWithSearchString() {
let requestManager = LocalRequestManager()
let sut = CountryListViewModel(requestManager: requestManager)
let expectation = XCTestExpectation(description: "didUpdate() for CountryListViewModel should be called.")
sut.didUpdate = {
expectation.fulfill()
let expectedRowCount = 5
XCTAssertEqual(sut.screenTitle, "Countries")
XCTAssertEqual(sut.numberOfSections, 1)
XCTAssertEqual(sut.numberOfRows(in: 0), expectedRowCount)
for i in 0..<expectedRowCount {
XCTAssertNotNil(sut.country(at: IndexPath(row: i, section: 0)))
_ = sut.itemForRow(at: IndexPath(row: i, section: 0))
}
}
XCTAssertEqual(sut.screenTitle, "Countries")
XCTAssertEqual(sut.numberOfSections, 1)
XCTAssertEqual(sut.numberOfRows(in: 0), 0)
sut.searchFor(keyword: "ind")
wait(for: [expectation], timeout: 2)
}
}
private class LoadingErrorDelegateStub: CountryListViewModelLoadingErrorDelegate {
var didFailLoadingCompletion: (() -> Void)?
func didFailLoading(error: Error) {
didFailLoadingCompletion?()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment