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