Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspecting custom modifier doesn't see removed views #262

Closed
noahsmartin opened this issue Oct 5, 2023 · 1 comment
Closed

Inspecting custom modifier doesn't see removed views #262

noahsmartin opened this issue Oct 5, 2023 · 1 comment

Comments

@noahsmartin
Copy link

I have a view like this:

struct TestView: View {
  var body: some View {
    Text("testing")
  }
}

and a modifier like this:

struct TestModifier: ViewModifier {
    func body(content: Self.Content) -> some View {
      Text("Hello world")
    }
}

I would expect that using the modifier:
let v = TestView().modifier(TestModifier())
would result in a view that does not have a TestView so this test should pass:

XCTAssertThrowsError(try v.inspect().find(TestView.self))

However, the test fails, an error is not raised when finding TestView.
Is this expected, or is there another way to assert that this modifier is removing the TestView?

@nalexn
Copy link
Owner

nalexn commented Oct 7, 2023

Hey - I would say this behavior is expected because the view's structure still contains the view, which ViewInspector sees and locates during the find. Similarly, if you do TestView().hidden(), the library would still succeed in finding this view. In this particular case, isHidden() check allows to verify the view is hidden.

If your intent is to test that your custom view modifier doesn't drop the content, you can write a find extension that locates viewModifierContent() (see these tests for reference, and this guide section).

If your intent is to test that your custom view will be hidden in runtime because that custom modifier is applied, there is currently no way to attest "absence" directly, but only indirect methods, such as locating the view and then verifying certain modifiers are or are not applied to this view or any of its parent views.

@nalexn nalexn closed this as completed Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants