Skip to content

Commit 8741f3d

Browse files
committed
Test @bindable projection and write-back
1 parent 056f402 commit 8741f3d

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

AndroidSwiftUICore/Tests/AndroidSwiftUICoreTests/ControlTests.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Testing
99
#if canImport(Observation)
1010
import Observation
1111

12-
@Observable final class ObservableModel { var counter = 0 }
12+
@Observable final class ObservableModel { var counter = 0; var name = "" }
1313
#endif
1414

1515
@Suite("Controls and environment")
@@ -109,5 +109,33 @@ struct ControlTests {
109109
node = host.evaluate()
110110
#expect(node.props["text"] == .string("count 1"))
111111
}
112+
113+
@Test("@Bindable projects a two-way binding into an observable model")
114+
func bindableProjection() {
115+
let model = ObservableModel()
116+
@Bindable var bound = model
117+
let binding = $bound.name
118+
binding.wrappedValue = "hi"
119+
#expect(model.name == "hi")
120+
#expect(binding.wrappedValue == "hi")
121+
}
122+
123+
@Test("A TextField bound via @Bindable writes back to the model")
124+
func bindableTextField() {
125+
struct Screen: View {
126+
@Bindable var model: ObservableModel
127+
var body: some View { TextField("Name", text: $model.name) }
128+
}
129+
let model = ObservableModel()
130+
let host = ViewHost(Screen(model: model))
131+
var node = host.evaluate()
132+
#expect(node.props["text"] == .string(""))
133+
if case .int(let id)? = node.props["onChange"] {
134+
host.callbacks.invokeString(Int64(id), "Coleman")
135+
}
136+
#expect(model.name == "Coleman")
137+
node = host.evaluate()
138+
#expect(node.props["text"] == .string("Coleman"))
139+
}
112140
#endif
113141
}

0 commit comments

Comments
 (0)