Skip to content

Commit 556f9e9

Browse files
committed
Merge branch 'main' of github.com:liveviewnative/liveview-client-swiftui into naming-convention
2 parents eb2d10a + 4a0e032 commit 556f9e9

File tree

7 files changed

+91
-9
lines changed

7 files changed

+91
-9
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [master]
7+
8+
jobs:
9+
test:
10+
runs-on: macos-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Run tests on iOS Simulator
14+
shell: bash
15+
run: |
16+
sudo xcode-select --switch /Applications/Xcode_14.2.app
17+
xcodebuild test -scheme LiveViewNative -sdk iphonesimulator16.2 -destination "OS=16.2,name=iPhone 14 Pro"

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/LiveViewNative/Views/Images/Image.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Image: View {
4646
}
4747

4848
private var symbolScale: SwiftUI.Image.Scale? {
49-
switch element.attributeValue(for: "symbol-scasle") {
49+
switch element.attributeValue(for: "symbol-scale") {
5050
case nil:
5151
return nil
5252
case "small":

Tests/RenderingTests/LinkTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// File.swift
2+
// LinkTests.swift
33
//
44
//
55
// Created by Carson Katri on 1/17/23.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// TextFieldTests.swift
3+
//
4+
//
5+
// Created by Carson Katri on 1/19/23.
6+
//
7+
8+
import XCTest
9+
import SwiftUI
10+
@testable import LiveViewNative
11+
12+
@MainActor
13+
final class TextFieldTests: XCTestCase {
14+
func testSimple() throws {
15+
try assertMatch(#"<textfield placeholder="Type here" />"#) {
16+
TextField("Type here", text: .constant(""))
17+
}
18+
try assertMatch(#"<securefield placeholder="Password" />"#) {
19+
SecureField("Password", text: .constant(""))
20+
}
21+
}
22+
func testPrompt() throws {
23+
try assertMatch(#"<textfield placeholder="Placeholder" prompt="Prompt" />"#) {
24+
TextField("Placeholder", text: .constant(""), prompt: Text("Prompt"))
25+
}
26+
try assertMatch(#"<securefield placeholder="Placeholder" prompt="Prompt" />"#) {
27+
SecureField("Placeholder", text: .constant(""), prompt: Text("Prompt"))
28+
}
29+
}
30+
}

Tests/RenderingTests/TextTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ This is some markdown text [click me](apple.com)
9696
}
9797

9898
func testFormat() throws {
99-
try assertMatch(#"<text format="date-time" value="2023-01-17" />"#) {
100-
Text(Date(timeIntervalSince1970: 1673931600.0), format: .dateTime)
99+
try assertMatch(#"<text format="date-time" value="0001-01-01T00:00:00.000Z" />"#) {
100+
Text(Date.distantPast, format: .dateTime)
101101
}
102102
try assertMatch(#"<text format="url">apple.com</text>"#) {
103103
Text(URL(string: "apple.com")!, format: .url)
104104
}
105-
try assertMatch(#"<text format="iso8601" value="2023-01-17T14:55:01.326Z" />"#) {
106-
Text(Date(timeIntervalSince1970: 1673967301.325973), format: .iso8601)
105+
try assertMatch(#"<text format="iso8601" value="0001-01-01T00:00:00.000Z" />"#) {
106+
Text(Date.distantPast, format: .iso8601)
107107
}
108108
try assertMatch(#"<text format="number" value="0.42" />"#) {
109109
Text(0.42, format: .number)

Tests/RenderingTests/assertMatch.swift

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ func assertMatch(
3838
document[document.root()].children(),
3939
context: LiveContext(coordinator: session.rootCoordinator, url: session.url)
4040
).environment(\.coordinatorEnvironment, CoordinatorEnvironment(session.rootCoordinator, document: document))
41-
let markupImage = ImageRenderer(content: viewTree.transformEnvironment(\.self, transform: environment).frame(width: size?.width, height: size?.height)).uiImage?.pngData()
42-
let viewImage = ImageRenderer(content: view().transformEnvironment(\.self, transform: environment).frame(width: size?.width, height: size?.height)).uiImage?.pngData()
41+
42+
let markupImage = snapshot(
43+
viewTree
44+
.transformEnvironment(\.self, transform: environment),
45+
size: size
46+
)?.pngData()
47+
let viewImage = snapshot(
48+
view()
49+
.transformEnvironment(\.self, transform: environment),
50+
size: size
51+
)?.pngData()
4352

4453
if markupImage == viewImage {
4554
XCTAssert(true)
@@ -51,3 +60,29 @@ func assertMatch(
5160
XCTAssert(false, "Rendered views did not match. Outputs saved to \(markupURL.path()) and \(viewURL.path())")
5261
}
5362
}
63+
64+
private class SnapshotWindow: UIWindow {
65+
override var safeAreaInsets: UIEdgeInsets {
66+
.zero
67+
}
68+
}
69+
70+
@MainActor
71+
private func snapshot(_ view: some View, size: CGSize?) -> UIImage? {
72+
73+
let controller = UIHostingController(rootView: view)
74+
75+
let uiView = controller.view!
76+
uiView.bounds = .init(origin: .zero, size: size ?? uiView.intrinsicContentSize)
77+
uiView.backgroundColor = .clear
78+
79+
let window = SnapshotWindow(frame: uiView.bounds)
80+
window.rootViewController = controller
81+
window.isHidden = false
82+
window.makeKeyAndVisible()
83+
84+
let renderer = UIGraphicsImageRenderer(size: uiView.bounds.size)
85+
return renderer.image { context in
86+
uiView.layer.render(in: context.cgContext)
87+
}
88+
}

0 commit comments

Comments
 (0)