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

Nested EnvironmentObject #62

Closed
joemasilotti opened this issue Dec 14, 2020 · 4 comments
Closed

Nested EnvironmentObject #62

joemasilotti opened this issue Dec 14, 2020 · 4 comments

Comments

@joemasilotti
Copy link
Contributor

Is it not possible to test a nested @EnvironmentObject? The following view works when running in the simulator.

import SwiftUI

struct ParentView: View {
    @EnvironmentObject var strings: Strings
    internal let inspection = Inspection<Self>()

    var body: some View {
        NestedView()
            .environmentObject(strings)
            .onReceive(inspection.notice) { self.inspection.visit(self, $0) }
    }
}

struct NestedView: View {
    @EnvironmentObject var strings: Strings
    internal let inspection = Inspection<Self>()

    var body: some View {
        Text(strings.all[0])
            .onReceive(inspection.notice) { self.inspection.visit(self, $0) }
    }
}

class Strings: ObservableObject {
    @Published var all: [String] = []
}

let strings = Strings()
strings.all.append("New string.")
let contentView = ParentView()
    .environmentObject(strings)

But the following test fails.

Thread 1: Fatal error: No ObservableObject of type Strings found. A View.environmentObject(_:) for Strings may be missing as an ancestor of this view.

import ViewInspector
@testable import App
import XCTest

class Tests: XCTestCase {
    func testNestedEnvironmentObject() throws {
        let strings = Strings()
        strings.all.append("First string")
        let view = ParentView()

        let expectation = view.inspection.inspect { view in
            let text = try view.view(NestedView.self, 0).text().string()
            XCTAssertEqual(text, "First string")
        }

        let hostedView = view.environmentObject(strings)
        ViewHosting.host(view: hostedView)
        wait(for: [expectation], timeout: 0.1)
    }
}

extension ParentView: Inspectable {}
extension NestedView: Inspectable {}
@nalexn
Copy link
Owner

nalexn commented Dec 19, 2020

I confirm this is currently not supported. You can test the nested view independently, but not as part of the parent hierarchy. This is a very annoying limitation, I know, looking for a way to work around it.

@joemasilotti
Copy link
Contributor Author

Got it, thanks for the quick response!

Would you like me to leave the issue open or mark it down in the README somewhere? Maybe a new "limitations" section?

@nalexn
Copy link
Owner

nalexn commented Dec 19, 2020

Let's leave the ticket opened

@nalexn
Copy link
Owner

nalexn commented Mar 13, 2021

This just got fixed as of v0.7.0

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