-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlTests.swift
More file actions
197 lines (182 loc) · 6.77 KB
/
Copy pathControlTests.swift
File metadata and controls
197 lines (182 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// ControlTests.swift
// AndroidSwiftUICoreTests
//
import Testing
@testable import AndroidSwiftUICore
#if canImport(Observation)
import Observation
@Observable final class ObservableModel { var counter = 0; var name = "" }
#endif
@Suite("Controls and environment")
struct ControlTests {
@Test("Slider emits value, bounds, and a working double callback")
func slider() {
struct Screen: View {
@State var value = 0.5
var body: some View { Slider(value: $value, in: 0...1) }
}
let host = ViewHost(Screen())
var node = host.evaluate()
#expect(node.props["value"] == .double(0.5))
if case .int(let id)? = node.props["onChange"] {
host.callbacks.invokeDouble(Int64(id), 0.75)
}
node = host.evaluate()
#expect(node.props["value"] == .double(0.75))
}
@Test("TextField round-trips its binding through the string callback")
func textField() {
struct Screen: View {
@State var name = ""
var body: some View { TextField("Name", text: $name) }
}
let host = ViewHost(Screen())
var node = host.evaluate()
#expect(node.props["text"] == .string(""))
if case .int(let id)? = node.props["onChange"] {
host.callbacks.invokeString(Int64(id), "Coleman")
}
node = host.evaluate()
#expect(node.props["text"] == .string("Coleman"))
}
@Test("Picker emits tagged children and maps the selection string back")
func picker() {
struct Screen: View {
@State var fruit = "Apple"
var body: some View {
Picker("Fruit", selection: $fruit) {
Text("Apple").tag("Apple")
Text("Banana").tag("Banana")
}
}
}
let host = ViewHost(Screen())
var node = host.evaluate()
#expect(node.props["selection"] == .string("Apple"))
#expect(node.children.count == 2)
// each child carries its tag as a modifier
let tags = node.children.compactMap { child -> String? in
guard let tag = child.modifiers.first(where: { $0.kind == "tag" }),
case .string(let value)? = tag.args["value"] else { return nil }
return value
}
#expect(tags == ["Apple", "Banana"])
if case .int(let id)? = node.props["onChange"] {
host.callbacks.invokeString(Int64(id), "Banana")
}
node = host.evaluate()
#expect(node.props["selection"] == .string("Banana"))
}
@Test("Stepper increments and decrements within bounds, updating its label")
func stepper() {
struct Screen: View {
@State var count = 5
var body: some View { Stepper("Count: \(count)", value: $count, in: 0...10) }
}
let host = ViewHost(Screen())
var node = host.evaluate()
#expect(node.type == "Stepper")
if case .int(let inc)? = node.props["onIncrement"] { host.callbacks.invokeVoid(Int64(inc)) }
node = host.evaluate()
#expect(firstTextString(node) == "Count: 6")
if case .int(let dec)? = node.props["onDecrement"] { host.callbacks.invokeVoid(Int64(dec)) }
node = host.evaluate()
#expect(firstTextString(node) == "Count: 5")
}
@Test("SecureField emits a secure TextField node")
func secureField() {
struct Screen: View {
@State var password = ""
var body: some View { SecureField("Password", text: $password) }
}
let node = ViewHost(Screen()).evaluate()
#expect(node.type == "TextField")
#expect(node.props["secure"] == .bool(true))
}
@Test("Menu emits its label and item children")
func menu() {
let node = ViewHost(Menu("Options") {
Button("First") {}
Button("Second") {}
}).evaluate()
#expect(node.type == "Menu")
#expect(node.props["label"] == .string("Options"))
#expect(node.children.count == 2)
}
@Test("Form nests Sections with their header and rows")
func formSection() {
let node = ViewHost(
Form {
Section("General") {
Text("Wi-Fi")
Text("Bluetooth")
}
}
).evaluate()
#expect(node.type == "Form")
let section = node.children.first
#expect(section?.type == "Section")
#expect(section?.props["header"] == .string("General"))
#expect(section?.children.count == 2)
}
@Test("Environment objects reach @Environment properties in the subtree")
func environmentInjection() {
final class Model { var value = 42 }
struct Child: View {
@Environment(Model.self) var model
var body: some View { Text("value \(model.value)") }
}
struct Screen: View {
let model: Model
var body: some View { Child().environment(model) }
}
let node = ViewHost(Screen(model: Model())).evaluate()
#expect(node.props["text"] == .string("value 42"))
}
#if canImport(Observation)
@Test("@Observable mutation triggers the state-change hook")
func observation() {
struct Screen: View {
let model: ObservableModel
var body: some View { Text("count \(model.counter)") }
}
let model = ObservableModel()
let host = ViewHost(Screen(model: model))
var fired = false
host.onStateChange = { fired = true }
var node = host.evaluate()
#expect(node.props["text"] == .string("count 0"))
model.counter += 1
#expect(fired)
node = host.evaluate()
#expect(node.props["text"] == .string("count 1"))
}
@Test("@Bindable projects a two-way binding into an observable model")
func bindableProjection() {
let model = ObservableModel()
@Bindable var bound = model
let binding = $bound.name
binding.wrappedValue = "hi"
#expect(model.name == "hi")
#expect(binding.wrappedValue == "hi")
}
@Test("A TextField bound via @Bindable writes back to the model")
func bindableTextField() {
struct Screen: View {
@Bindable var model: ObservableModel
var body: some View { TextField("Name", text: $model.name) }
}
let model = ObservableModel()
let host = ViewHost(Screen(model: model))
var node = host.evaluate()
#expect(node.props["text"] == .string(""))
if case .int(let id)? = node.props["onChange"] {
host.callbacks.invokeString(Int64(id), "Coleman")
}
#expect(model.name == "Coleman")
node = host.evaluate()
#expect(node.props["text"] == .string("Coleman"))
}
#endif
}