nalexn / ViewInspector

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

Wrong returned type for Alert message() attribute #261

Closed Narayane closed 1 year ago

Narayane commented 1 year ago

Hi,

I think there is a mistake in InspectableView extension where View is an Alert. I can not get the string value of this attribute because returned type is not ViewType.Text but ViewType.ClassifiedView.

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
public extension InspectableView where View == ViewType.Alert {

    func title() throws -> InspectableView<ViewType.Text> {
        return try View.supplementaryChildren(self).element(at: 0)
            .asInspectableView(ofType: ViewType.Text.self)
    }

    func message() throws -> InspectableView<ViewType.ClassifiedView> {
        return try View.supplementaryChildren(self).element(at: 1)
            .asInspectableView(ofType: ViewType.ClassifiedView.self)
    }

I can do this with an ActionSheet so I guess is really a mistake.

// MARK: - Custom Attributes

@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
public extension InspectableView where View == ViewType.ActionSheet {

    func title() throws -> InspectableView<ViewType.Text> {
        return try View.supplementaryChildren(self).element(at: 0)
            .asInspectableView(ofType: ViewType.Text.self)
    }

    func message() throws -> InspectableView<ViewType.Text> {
        return try View.supplementaryChildren(self).element(at: 1)
            .asInspectableView(ofType: ViewType.Text.self)
    }
nalexn commented 1 year ago

Hey, thanks for the heads up. There is no error here - API for Alert view supports arbitrary view types as its message, not just Text. You can see it in the tests. ActionSheet on the other hand doesn't offer API that accepts custom views, and its message is always Text. InspectableView<ViewType.ClassifiedView> still allows you to access Text, if that's the view type stored in the Alert, you just need to unwrap it: alert.message().text()

Narayane commented 1 year ago

Perfect, thanks !