🚚 :: Restructure test cases

They are now in folders with the same name as the features
This commit is contained in:
Felix Mau
2022-08-18 21:55:15 +02:00
parent d979f65b80
commit 0ec26dfee9
10 changed files with 67 additions and 46 deletions
@@ -0,0 +1,208 @@
//
// GradientActivityIndicatorView+AnimateIsHiddenTestCase.swift
// ExampleTests
//
// Created by Felix Mau on 19.05.19.
// Copyright © 2017 Felix Mau. All rights reserved.
//
import XCTest
@testable import GradientLoadingBar
// swiftlint:disable:next type_name
final class GradientActivityIndicatorView_AnimateIsHiddenTestCase: XCTestCase {
// MARK: - Private properties
private var window: UIWindow!
private var gradientActivityIndicatorView: GradientActivityIndicatorView!
// MARK: - Public methods
override func setUp() {
super.setUp()
// In order for UIView animations to be executed correctly, the corresponding view has to be attached to a visible window.
// Therefore we're gonna use the current key-window, add our testing view here in `setUp()` and remove it later in `tearDown()`.
window = UIApplication.shared.keyWindowInConnectedScenes
gradientActivityIndicatorView = GradientActivityIndicatorView()
window.addSubview(gradientActivityIndicatorView)
}
override func tearDown() {
gradientActivityIndicatorView.removeFromSuperview()
gradientActivityIndicatorView = nil
window = nil
super.tearDown()
}
// MARK: - Test method `animate(isHidden:)`
func test_animateIsHidden_shouldShowView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// Hide view to validate fade-in.
gradientActivityIndicatorView.alpha = 0
gradientActivityIndicatorView.isHidden = true
// When
gradientActivityIndicatorView.animate(isHidden: false, duration: 0.1) { isFinished in
XCTAssertTrue(isFinished)
expectation.fulfill()
}
// Then
wait(for: [expectation], timeout: 1)
XCTAssertFalse(gradientActivityIndicatorView.isHidden)
XCTAssertEqual(gradientActivityIndicatorView.alpha, 1, accuracy: CGFloat.ulpOfOne)
}
func test_animateIsHidden_withInterruption_shouldShowView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// Hide view to validate fade-in.
gradientActivityIndicatorView.alpha = 0
gradientActivityIndicatorView.isHidden = true
// When
gradientActivityIndicatorView.animate(isHidden: false, duration: 0.1) { isFinished in
XCTAssertFalse(isFinished)
expectation.fulfill()
}
// Cancel animation.
gradientActivityIndicatorView.layer.removeAllAnimations()
// Then
wait(for: [expectation], timeout: 1)
XCTAssertFalse(gradientActivityIndicatorView.isHidden)
}
func test_animateIsHidden_shouldHideView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// When
gradientActivityIndicatorView.animate(isHidden: true, duration: 0.1) { isFinished in
XCTAssertTrue(isFinished)
expectation.fulfill()
}
// Then
wait(for: [expectation], timeout: 1)
XCTAssertTrue(gradientActivityIndicatorView.isHidden)
XCTAssertEqual(gradientActivityIndicatorView.alpha, 0, accuracy: CGFloat.ulpOfOne)
}
func test_animateIsHidden_withInterruption_shouldNotHideView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// When
gradientActivityIndicatorView.animate(isHidden: true, duration: 0.1) { isFinished in
XCTAssertFalse(isFinished)
expectation.fulfill()
}
// Cancel animation.
gradientActivityIndicatorView.layer.removeAllAnimations()
// Then
wait(for: [expectation], timeout: 1)
XCTAssertFalse(gradientActivityIndicatorView.isHidden, "As we've interrupted the animation, we expect the `isHidden` flag to still be `false`.")
}
// MARK: - Test method `fadeIn()`
func test_fadeIn_shouldShowView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// Hide view to validate fade-in.
gradientActivityIndicatorView.alpha = 0
gradientActivityIndicatorView.isHidden = true
// When
gradientActivityIndicatorView.fadeIn(duration: 0.1) { isFinished in
XCTAssertTrue(isFinished)
expectation.fulfill()
}
// Then
wait(for: [expectation], timeout: 1)
XCTAssertFalse(gradientActivityIndicatorView.isHidden)
XCTAssertEqual(gradientActivityIndicatorView.alpha, 1, accuracy: CGFloat.ulpOfOne)
}
func test_fadeIn_withInterruption_shouldShowView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// Hide view to validate fade-in.
gradientActivityIndicatorView.alpha = 0
gradientActivityIndicatorView.isHidden = true
// When
gradientActivityIndicatorView.fadeIn(duration: 0.1) { isFinished in
XCTAssertFalse(isFinished)
expectation.fulfill()
}
// Cancel animation.
gradientActivityIndicatorView.layer.removeAllAnimations()
// Then
wait(for: [expectation], timeout: 1)
XCTAssertFalse(gradientActivityIndicatorView.isHidden)
}
// MARK: - Test method `fadeOut()`
func test_fadeOut_shouldHideView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// When
gradientActivityIndicatorView.fadeOut(duration: 0.1) { isFinished in
XCTAssertTrue(isFinished)
expectation.fulfill()
}
// Then
wait(for: [expectation], timeout: 1)
XCTAssertTrue(gradientActivityIndicatorView.isHidden)
XCTAssertEqual(gradientActivityIndicatorView.alpha, 0, accuracy: CGFloat.ulpOfOne)
}
func test_fadeOut_withInterruption_shouldNotHideView_andCallCompletionHandler() {
// Given
let expectation = expectation(description: "Expect completion handler to be invoked.")
// When
gradientActivityIndicatorView.fadeOut(duration: 0.1) { isFinished in
XCTAssertFalse(isFinished)
expectation.fulfill()
}
// Cancel animation.
gradientActivityIndicatorView.layer.removeAllAnimations()
// Then
wait(for: [expectation], timeout: 1)
XCTAssertFalse(gradientActivityIndicatorView.isHidden, "As we've interrupted the animation, we expect the `isHidden` flag to still be `false`.")
}
}
@@ -0,0 +1,215 @@
//
// GradientActivityIndicatorViewModelTestCase.swift
// ExampleTests
//
// Created by Felix Mau on 26.08.19.
// Copyright © 2019 Felix Mau. All rights reserved.
//
import LightweightObservable
import XCTest
@testable import GradientLoadingBar
// swiftlint:disable:next type_name
final class GradientActivityIndicatorViewModelTestCase: XCTestCase {
// MARK: - Private properties
private var viewModel: GradientActivityIndicatorViewModel!
// MARK: - Public methods
override func setUp() {
super.setUp()
viewModel = GradientActivityIndicatorViewModel()
}
override func tearDown() {
viewModel = nil
super.tearDown()
}
// MARK: - Test property `isHidden`
func test_settingIsHidden_toTrue_shouldUpdateIsAnimatingSubject_withFalse() throws {
// Given
var receivedIsAnimating: Bool?
let disposable = viewModel.isAnimating.subscribe { isAnimating, _ in
receivedIsAnimating = isAnimating
}
// When
withExtendedLifetime(disposable) {
viewModel.isHidden = true
}
// Then
XCTAssertFalse(
try XCTUnwrap(receivedIsAnimating, "Expected to have received a value from the subscription closure at this point.")
)
}
func test_settingIsHidden_toFalse_shouldUpdateIsAnimatingSubject_withTrue() throws {
// Given
var receivedIsAnimating: Bool?
let disposable = viewModel.isAnimating.subscribe { isAnimating, _ in
receivedIsAnimating = isAnimating
}
// When
withExtendedLifetime(disposable) {
viewModel.isHidden = false
}
// Then
XCTAssertTrue(
try XCTUnwrap(receivedIsAnimating, "Expected to have received a value from the subscription closure at this point.")
)
}
// MARK: - Test property `bounds`
func test_settingBounds_shouldUpdateGradientLayerSizeUpdate() {
// Given
var receivedSizeUpdate: GradientActivityIndicatorViewModel.SizeUpdate?
let disposable = viewModel.gradientLayerSizeUpdate.subscribe { sizeUpdate, _ in
receivedSizeUpdate = sizeUpdate
}
let size = CGSize(width: .random(in: 1 ... 100), height: .random(in: 1 ... 100))
let bounds = CGRect(origin: .zero, size: size)
// When
withExtendedLifetime(disposable) {
viewModel.bounds = bounds
}
// Then
let expectedSizeUpdate = GradientActivityIndicatorViewModel.SizeUpdate(bounds: bounds)
XCTAssertEqual(receivedSizeUpdate, expectedSizeUpdate)
}
func test_settingBounds_shouldRestartAnimation() {
// Given
var receivedIsAnimating = [Bool]()
let disposable = viewModel.isAnimating.subscribe { isAnimating, _ in
receivedIsAnimating.append(isAnimating)
}
receivedIsAnimating.removeAll()
let size = CGSize(width: .random(in: 1 ... 100), height: .random(in: 1 ... 100))
let bounds = CGRect(origin: .zero, size: size)
// When
withExtendedLifetime(disposable) {
viewModel.bounds = bounds
}
// Then
let expectedIsAnimating = [false, true]
XCTAssertEqual(receivedIsAnimating, expectedIsAnimating)
}
func test_settingBounds_shouldNotRestartAnimation_dueToViewIsHidden() {
// Given
var receivedIsAnimating = [Bool]()
let disposable = viewModel.isAnimating.subscribe { isAnimating, _ in
receivedIsAnimating.append(isAnimating)
}
let size = CGSize(width: .random(in: 1 ... 100), height: .random(in: 1 ... 100))
let bounds = CGRect(origin: .zero, size: size)
viewModel.isHidden = true
receivedIsAnimating.removeAll()
// When
withExtendedLifetime(disposable) {
viewModel.bounds = bounds
}
// Then
XCTAssertTrue(receivedIsAnimating.isEmpty)
}
// MARK: - Test property `gradientColors`
func test_settingGradientColors_shouldUpdateGradientLayerColors() {
// Given
var receivedGradientLayerColors: [CGColor]?
let disposable = viewModel.gradientLayerColors.subscribe { gradientLayerColors, _ in
receivedGradientLayerColors = gradientLayerColors
}
let gradientColors: [UIColor] = [.red, .yellow, .green]
// When
withExtendedLifetime(disposable) {
viewModel.gradientColors = gradientColors
}
// Then
let expectedGradientLayerColors = [UIColor.red, .yellow, .green, .yellow, .red, .yellow, .green].map(\.cgColor)
XCTAssertEqual(receivedGradientLayerColors, expectedGradientLayerColors)
}
// MARK: - Test property `progressAnimationDuration`
func test_settingProgressAnimationDuration_shouldUpdateGradientLayerAnimationDuration() {
// Given
var receivedGradientLayerAnimationDuration: TimeInterval?
let disposable = viewModel.gradientLayerAnimationDuration.subscribe { gradientLayerAnimationDuration, _ in
receivedGradientLayerAnimationDuration = gradientLayerAnimationDuration
}
let progressAnimationDuration: TimeInterval = .random(in: 0 ... 100)
// When
withExtendedLifetime(disposable) {
viewModel.progressAnimationDuration = progressAnimationDuration
}
// Then
XCTAssertEqual(receivedGradientLayerAnimationDuration, progressAnimationDuration)
}
func test_settingProgressAnimationDuration_shouldRestartAnimation() {
// Given
var receivedIsAnimating = [Bool]()
let disposable = viewModel.isAnimating.subscribe { isAnimating, _ in
receivedIsAnimating.append(isAnimating)
}
receivedIsAnimating.removeAll()
// When
withExtendedLifetime(disposable) {
viewModel.progressAnimationDuration = .random(in: 0 ... 100)
}
// Then
let expectedIsAnimating = [false, true]
XCTAssertEqual(receivedIsAnimating, expectedIsAnimating)
}
func test_settingProgressAnimationDuration_shouldNotRestartAnimation_dueToViewIsHidden() {
// Given
var receivedIsAnimating = [Bool]()
let disposable = viewModel.isAnimating.subscribe { isAnimating, _ in
receivedIsAnimating.append(isAnimating)
}
viewModel.isHidden = true
receivedIsAnimating.removeAll()
// When
withExtendedLifetime(disposable) {
viewModel.progressAnimationDuration = .random(in: 0 ... 100)
}
// Then
XCTAssertTrue(receivedIsAnimating.isEmpty)
}
}