|
| 1 | +import XCTest |
| 2 | +import BuilderIO |
| 3 | +import SnapshotTesting |
| 4 | +import UIKit |
| 5 | +import SwiftUI |
| 6 | + |
| 7 | +@MainActor |
| 8 | +class BuilderIOPageViewTests: XCTestCase { |
| 9 | + |
| 10 | + static let record = false |
| 11 | + |
| 12 | + override func setUpWithError() throws { |
| 13 | + BuilderIOManager.configure(apiKey: "UNITTESTINGAPIKEY", customNavigationScheme: "builderio") |
| 14 | + |
| 15 | + continueAfterFailure = false |
| 16 | + } |
| 17 | + |
| 18 | + override func tearDownWithError() throws { |
| 19 | + // Clear all mocks after each test |
| 20 | + print(" 🚨 Tests deregistered") |
| 21 | + |
| 22 | + BuilderIOMockManager.shared.clearAllMocks() |
| 23 | + try super.tearDownWithError() |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + func testTextView() throws { |
| 28 | + BuilderIOMockManager.shared.registerMock(for: "/text", with: "text", statusCode: 200) |
| 29 | + |
| 30 | + let hostingController = makeHostingController(for: "/text", width: 375, height: 812) |
| 31 | + |
| 32 | + let expectation = XCTestExpectation(description: "Wait for view to render") |
| 33 | + |
| 34 | + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { |
| 35 | + |
| 36 | + assertSnapshot(matching: hostingController, as: .image, record: BuilderIOPageViewTests.record) |
| 37 | + expectation.fulfill() |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + wait(for: [expectation], timeout: 3) |
| 42 | + } |
| 43 | + |
| 44 | + func testLayoutsView() throws { |
| 45 | + BuilderIOMockManager.shared.registerMock(for: "/layout", with: "layout", statusCode: 200) |
| 46 | + |
| 47 | + let hostingController = makeHostingController(for: "/layout", width: 375, height: 812) |
| 48 | + |
| 49 | + let expectation = XCTestExpectation(description: "Wait for view to render") |
| 50 | + |
| 51 | + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { |
| 52 | + |
| 53 | + assertSnapshot(matching: hostingController, as: .image, record: BuilderIOPageViewTests.record) |
| 54 | + expectation.fulfill() |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + wait(for: [expectation], timeout: 3) |
| 59 | + } |
| 60 | + |
| 61 | + /// - Returns: A UIHostingController wrapping the SwiftUI Text view. |
| 62 | + func makeHostingController(for url: String, width: CGFloat, height: CGFloat) -> UIHostingController<some View> { |
| 63 | + let view = BuilderIOPage(url: url, onClickEventHandler: { event in |
| 64 | + print("Handle Event Action") |
| 65 | + }) |
| 66 | + |
| 67 | + let hostingController = UIHostingController(rootView: view.frame(width: 375, height: 812)) |
| 68 | + hostingController.view.frame = CGRect(x: 0, y: 0, width: 375, height: 812) |
| 69 | + |
| 70 | + let window = UIWindow(frame: hostingController.view.frame) |
| 71 | + window.rootViewController = hostingController |
| 72 | + window.makeKeyAndVisible() |
| 73 | + |
| 74 | + |
| 75 | + return hostingController |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments