Skip to content

Instantly share code, notes, and snippets.

@ykpoh
Last active July 24, 2021 12:07
Show Gist options
  • Save ykpoh/be3e954338ddd60f28179e67c13542a1 to your computer and use it in GitHub Desktop.
Save ykpoh/be3e954338ddd60f28179e67c13542a1 to your computer and use it in GitHub Desktop.
import XCTest
@testable import SpaceXLaunch
class LaunchListTableViewCellViewModelTests: XCTestCase {
// 1
var sut: LaunchListTableViewCellViewModel!
var cell: LaunchListTableViewCell!
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
try super.setUpWithError()
whenSUTSetFromLaunch() // 2
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
cell = nil
sut = nil
try super.tearDownWithError()
}
func testConfigureCell_setsLaunchLabelText() {
// when
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.launchNumberLabel.text, sut.launchNumber.value)
}
func testConfigureCell_setsDetailLabelText() {
// when
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.detailLabel.text, sut.detail.value)
}
func testHideDetailLabel_givenDetailsIsNil_whenConfigureCell() {
// when
whenSUTSetFromLaunch(details: nil)
whenConfigureLaunchListTableViewCell()
// then
XCTAssertTrue(cell.detailLabel.isHidden)
}
func testConfigureCell_setsDateTimeLabelText() {
// when
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.dateLabel.text, sut.dateTime.value)
}
func testConfigureCell_setsStatusLabelText() {
// when
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.statusLabel.text, sut.statusString.value)
}
func testStatusLabelTextColor_givenUpcomingIsTrue_whenConfigureCell() {
// when
whenSUTSetFromLaunch(upcoming: true)
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.statusLabel.textColor, .brown)
}
func testStatusLabelTextColor_givenSuccessIsTrue_whenConfigureCell() {
// when
whenSUTSetFromLaunch(success: true)
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.statusLabel.textColor, .green)
}
func testStatusLabelTextColor_givenBothUpcomingAndSuccessAreFalse_whenConfigureCell() {
// when
whenConfigureLaunchListTableViewCell()
// then
XCTAssertEqual(cell.statusLabel.textColor, .red)
}
func whenSUTSetFromLaunch(
id: String = "id",
name: String = "name",
details: String? = "details",
date_utc: Date = Date().addingTimeInterval(100),
upcoming: Bool = false,
success: Bool = false,
rocket: String = "rocket") {
sut = LaunchListTableViewCellViewModel(launch: Launch(id: id, name: name, details: details, date_utc: date_utc, upcoming: upcoming, success: success, rocket: rocket))
}
// 3
func givenLaunchListTableViewCell() {
let viewController = LaunchListViewController.instanceFromStoryboard()
viewController.loadViewIfNeeded()
let tableView = viewController.tableView!
cell = tableView.dequeueReusableCell(withIdentifier: "\(LaunchListTableViewCell.self)") as? LaunchListTableViewCell
}
// 4
// MARK: - When
func whenConfigureLaunchListTableViewCell() {
givenLaunchListTableViewCell()
sut.configure(cell)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment