func testReceiveValidData()

in AppSyncRealTimeClientTests/Connection/AppSyncSubscriptionConnectionTests.swift [194:233]


    func testReceiveValidData() {
        let connectionProvider = MockConnectionProvider()
        let connection = AppSyncSubscriptionConnection(provider: connectionProvider)

        let connectingMessageExpectation = expectation(description: "Connecting event should be fired")
        let connectedMessageExpectation = expectation(description: "Connected event should be fired")

        let dataEventExpectation = expectation(description: "Data event should be fired")

        let item = connection.subscribe(
            requestString: mockRequestString,
            variables: variables
        ) { event, _ in
            switch event {
            case .connection(let status):

                if status == .connected {
                    connectedMessageExpectation.fulfill()
                }
                if status == .connecting {
                    connectingMessageExpectation.fulfill()
                }
            case .data(let data):
                dataEventExpectation.fulfill()
                XCTAssertNotNil(data, "Data should not be nil")
            case .failed:
                XCTFail("Error should not be thrown")
            }
        }
        XCTAssertNotNil(item, "Subscription item should not be nil")
        wait(for: [connectingMessageExpectation, connectedMessageExpectation], timeout: 5, enforceOrder: true)

        let mockResponse = AppSyncResponse(
            id: item.identifier,
            payload: ["data": "testData"],
            type: .data
        )
        connectionProvider.sendDataResponse(mockResponse)
        wait(for: [dataEventExpectation], timeout: 2)
    }