-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(datastore): Dispatch networkStatus event #766
Changes from all commits
77f0e5d
336ff47
44c98f5
7394116
f2c90c3
1d96b55
20294d3
6cb87f1
03d52ae
77c33d9
120d188
2836469
b256501
205ae90
ff5177a
b9532d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
/// Used as HubPayload for the `NetworkStatus` | ||
public struct NetworkStatusEvent { | ||
/// status of network: true if network is active | ||
public let active: Bool | ||
|
||
public init(active: Bool) { | ||
self.active = active | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,16 +23,27 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase { | |
/// - When: | ||
/// - DataStore's remote sync engine is initialized | ||
/// - Then: | ||
/// - networkStatus received, payload should be: {active: true} | ||
/// - subscriptionEstablished received, payload should be nil | ||
/// - syncQueriesStarted received, payload should be: {models: ["Post", "Comment"]} | ||
/// - outboxStatus received, payload should be {isEmpty: true} | ||
func testDataStoreConfiguredDispatchesHubEvents() throws { | ||
|
||
let networkStatusReceived = expectation(description: "networkStatus received") | ||
let subscriptionsEstablishedReceived = expectation(description: "subscriptionsEstablished received") | ||
let syncQueriesStartedReceived = expectation(description: "syncQueriesStarted received") | ||
let outboxStatusReceived = expectation(description: "outboxStatus received") | ||
|
||
let hubListener = Amplify.Hub.listen(to: .dataStore) { payload in | ||
let hubListener = Amplify.Hub.publisher(for: .dataStore).sink { payload in | ||
if payload.eventName == HubPayload.EventName.DataStore.networkStatus { | ||
guard let networkStatusEvent = payload.data as? NetworkStatusEvent else { | ||
XCTFail("Failed to cast payload data as NetworkStatusEvent") | ||
return | ||
} | ||
XCTAssertEqual(networkStatusEvent.active, true) | ||
networkStatusReceived.fulfill() | ||
} | ||
|
||
if payload.eventName == HubPayload.EventName.DataStore.subscriptionsEstablished { | ||
XCTAssertNil(payload.data) | ||
subscriptionsEstablishedReceived.fulfill() | ||
|
@@ -57,13 +68,10 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase { | |
} | ||
} | ||
|
||
guard try HubListenerTestUtilities.waitForListener(with: hubListener, timeout: 5.0) else { | ||
XCTFail("Listener not registered for hub") | ||
return | ||
} | ||
startAmplify() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how does this work? Amplify.Hub.listen is called before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to Tim, listening for events should work prior to |
||
|
||
waitForExpectations(timeout: networkTimeout, handler: nil) | ||
Amplify.Hub.removeListener(hubListener) | ||
hubListener.cancel() | ||
} | ||
|
||
/// - Given: | ||
|
@@ -83,7 +91,7 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase { | |
let outboxMutationEnqueuedReceived = expectation(description: "outboxMutationEnqueued received") | ||
let outboxMutationProcessedReceived = expectation(description: "outboxMutationProcessed received") | ||
|
||
let hubListener = Amplify.Hub.listen(to: .dataStore) { payload in | ||
let hubListener = Amplify.Hub.publisher(for: .dataStore).sink { payload in | ||
if payload.eventName == HubPayload.EventName.DataStore.outboxMutationEnqueued { | ||
guard let outboxMutationEnqueuedEvent = payload.data as? OutboxMutationEvent else { | ||
XCTFail("Failed to cast payload data as OutboxMutationEvent") | ||
|
@@ -110,15 +118,11 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase { | |
outboxMutationProcessedReceived.fulfill() | ||
} | ||
} | ||
|
||
guard try HubListenerTestUtilities.waitForListener(with: hubListener, timeout: 5.0) else { | ||
XCTFail("Listener not registered for hub") | ||
return | ||
} | ||
|
||
|
||
startAmplify() | ||
Amplify.DataStore.save(post) { _ in } | ||
|
||
waitForExpectations(timeout: networkTimeout, handler: nil) | ||
Amplify.Hub.removeListener(hubListener) | ||
hubListener.cancel() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update testcomment accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated