Skip to content

Commit 0959096

Browse files
committed
Add snapshot publisher tests for snapshotting and restoring for optimistic updates
1 parent 8bd3dcd commit 0959096

5 files changed

+98
-0
lines changed

Tests/RelayTests/SnapshotPublisherTests.swift

+78
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,84 @@ class SnapshotPublisherTests: XCTestCase {
7272
expect(updateCount) == 0
7373
}
7474

75+
func testUpdatesForOptimisticUpdate() throws {
76+
let op = CurrentUserToDoListQuery()
77+
try environment.cachePayload(op, CurrentUserToDoList.myTodos)
78+
79+
let operation = op.createDescriptor()
80+
let selector = SingularReaderSelector(dataID: "VG9kbzox", node: ToDoItem_todo.node, owner: operation.request, variables: operation.request.variables)
81+
var snapshot: Snapshot<ToDoItem_todo.Data?> = environment.lookup(selector: selector)
82+
assertSnapshot(matching: snapshot.data, as: .dump)
83+
84+
var updateCount = 0
85+
environment.subscribe(snapshot: snapshot)
86+
.sink { newSnapshot in
87+
snapshot = newSnapshot
88+
updateCount += 1
89+
}
90+
.store(in: &cancellables)
91+
92+
let mutation = ChangeTodoStatusMutation(input: .init(complete: true, id: "VG9kbzox", userId: "me"))
93+
_ = try environment.delayMockedResponse(mutation, ChangeTodoStatus.completeBuyHorse)
94+
95+
let optimisticPayload = [
96+
"changeTodoStatus": [
97+
"todo": [
98+
"id": "VG9kbzox",
99+
"complete": true,
100+
],
101+
],
102+
]
103+
environment.commitMutation(mutation, optimisticResponse: optimisticPayload)
104+
.sink(receiveCompletion: {_ in }) { _ in }
105+
.store(in: &cancellables)
106+
107+
// since we haven't advanced, this will be using the optimistic response
108+
expect(updateCount).toEventually(equal(1))
109+
assertSnapshot(matching: snapshot.data, as: .dump)
110+
}
111+
112+
func testRevertsOptimisticUpdate() throws {
113+
let op = CurrentUserToDoListQuery()
114+
try environment.cachePayload(op, CurrentUserToDoList.myTodos)
115+
116+
let operation = op.createDescriptor()
117+
let selector = SingularReaderSelector(dataID: "VG9kbzox", node: ToDoItem_todo.node, owner: operation.request, variables: operation.request.variables)
118+
var snapshot: Snapshot<ToDoItem_todo.Data?> = environment.lookup(selector: selector)
119+
assertSnapshot(matching: snapshot.data, as: .dump)
120+
121+
var updateCount = 0
122+
environment.subscribe(snapshot: snapshot)
123+
.sink { newSnapshot in
124+
snapshot = newSnapshot
125+
updateCount += 1
126+
}
127+
.store(in: &cancellables)
128+
129+
let mutation = ChangeTodoStatusMutation(input: .init(complete: true, id: "VG9kbzox", userId: "me"))
130+
let advance = try environment.delayMockedResponse(mutation, ChangeTodoStatus.error)
131+
132+
let optimisticPayload = [
133+
"changeTodoStatus": [
134+
"todo": [
135+
"id": "VG9kbzox",
136+
"complete": true,
137+
],
138+
],
139+
]
140+
environment.commitMutation(mutation, optimisticResponse: optimisticPayload)
141+
.sink(receiveCompletion: {_ in }) { _ in }
142+
.store(in: &cancellables)
143+
144+
// since we haven't advanced, this will be using the optimistic response
145+
expect(updateCount).toEventually(equal(1))
146+
147+
// now we advance so the error happens and we revert the response
148+
advance()
149+
expect(updateCount).toEventually(equal(2))
150+
assertSnapshot(matching: snapshot.data, as: .dump)
151+
}
152+
75153
private func loadInitialData() throws {
76154
let op = MoviesTabQuery()
77155
try environment.mockResponse(op, MoviesTab.allFilms)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
▿ Optional<Data>
2+
▿ some: Data
3+
- complete: false
4+
- id: "VG9kbzox"
5+
- text: "Buy a unicorn"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
▿ Optional<Data>
2+
▿ some: Data
3+
- complete: false
4+
- id: "VG9kbzox"
5+
- text: "Buy a unicorn"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
▿ Optional<Data>
2+
▿ some: Data
3+
- complete: false
4+
- id: "VG9kbzox"
5+
- text: "Buy a unicorn"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
▿ Optional<Data>
2+
▿ some: Data
3+
- complete: true
4+
- id: "VG9kbzox"
5+
- text: "Buy a unicorn"

0 commit comments

Comments
 (0)