Skip to content

Commit 9fdb722

Browse files
authored
Fix: DataStore E2E Integration Tests (#596)
* fix e2e integration test * Make sure only cast as Post event and added a explanation comment
1 parent 25740b3 commit 9fdb722

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Amplify.xcodeproj/project.pbxproj

100644100755
File mode changed.

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/DataStoreEndToEndTests.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
3838
let hubListener = Amplify.Hub.listen(
3939
to: .dataStore,
4040
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
4342
else {
4443
XCTFail("Can't cast payload as mutation event")
4544
return
4645
}
4746

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+
4853
if mutationEvent.mutationType == GraphQLMutationType.create.rawValue {
49-
XCTAssertEqual(post.content, post.content)
54+
XCTAssertEqual(post.content, newPost.content)
5055
XCTAssertEqual(mutationEvent.version, 1)
5156
createReceived.fulfill()
5257
return
@@ -109,15 +114,20 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
109114
let hubListener = Amplify.Hub.listen(
110115
to: .dataStore,
111116
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
114118
else {
115119
XCTFail("Can't cast payload as mutation event")
116120
return
117121
}
118122

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+
119129
if mutationEvent.mutationType == GraphQLMutationType.create.rawValue {
120-
XCTAssertEqual(post.content, post.content)
130+
XCTAssertEqual(post.content, newPost.content)
121131
XCTAssertEqual(mutationEvent.version, 1)
122132
createReceived.fulfill()
123133
return
@@ -171,16 +181,21 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
171181
let conditionalReceived = expectation(description: "Conditional save failed received")
172182

173183
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
176185
else {
177186
XCTFail("Can't cast payload as mutation event")
178187
return
179188
}
180189

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+
181196
if payload.eventName == HubPayload.EventName.DataStore.syncReceived {
182197
if mutationEvent.mutationType == GraphQLMutationType.create.rawValue {
183-
XCTAssertEqual(post.content, post.content)
198+
XCTAssertEqual(post.content, newPost.content)
184199
XCTAssertEqual(mutationEvent.version, 1)
185200
createReceived.fulfill()
186201
return

AmplifyPlugins/DataStore/DataStoreCategoryPlugin.xcodeproj/project.pbxproj

100644100755
File mode changed.

0 commit comments

Comments
 (0)