in ApolloTests/WatchQueryTests.swift [195:252]
func testWatchedQueryWithID() throws {
let query = HeroNameWithIdQuery()
let initialRecords: RecordSet = [
"QUERY_ROOT": ["hero": Reference(key: "2001")],
"2001": [
"id": "2001",
"name": "R2-D2",
"__typename": "Droid",
]
]
withCache(initialRecords: initialRecords) { (cache) in
let networkTransport = MockNetworkTransport(body: [
"data": [
"hero": [
"id": "2001",
"name": "Luke Skywalker",
"__typename": "Human"
]
]
])
let store = ApolloStore(cache: cache)
let client = ApolloClient(networkTransport: networkTransport, store: store)
client.store.cacheKeyForObject = { $0["id"] }
var verifyResult: OperationResultHandler<HeroNameWithIdQuery>
verifyResult = { (result, error) in
XCTAssertNil(error)
XCTAssertNil(result?.errors)
XCTAssertEqual(result?.data?.hero?.name, "R2-D2")
}
var expectation = self.expectation(description: "Fetching query")
_ = client.watch(query: query) { (result, error) in
verifyResult(result, error)
expectation.fulfill()
}
waitForExpectations(timeout: 5, handler: nil)
verifyResult = { (result, error) in
XCTAssertNil(error)
XCTAssertNil(result?.errors)
XCTAssertEqual(result?.data?.hero?.name, "Luke Skywalker")
}
expectation = self.expectation(description: "Fetching other query")
client.fetch(query: HeroNameWithIdQuery(), cachePolicy: .fetchIgnoringCacheData)
waitForExpectations(timeout: 5, handler: nil)
}
}