Skip to content

Commit

Permalink
Make snapshot tests pass for SDWebImageSwiftUI after version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-pod committed Mar 29, 2023
1 parent 7a6a5cc commit 11abca6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Tests: XCTestCase {
func testMultipleSwiftUIImagesViewController() throws {
let viewController = SDWebImageMockPlugin_Example.SwiftUIHostingViewController()

prepareViewControllerForSDWebImageSwiftUIAppearanceHack(viewController)
assertSnapshot(matching: viewController, as: .image(on: .iPhoneX))
}

Expand All @@ -68,6 +69,23 @@ class Tests: XCTestCase {
(URL(string: "http://127.0.0.1"), nil)
])

prepareViewControllerForSDWebImageSwiftUIAppearanceHack(viewController)
assertSnapshot(matching: viewController, as: .image(on: .iPhoneX))
}
}

extension XCTestCase {
// SDWebImageSwiftUI is using a hack to bypass strange behavior in SwiftUI with `onAppear` and `onDisappear`.
// The hack uses UIKit to have more reliable appearance management, to have the image loaded instantly
// we need to add the WebImage to a window and wait for an async closure on main thread to complete.
func prepareViewControllerForSDWebImageSwiftUIAppearanceHack(_ viewController: UIViewController) {
let fakeWindow = UIWindow()
fakeWindow.rootViewController = viewController
fakeWindow.isHidden = false
let expectation = XCTestExpectation()
DispatchQueue.main.async {
expectation.fulfill()
}
wait(for: [expectation], timeout: 1.0)
}
}

1 comment on commit 11abca6

@kpacholak
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executed 1 test, with 0 failures (0 unexpected) in 0.246 (0.250) seconds 👌

Please sign in to comment.