2021-12-16 12:02:42 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
import Combine
|
2021-12-22 12:12:42 +00:00
|
|
|
import CoreLocation
|
2021-12-16 12:02:42 +00:00
|
|
|
|
|
|
|
@testable import RiotSwiftUI
|
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
|
|
|
class LocationSharingViewModelTests: XCTestCase {
|
2021-12-22 12:12:42 +00:00
|
|
|
|
2021-12-16 12:02:42 +00:00
|
|
|
var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-12-22 12:12:42 +00:00
|
|
|
func testInitialState() {
|
2022-04-11 08:55:23 +00:00
|
|
|
let viewModel = buildViewModel()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
XCTAssertTrue(viewModel.context.viewState.shareButtonEnabled)
|
|
|
|
XCTAssertFalse(viewModel.context.viewState.showLoadingIndicator)
|
2021-12-17 12:27:43 +00:00
|
|
|
|
2022-01-27 11:58:43 +00:00
|
|
|
XCTAssertNotNil(viewModel.context.viewState.mapStyleURL)
|
2022-03-16 13:39:51 +00:00
|
|
|
XCTAssertNotNil(viewModel.context.viewState.userAvatarData)
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
XCTAssertNil(viewModel.context.viewState.bindings.userLocation)
|
|
|
|
XCTAssertNil(viewModel.context.viewState.bindings.alertInfo)
|
2021-12-16 12:02:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 12:12:42 +00:00
|
|
|
func testCancellation() {
|
2022-04-11 08:55:23 +00:00
|
|
|
let viewModel = buildViewModel()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
let expectation = self.expectation(description: "Cancellation completion should be invoked")
|
|
|
|
|
|
|
|
viewModel.completion = { result in
|
|
|
|
switch result {
|
|
|
|
case .share:
|
|
|
|
XCTFail()
|
|
|
|
case .cancel:
|
|
|
|
expectation.fulfill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
viewModel.context.send(viewAction: .cancel)
|
|
|
|
|
|
|
|
waitForExpectations(timeout: 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testShareNoUserLocation() {
|
2022-04-11 08:55:23 +00:00
|
|
|
let viewModel = buildViewModel()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
XCTAssertNil(viewModel.context.viewState.bindings.userLocation)
|
|
|
|
|
|
|
|
viewModel.context.send(viewAction: .share)
|
|
|
|
|
|
|
|
XCTAssertNotNil(viewModel.context.viewState.bindings.alertInfo)
|
|
|
|
XCTAssertEqual(viewModel.context.viewState.bindings.alertInfo?.id, .userLocatingError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoading() {
|
2022-04-11 08:55:23 +00:00
|
|
|
let viewModel = buildViewModel()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
2022-01-28 10:58:31 +00:00
|
|
|
viewModel.startLoading()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
XCTAssertFalse(viewModel.context.viewState.shareButtonEnabled)
|
|
|
|
XCTAssertTrue(viewModel.context.viewState.showLoadingIndicator)
|
|
|
|
|
2022-01-28 10:58:31 +00:00
|
|
|
viewModel.stopLoading()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
XCTAssertTrue(viewModel.context.viewState.shareButtonEnabled)
|
|
|
|
XCTAssertFalse(viewModel.context.viewState.showLoadingIndicator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInvalidLocationAuthorization() {
|
2022-04-11 08:55:23 +00:00
|
|
|
let viewModel = buildViewModel()
|
2021-12-22 12:12:42 +00:00
|
|
|
|
|
|
|
viewModel.context.viewState.errorSubject.send(.invalidLocationAuthorization)
|
|
|
|
|
|
|
|
XCTAssertNotNil(viewModel.context.alertInfo)
|
|
|
|
XCTAssertEqual(viewModel.context.viewState.bindings.alertInfo?.id, .authorizationError)
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:55:23 +00:00
|
|
|
private func buildViewModel() -> LocationSharingViewModel {
|
2022-01-27 11:58:43 +00:00
|
|
|
LocationSharingViewModel(mapStyleURL: URL(string: "http://empty.com")!,
|
2022-04-11 08:55:23 +00:00
|
|
|
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: ""))
|
2021-12-16 12:02:42 +00:00
|
|
|
}
|
|
|
|
}
|