nalexn / ViewInspector

Runtime introspection and unit testing of SwiftUI views
MIT License
2.16k stars 148 forks source link

Provide access to a TextField’s prompt #259

Closed grantneufeld closed 10 months ago

grantneufeld commented 1 year ago

Add an accessor for the prompt when inspecting a TextField.

Given code like the following:

struct MyView: View {
    @Binding var myText: String = ""
    var prompt: String = ""
    var body: some View {
        TextField("My Label", text: $myText, prompt: self.prompt)
    }
}

We would be able to do something like:

func test_promptIsCorrect() throws {
    var textBinding = Binding<String>(wrappedValue: "")
    let sut = MyView(prompt: "My Prompt")
    let prompt = try sut.inspect().view(MyView.self).textField().prompt()
    let promptText = try prompt.string()
    XCTAssertEqual(promptText, "My Prompt")
}

Discussed in https://github.com/nalexn/ViewInspector/discussions/258

nalexn commented 10 months ago

Released in v0.9.9