@@ -38,15 +38,20 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
38
38
let hubListener = Amplify . Hub. listen (
39
39
to: . dataStore,
40
40
eventName: HubPayload . EventName. DataStore. syncReceived) { payload in
41
- guard let mutationEvent = payload. data as? MutationEvent ,
42
- let post = try ? mutationEvent. decodeModel ( ) as? Post
41
+ guard let mutationEvent = payload. data as? MutationEvent
43
42
else {
44
43
XCTFail ( " Can't cast payload as mutation event " )
45
44
return
46
45
}
47
46
47
+ // This check is to protect against stray events being processed after the test has completed,
48
+ // and it shouldn't be construed as a pattern necessary for production applications.
49
+ guard let post = try ? mutationEvent. decodeModel ( ) as? Post , post. id == newPost. id else {
50
+ return
51
+ }
52
+
48
53
if mutationEvent. mutationType == GraphQLMutationType . create. rawValue {
49
- XCTAssertEqual ( post. content, post . content)
54
+ XCTAssertEqual ( post. content, newPost . content)
50
55
XCTAssertEqual ( mutationEvent. version, 1 )
51
56
createReceived. fulfill ( )
52
57
return
@@ -109,15 +114,20 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
109
114
let hubListener = Amplify . Hub. listen (
110
115
to: . dataStore,
111
116
eventName: HubPayload . EventName. DataStore. syncReceived) { payload in
112
- guard let mutationEvent = payload. data as? MutationEvent ,
113
- let post = try ? mutationEvent. decodeModel ( ) as? Post
117
+ guard let mutationEvent = payload. data as? MutationEvent
114
118
else {
115
119
XCTFail ( " Can't cast payload as mutation event " )
116
120
return
117
121
}
118
122
123
+ // This check is to protect against stray events being processed after the test has completed,
124
+ // and it shouldn't be construed as a pattern necessary for production applications.
125
+ guard let post = try ? mutationEvent. decodeModel ( ) as? Post , post. id == newPost. id else {
126
+ return
127
+ }
128
+
119
129
if mutationEvent. mutationType == GraphQLMutationType . create. rawValue {
120
- XCTAssertEqual ( post. content, post . content)
130
+ XCTAssertEqual ( post. content, newPost . content)
121
131
XCTAssertEqual ( mutationEvent. version, 1 )
122
132
createReceived. fulfill ( )
123
133
return
@@ -171,16 +181,21 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
171
181
let conditionalReceived = expectation ( description: " Conditional save failed received " )
172
182
173
183
let hubListener = Amplify . Hub. listen ( to: . dataStore) { payload in
174
- guard let mutationEvent = payload. data as? MutationEvent ,
175
- let post = try ? mutationEvent. decodeModel ( ) as? Post
184
+ guard let mutationEvent = payload. data as? MutationEvent
176
185
else {
177
186
XCTFail ( " Can't cast payload as mutation event " )
178
187
return
179
188
}
180
189
190
+ // This check is to protect against stray events being processed after the test has completed,
191
+ // and it shouldn't be construed as a pattern necessary for production applications.
192
+ guard let post = try ? mutationEvent. decodeModel ( ) as? Post , post. id == newPost. id else {
193
+ return
194
+ }
195
+
181
196
if payload. eventName == HubPayload . EventName. DataStore. syncReceived {
182
197
if mutationEvent. mutationType == GraphQLMutationType . create. rawValue {
183
- XCTAssertEqual ( post. content, post . content)
198
+ XCTAssertEqual ( post. content, newPost . content)
184
199
XCTAssertEqual ( mutationEvent. version, 1 )
185
200
createReceived. fulfill ( )
186
201
return
0 commit comments