Skip to content

Commit

Permalink
Fix UIViewController lifecycle test
Browse files Browse the repository at this point in the history
  • Loading branch information
lunij committed Mar 24, 2024
1 parent 349fc89 commit d785621
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Sources/SnapshotTesting/Common/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@
viewController.view.layoutIfNeeded()

return {
rootViewController.beginAppearanceTransition(false, animated: false)
viewController.beginAppearanceTransition(false, animated: false)
viewController.willMove(toParent: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParent()
viewController.didMove(toParent: nil)
rootViewController.endAppearanceTransition()
viewController.endAppearanceTransition()
window.rootViewController = nil
}
}
Expand Down
68 changes: 20 additions & 48 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -964,28 +964,14 @@ final class SnapshotTestingTests: XCTestCase {
func testUIViewControllerLifeCycle() {
#if os(iOS)
class ViewController: UIViewController {
let viewDidLoadExpectation: XCTestExpectation
let viewWillAppearExpectation: XCTestExpectation
let viewDidAppearExpectation: XCTestExpectation
let viewWillDisappearExpectation: XCTestExpectation
let viewDidDisappearExpectation: XCTestExpectation
init(
viewDidLoadExpectation: XCTestExpectation,
viewWillAppearExpectation: XCTestExpectation,
viewDidAppearExpectation: XCTestExpectation,
viewWillDisappearExpectation: XCTestExpectation,
viewDidDisappearExpectation: XCTestExpectation
) {
self.viewDidLoadExpectation = viewDidLoadExpectation
self.viewWillAppearExpectation = viewWillAppearExpectation
self.viewDidAppearExpectation = viewDidAppearExpectation
self.viewWillDisappearExpectation = viewWillDisappearExpectation
self.viewDidDisappearExpectation = viewDidDisappearExpectation
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let viewDidLoadExpectation = XCTestExpectation(description: "viewDidLoad")

let viewWillAppearExpectation = XCTestExpectation(description: "viewWillAppear")
let viewDidAppearExpectation = XCTestExpectation(description: "viewDidAppear")

let viewWillDisappearExpectation = XCTestExpectation(description: "viewWillDisappear")
let viewDidDisappearExpectation = XCTestExpectation(description: "viewDidDisappear")

override func viewDidLoad() {
super.viewDidLoad()
viewDidLoadExpectation.fulfill()
Expand All @@ -1008,35 +994,21 @@ final class SnapshotTestingTests: XCTestCase {
}
}

let viewDidLoadExpectation = expectation(description: "viewDidLoad")
let viewWillAppearExpectation = expectation(description: "viewWillAppear")
let viewDidAppearExpectation = expectation(description: "viewDidAppear")
let viewWillDisappearExpectation = expectation(description: "viewWillDisappear")
let viewDidDisappearExpectation = expectation(description: "viewDidDisappear")
viewWillAppearExpectation.expectedFulfillmentCount = 4
viewDidAppearExpectation.expectedFulfillmentCount = 4
viewWillDisappearExpectation.expectedFulfillmentCount = 4
viewDidDisappearExpectation.expectedFulfillmentCount = 4

let viewController = ViewController(
viewDidLoadExpectation: viewDidLoadExpectation,
viewWillAppearExpectation: viewWillAppearExpectation,
viewDidAppearExpectation: viewDidAppearExpectation,
viewWillDisappearExpectation: viewWillDisappearExpectation,
viewDidDisappearExpectation: viewDidDisappearExpectation
)
let viewController = ViewController()
viewController.viewWillAppearExpectation.expectedFulfillmentCount = 2
viewController.viewDidAppearExpectation.expectedFulfillmentCount = 2
viewController.viewWillDisappearExpectation.expectedFulfillmentCount = 1
viewController.viewDidDisappearExpectation.expectedFulfillmentCount = 1

assertSnapshot(of: viewController, as: .image)
assertSnapshot(of: viewController, as: .image)

wait(
for: [
viewDidLoadExpectation,
viewWillAppearExpectation,
viewDidAppearExpectation,
viewWillDisappearExpectation,
viewDidDisappearExpectation,
], timeout: 1.0, enforceOrder: true)
wait(for: [
viewController.viewDidLoadExpectation,
viewController.viewWillAppearExpectation,
viewController.viewDidAppearExpectation,
viewController.viewWillDisappearExpectation,
viewController.viewDidDisappearExpectation,
], timeout: 10, enforceOrder: true)
#endif
}

Expand Down

0 comments on commit d785621

Please sign in to comment.