:: Add snapshot tests

This commit is contained in:
Felix Mau
2020-06-15 20:55:48 +02:00
parent 175baf0530
commit 48fdffcbeb
12 changed files with 192 additions and 2 deletions
@@ -28,6 +28,9 @@
97B6F5FE2143F31F008CFEA5 /* UIColor+CustomColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97B6F5FA2143F31F008CFEA5 /* UIColor+CustomColors.swift */; };
97B6F5FF2143F31F008CFEA5 /* BlueFilledButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97B6F5FC2143F31F008CFEA5 /* BlueFilledButton.swift */; };
97B6F6002143F31F008CFEA5 /* BlueBorderedButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97B6F5FD2143F31F008CFEA5 /* BlueBorderedButton.swift */; };
DC190F622496A25A0069E2B0 /* NotchGradientLoadingBarControllerTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC190F602496A24F0069E2B0 /* NotchGradientLoadingBarControllerTestCase.swift */; };
DCE3AB13249761B300889FFA /* Snapshotting+WindowedImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE3AB11249761AB00889FFA /* Snapshotting+WindowedImage.swift */; };
DCE3AB16249764BB00889FFA /* GradientLoadingBarControllerTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE3AB14249764A000889FFA /* GradientLoadingBarControllerTestCase.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -81,6 +84,10 @@
A98CA9F1B0C1BD6864C14E01 /* Pods-GradientLoadingBar_SnapshotTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GradientLoadingBar_SnapshotTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-GradientLoadingBar_SnapshotTests/Pods-GradientLoadingBar_SnapshotTests.release.xcconfig"; sourceTree = "<group>"; };
ABA9B1EF5F3928BA6E31D3EA /* Pods_GradientLoadingBar_SnapshotTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_GradientLoadingBar_SnapshotTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C06CA4DFA1DCB931095ABD3D /* GradientLoadingBar.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = GradientLoadingBar.podspec; path = ../GradientLoadingBar.podspec; sourceTree = "<group>"; };
DC190F602496A24F0069E2B0 /* NotchGradientLoadingBarControllerTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotchGradientLoadingBarControllerTestCase.swift; sourceTree = "<group>"; };
DCE3AB11249761AB00889FFA /* Snapshotting+WindowedImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Snapshotting+WindowedImage.swift"; sourceTree = "<group>"; };
DCE3AB14249764A000889FFA /* GradientLoadingBarControllerTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientLoadingBarControllerTestCase.swift; sourceTree = "<group>"; };
DCE3AB172497670400889FFA /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
E7B292793F03D669ACAA92D2 /* Pods-GradientLoadingBar_SnapshotTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GradientLoadingBar_SnapshotTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-GradientLoadingBar_SnapshotTests/Pods-GradientLoadingBar_SnapshotTests.debug.xcconfig"; sourceTree = "<group>"; };
F76834BE9E43CA11377EC030 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
FECB95A4857728345199461F /* Pods-GradientLoadingBar_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GradientLoadingBar_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-GradientLoadingBar_Tests/Pods-GradientLoadingBar_Tests.debug.xcconfig"; sourceTree = "<group>"; };
@@ -196,9 +203,11 @@
649A163A23770B4400404F3C /* SnapshotTests */ = {
isa = PBXGroup;
children = (
649A163B23770B4400404F3C /* DefaultColorSnapshotTestCase.swift */,
649A164323770E9300404F3C /* CustomColorSnapshotTestCase.swift */,
DCE3AB102497619600889FFA /* _Helper */,
DC190F5E2496A2290069E2B0 /* GradientActivityIndicatorView */,
DC190F5F2496A2380069E2B0 /* GradientLoadingBarController */,
649A163D23770B4400404F3C /* Info.plist */,
DCE3AB172497670400889FFA /* README.md */,
);
path = SnapshotTests;
sourceTree = "<group>";
@@ -261,6 +270,32 @@
name = Frameworks;
sourceTree = "<group>";
};
DC190F5E2496A2290069E2B0 /* GradientActivityIndicatorView */ = {
isa = PBXGroup;
children = (
649A163B23770B4400404F3C /* DefaultColorSnapshotTestCase.swift */,
649A164323770E9300404F3C /* CustomColorSnapshotTestCase.swift */,
);
path = GradientActivityIndicatorView;
sourceTree = "<group>";
};
DC190F5F2496A2380069E2B0 /* GradientLoadingBarController */ = {
isa = PBXGroup;
children = (
DCE3AB14249764A000889FFA /* GradientLoadingBarControllerTestCase.swift */,
DC190F602496A24F0069E2B0 /* NotchGradientLoadingBarControllerTestCase.swift */,
);
path = GradientLoadingBarController;
sourceTree = "<group>";
};
DCE3AB102497619600889FFA /* _Helper */ = {
isa = PBXGroup;
children = (
DCE3AB11249761AB00889FFA /* Snapshotting+WindowedImage.swift */,
);
path = _Helper;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -586,6 +621,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DCE3AB13249761B300889FFA /* Snapshotting+WindowedImage.swift in Sources */,
DC190F622496A25A0069E2B0 /* NotchGradientLoadingBarControllerTestCase.swift in Sources */,
DCE3AB16249764BB00889FFA /* GradientLoadingBarControllerTestCase.swift in Sources */,
649A164423770E9300404F3C /* CustomColorSnapshotTestCase.swift in Sources */,
649A163C23770B4400404F3C /* DefaultColorSnapshotTestCase.swift in Sources */,
);
@@ -0,0 +1,49 @@
//
// GradientLoadingBarControllerTestCase.swift
// GradientLoadingBar_SnapshotTests
//
// Created by Felix Mau on 06/14/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//
import XCTest
import SnapshotTesting
@testable import GradientLoadingBar
class GradientLoadingBarControllerTestCase: XCTestCase {
// MARK: - Private properties
private var gradientLoadingBarController: GradientLoadingBarController!
// MARK: - Public methods
override func setUpWithError() throws {
try super.setUpWithError()
gradientLoadingBarController = GradientLoadingBarController()
gradientLoadingBarController.fadeIn(duration: 0)
}
override func tearDownWithError() throws {
gradientLoadingBarController = nil
try super.tearDownWithError()
}
// MARK: - Tests
func testGradientLoadingBarController() {
// Given
let rootViewController = UIViewController()
let expectation = self.expectation(description: "Waiting for view to appear.")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
expectation.fulfill()
}
// Then
wait(for: [expectation], timeout: 1)
assertSnapshot(matching: rootViewController, as: .windowedImage)
}
}
@@ -0,0 +1,51 @@
//
// NotchGradientLoadingBarControllerTestCase.swift
// GradientLoadingBar_SnapshotTests
//
// Created by Felix Mau on 06/14/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//
import XCTest
import SnapshotTesting
@testable import GradientLoadingBar
class NotchGradientLoadingBarControllerTestCase: XCTestCase {
// swiftlint:disable:previous type_name
// MARK: - Private properties
private var notchGradientLoadingBarController: NotchGradientLoadingBarController!
// MARK: - Public methods
override func setUpWithError() throws {
try super.setUpWithError()
notchGradientLoadingBarController = NotchGradientLoadingBarController()
notchGradientLoadingBarController.fadeIn(duration: 0)
}
override func tearDownWithError() throws {
notchGradientLoadingBarController = nil
try super.tearDownWithError()
}
// MARK: - Tests
func testNotchGradientLoadingBarController() {
// Given
let rootViewController = UIViewController()
let expectation = self.expectation(description: "Waiting for view to appear.")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
expectation.fulfill()
}
// Then
wait(for: [expectation], timeout: 1)
assertSnapshot(matching: rootViewController, as: .windowedImage)
}
}
+4
View File
@@ -0,0 +1,4 @@
# Snapshot-Tests
## Device
Run test-cases with the `iPhone X` using `iOS 13.5`.
@@ -0,0 +1,44 @@
//
// Snapshotting+WindowedImage.swift
// GradientLoadingBar_SnapshotTests
//
// Created by Felix Mau on 06/14/20.
// Copyright © 2020 CocoaPods. All rights reserved.
//
import SnapshotTesting
// MARK: - Config
private enum Config {
/// Allow for a 1% pixel difference.
static let precision: Float = 0.99
}
// MARK: - Public properties
extension Snapshotting where Value: UIViewController, Format == UIImage {
/// Based on:
/// <https://github.com/pointfreeco/swift-snapshot-testing/issues/279> and <https://www.pointfree.co/episodes/ep86-swiftui-snapshot-testing#t657>
static var windowedImage: Snapshotting {
Snapshotting<UIImage, UIImage>.image(precision: Config.precision).asyncPullback { viewController in
Async<UIImage> { callback in
UIView.setAnimationsEnabled(false)
guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else {
fatalError("⚠️ Failed to get key window from application!")
}
keyWindow.rootViewController = viewController
DispatchQueue.main.async {
let image = UIGraphicsImageRenderer(bounds: keyWindow.bounds).image { _ in
keyWindow.drawHierarchy(in: keyWindow.bounds, afterScreenUpdates: true)
}
callback(image)
UIView.setAnimationsEnabled(true)
}
}
}
}
}
@@ -80,6 +80,10 @@ open class GradientLoadingBarController {
bindViewModelToView()
}
deinit {
gradientActivityIndicatorView.removeFromSuperview()
}
// MARK: - Public methods
/// Apply layout constraints for gradient loading view.