nalexn / ViewInspector

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

Inspecting conditional toolbar content #265

Open pteasima opened 9 months ago

pteasima commented 9 months ago

Hi, thanks for the awesome work!

I believe that as of version 0.9.9, inspecting conditional toolbar content is not supported. I.e. if I have a toolbar like:

.toolbar {
  if true {
    ToolbarItem {
      Text("X")
    }
  } else {
    ToolbarItem {
      Text("Y")
    }
  }
}

, there is no way to inspect the Text("X"). You can't .find it, nor can you reach it via .toolbar().item().

Screenshot 2023-10-09 at 16 27 35

FWIW, the workaround Im using is always including the ToolbarItems but making their label conditional:

.toolbar {
  ToolbarItem {
    if true {
      Text("X")
    }
  } 
  //...
}

Can someone please confirm that this problem is real (missing functionality)? Any pointers as to how to implement this would be welcome. I've only scratched the surface of how the introspection works, and Im not sure if supporting @ToolbarContentBuilder is any different from @ViewBuilder. (I guess it shouldn't matter? 🤷‍♂️)