@@ -72,6 +72,84 @@ class SnapshotPublisherTests: XCTestCase {
72
72
expect ( updateCount) == 0
73
73
}
74
74
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
+
75
153
private func loadInitialData( ) throws {
76
154
let op = MoviesTabQuery ( )
77
155
try environment. mockResponse ( op, MoviesTab . allFilms)
0 commit comments