PR Suggestions

This commit is contained in:
Mauro Romito
2026-03-31 12:09:50 +02:00
committed by Mauro
parent f31c256037
commit a1029132c5
5 changed files with 40 additions and 43 deletions
@@ -12,11 +12,11 @@ import CoreLocation
import Testing
@MainActor
struct LocationSharingScreenViewModelTests {
var timelineProxy: TimelineProxyMock!
var viewModel: LocationSharingScreenViewModel!
final class LocationSharingScreenViewModelTests {
private var timelineProxy: TimelineProxyMock!
private var viewModel: LocationSharingScreenViewModel!
var context: LocationSharingScreenViewModel.Context {
private var context: LocationSharingScreenViewModel.Context {
viewModel.context
}
@@ -24,8 +24,12 @@ struct LocationSharingScreenViewModelTests {
AppSettings.resetAllSettings()
}
deinit {
AppSettings.resetAllSettings()
}
@Test
mutating func userDidPan() {
func userDidPan() {
setupViewModel()
#expect(context.viewState.isSharingUserLocation)
#expect(context.showsUserLocationMode == .showAndFollow)
@@ -35,7 +39,7 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func centerOnUser() {
func centerOnUser() {
setupViewModel()
#expect(context.viewState.isSharingUserLocation)
context.showsUserLocationMode = .show
@@ -46,7 +50,7 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func centerOnUserWithoutAuth() {
func centerOnUserWithoutAuthorization() {
setupViewModel()
context.showsUserLocationMode = .hide
context.isLocationAuthorized = nil
@@ -55,7 +59,7 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func centerOnUserWithDeniedAuth() {
func centerOnUserWithDeniedAuthorization() {
setupViewModel()
context.isLocationAuthorized = false
context.showsUserLocationMode = .hide
@@ -65,18 +69,18 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func errorMapping() {
func errorMapping() {
setupViewModel()
let mapError = AlertInfo(locationSharingViewError: .mapError(.failedLoadingMap))
#expect(mapError.title == L10n.errorFailedLoadingMap(InfoPlistReader.main.bundleDisplayName))
let locationError = AlertInfo(locationSharingViewError: .mapError(.failedLocatingUser))
#expect(locationError.title == L10n.errorFailedLocatingUser(InfoPlistReader.main.bundleDisplayName))
let authorizationError = AlertInfo(locationSharingViewError: .missingAuthorization)
#expect(authorizationError.message == L10n.dialogPermissionLocationDescriptionIos(InfoPlistReader.main.bundleDisplayName))
let AuthorizationError = AlertInfo(locationSharingViewError: .missingAuthorization)
#expect(AuthorizationError.message == L10n.dialogPermissionLocationDescriptionIos(InfoPlistReader.main.bundleDisplayName))
}
@Test
mutating func sendUserLocation() async throws {
func sendUserLocation() async throws {
setupViewModel()
context.mapCenterLocation = .init(latitude: 0, longitude: 0)
context.geolocationUncertainty = 10
@@ -98,7 +102,7 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func sendPickedLocation() async throws {
func sendPickedLocation() async throws {
setupViewModel()
context.mapCenterLocation = .init(latitude: 0, longitude: 0)
context.isLocationAuthorized = nil
@@ -123,28 +127,28 @@ struct LocationSharingScreenViewModelTests {
// MARK: - Live Location Authorization Tests
@Test
mutating func startLiveLocationWithDeniedAuth() {
func startLiveLocationWithDeniedAuthorization() {
setupViewModel(liveLocationManagerConfiguration: .init(authorizationStatus: .denied))
context.send(viewAction: .startLiveLocation)
#expect(context.alertInfo?.id == .missingAlwaysAuthorization)
}
@Test
mutating func startLiveLocationWithRestrictedAuth() {
func startLiveLocationWithRestrictedAuthorization() {
setupViewModel(liveLocationManagerConfiguration: .init(authorizationStatus: .restricted))
context.send(viewAction: .startLiveLocation)
#expect(context.alertInfo?.id == .missingAlwaysAuthorization)
}
@Test
mutating func startLiveLocationWithAlwaysAuth() {
func startLiveLocationWithAlwaysAuthorization() {
setupViewModel(liveLocationManagerConfiguration: .init(authorizationStatus: .authorizedAlways))
context.send(viewAction: .startLiveLocation)
#expect(context.alertInfo == nil)
}
@Test
mutating func startLiveLocationWithWhenInUseAuthAlreadyRequested() {
func startLiveLocationWithWhenInUseAuthorizationAlreadyRequested() {
setupViewModel(liveLocationManagerConfiguration: .init(authorizationStatus: .authorizedWhenInUse,
requestAlwaysAuthorizationIfPossibleReturnValue: false))
context.send(viewAction: .startLiveLocation)
@@ -152,7 +156,7 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func startLiveLocationWithWhenInUseAuthNotYetRequested() {
func startLiveLocationWithWhenInUseAuthorizationNotYetRequested() {
setupViewModel(liveLocationManagerConfiguration: .init(authorizationStatus: .authorizedWhenInUse,
requestAlwaysAuthorizationIfPossibleReturnValue: true))
context.send(viewAction: .startLiveLocation)
@@ -161,7 +165,7 @@ struct LocationSharingScreenViewModelTests {
}
@Test
mutating func startLiveLocationWithNotDeterminedAuthTransitionsToWhenInUse() async {
func startLiveLocationWithNotDeterminedAuthorizationTransitionsToWhenInUse() async {
let authorizationStatusSubject = CurrentValueSubject<CLAuthorizationStatus, Never>(.notDetermined)
let liveLocationManagerMock = LiveLocationManagerMock()
liveLocationManagerMock.underlyingAuthorizationStatus = .init(authorizationStatusSubject)
@@ -173,7 +177,7 @@ struct LocationSharingScreenViewModelTests {
// No alert yet — waiting for MapLibre to resolve the status to whenInUse
#expect(context.alertInfo == nil)
// Simulate MapLibre resolving the authorization to whenInUse, and confirm that the ViewModel
// Simulate MapLibre resolving the Authorization to whenInUse, and confirm that the ViewModel
// recurses and calls requestAlwaysAuthorizationIfPossible as a result
await waitForConfirmation { confirmation in
liveLocationManagerMock.requestAlwaysAuthorizationIfPossibleClosure = {
@@ -183,13 +187,13 @@ struct LocationSharingScreenViewModelTests {
authorizationStatusSubject.send(.authorizedWhenInUse)
}
// The request was made, so no alert — waiting for the always authorization prompt response
// The request was made, so no alert — waiting for the always Authorization prompt response
#expect(context.alertInfo == nil)
}
// MARK: - Private
private mutating func setupViewModel(liveLocationManagerConfiguration: LiveLocationManagerMock.Configuration = .init()) {
private func setupViewModel(liveLocationManagerConfiguration: LiveLocationManagerMock.Configuration = .init()) {
timelineProxy = TimelineProxyMock(.init())
viewModel = LocationSharingScreenViewModel(interactionMode: .picker,
mapURLBuilder: ServiceLocator.shared.settings.mapTilerConfiguration,
@@ -203,7 +207,7 @@ struct LocationSharingScreenViewModelTests {
viewModel.state.bindings.isLocationAuthorized = true
}
private mutating func setupViewModel(liveLocationManagerMock: LiveLocationManagerMock) {
private func setupViewModel(liveLocationManagerMock: LiveLocationManagerMock) {
timelineProxy = TimelineProxyMock(.init())
viewModel = LocationSharingScreenViewModel(interactionMode: .picker,
mapURLBuilder: ServiceLocator.shared.settings.mapTilerConfiguration,