ViewInspector icon indicating copy to clipboard operation
ViewInspector copied to clipboard

callOnTapGesture not working when in async

Open nh7a opened this issue 1 year ago • 0 comments

callOnTapGesture() does not work when the view is presented with host() async. Tested with Xcode 16.0 with Swift 6.0 mode.

struct TapGestureView: View {
    @State var flag = false
    let inspection = Inspection<Self>()

    var body: some View {
        Text("flag = \(flag)")
            .onTapGesture { self.flag.toggle() }
            .onReceive(inspection.notice) { self.inspection.visit(self, $0) }
    }
}

final class TapGestureViewTests: XCTestCase {

    @MainActor func test_regular_host() throws {
        let sut = TapGestureView()
        let exp = sut.inspection.inspect { view in
            XCTAssertEqual(try view.anyView().text().string(), "flag = false")
            try view.anyView().text().callOnTapGesture()
            XCTAssertEqual(try view.anyView().text().string(), "flag = true")  // 🆗
        }
        ViewHosting.host(view: sut)
        wait(for: [exp], timeout: 1)
    }

    @MainActor func test_async_host() async throws {
        let sut = TapGestureView()
        try await ViewHosting.host(sut) {
            let view = try $0.inspect()
            XCTAssertEqual(try view.anyView().text().string(), "flag = false")
            try view.anyView().text().callOnTapGesture()
            XCTAssertEqual(try view.anyView().text().string(), "flag = true")  // 💥
        }
    }
}

nh7a avatar Sep 18 '24 23:09 nh7a