Skip to content

Commit 1474c65

Browse files
committed
Add test coverage to loadAllActiveLiveLocations
1 parent 57c9e1e commit 1474c65

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

TestTools/StreamChatTestTools/TestData/DummyData/SharedLocationPayload.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import Foundation
88
extension SharedLocationPayload {
99
/// Returns dummy draft payload with the given values.
1010
static func dummy(
11-
channelId: String = .unique,
11+
channelId: ChannelId = .unique,
1212
messageId: String = .unique,
1313
latitude: Double,
1414
longitude: Double,
1515
endAt: Date? = nil,
1616
createdByDeviceId: DeviceId = .unique
1717
) -> SharedLocationPayload {
1818
.init(
19-
channelId: channelId,
19+
channelId: channelId.rawValue,
2020
messageId: messageId,
2121
latitude: latitude,
2222
longitude: longitude,

Tests/StreamChatTests/Workers/CurrentUserUpdater_Tests.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,70 @@ final class CurrentUserUpdater_Tests: XCTestCase {
799799
XCTAssertEqual(receivedError as? TestError, expectedError)
800800
}
801801

802+
// MARK: - Load Active Live Locations
803+
804+
func test_loadActiveLiveLocations_makesCorrectAPICall() {
805+
// WHEN
806+
currentUserUpdater.loadActiveLiveLocations { _ in }
807+
808+
// THEN
809+
let endpoint = apiClient.request_endpoint
810+
XCTAssertEqual(endpoint?.path.value, "users/live_locations")
811+
XCTAssertEqual(endpoint?.method, .get)
812+
}
813+
814+
func test_loadActiveLiveLocations_successfulResponse_savesToDBAndReturnsModels() throws {
815+
// GIVEN
816+
let payloads = [
817+
SharedLocationPayload.dummy(latitude: 10, longitude: 20, endAt: Date().addingTimeInterval(100)),
818+
SharedLocationPayload.dummy(latitude: 30, longitude: 40, endAt: Date().addingTimeInterval(200))
819+
]
820+
let response = ActiveLiveLocationsResponsePayload(locations: payloads)
821+
var result: Result<[SharedLocation], Error>?
822+
823+
// WHEN
824+
let expectation = self.expectation(description: "loadActiveLiveLocations")
825+
currentUserUpdater.loadActiveLiveLocations {
826+
result = $0
827+
expectation.fulfill()
828+
}
829+
apiClient.test_simulateResponse(.success(response))
830+
831+
waitForExpectations(timeout: defaultTimeout)
832+
833+
// THEN
834+
let sharedLocations = try result?.get()
835+
XCTAssertEqual(sharedLocations?.count, payloads.count)
836+
for (model, payload) in zip(sharedLocations ?? [], payloads) {
837+
XCTAssertEqual(model.messageId, payload.messageId)
838+
XCTAssertEqual(model.channelId.rawValue, payload.channelId)
839+
XCTAssertEqual(model.latitude, payload.latitude)
840+
XCTAssertEqual(model.longitude, payload.longitude)
841+
XCTAssertEqual(model.endAt?.timeIntervalSince1970, payload.endAt?.timeIntervalSince1970)
842+
XCTAssertEqual(model.createdByDeviceId, payload.createdByDeviceId)
843+
}
844+
}
845+
846+
func test_loadActiveLiveLocations_propagatesNetworkError() {
847+
// GIVEN
848+
let expectedError = TestError()
849+
var result: Result<[SharedLocation], Error>?
850+
851+
// WHEN
852+
currentUserUpdater.loadActiveLiveLocations {
853+
result = $0
854+
}
855+
apiClient.test_simulateResponse(Result<ActiveLiveLocationsResponsePayload, Error>.failure(expectedError))
856+
857+
// THEN
858+
switch result {
859+
case .failure(let error as TestError):
860+
XCTAssertEqual(error, expectedError)
861+
default:
862+
XCTFail("Expected TestError")
863+
}
864+
}
865+
802866
// MARK: -
803867

804868
private func setUpDownloadedAttachment(with payload: AnyAttachmentPayload, messageId: MessageId = .unique, cid: ChannelId = .unique) throws -> AttachmentId {

0 commit comments

Comments
 (0)