nalexn / ViewInspector

Runtime introspection and unit testing of SwiftUI views
MIT License
2.09k stars 145 forks source link

Views with .help become unfindable #280

Closed grantneufeld closed 7 months ago

grantneufeld commented 7 months ago

ViewInspector: 0.9.8 Xcode: 15.0.1 macOS: Sonoma 14.1.1 Processor: M1

When a view has a .help modifier (macOS for help tool-tips), it causes ViewInspector’s find method to fail.

Removing the .help modifier lets the test pass, but that’s not an acceptable solution.

MyView.swift:

import SwiftUI
struct MyView: View {
    var didAppear: ((Self) -> Void)?
    // various properties ...
    var body: some View {
        VStack {
            Button("My Button") { self.buttonAction() }
                .accessibilityIdentifier("my button")
                .help("The help text for my button.") // ⬅️ the modifier that causes problems
        }
        .onAppear {
            self.didAppear?(self)
        }
    }
    // other methods and stuff ...
}

MyViewTests.swift

@testable import MyApp
import SwiftUI
import ViewInspector
import XCTest

final class MyViewTests: XCTestCase {
    func test_whenTryToFind_shouldReturnButtonView() throws {
        var sut = MyView()
        // the following line reports an error, based on the `find` call in its closure:
        // 🛑 failed - Search did not find a match. Possible blockers: HelpView<ModifiedContent<Button<Text>, AccessibilityAttachmentModifier>>
        let appear = sut.on(\.didAppear) { view in
            let button = try view.find(viewWithAccessibilityIdentifier: "my button").button()
            try button.tap()
            // my assertion
        }
        ViewHosting.host(view: sut)
        wait(for: [appear], timeout: 0.1)
    }
}
nalexn commented 7 months ago

Thank you for the heads up! This is fixed now with #281

grantneufeld commented 7 months ago

Lovely! Thanks for such a quick fix 😁

nalexn commented 7 months ago

Released in v0.9.9

grantneufeld commented 7 months ago

Just tried it out and it works well now. Thanks, again!