98 lines
3.3 KiB
Swift
98 lines
3.3 KiB
Swift
//
|
|
// LocationPickerTests.swift
|
|
// AutoCatTests
|
|
//
|
|
// Created by Selim Mustafaev on 10.08.2024.
|
|
// Copyright © 2024 Selim Mustafaev. All rights reserved.
|
|
//
|
|
|
|
import Testing
|
|
import CoreLocation
|
|
import Mockable
|
|
import Intents
|
|
import Contacts
|
|
|
|
@testable import AutoCat
|
|
@testable import AutoCatCore
|
|
|
|
@MainActor
|
|
struct LocationPickerTests {
|
|
|
|
let latitude: CLLocationDegrees = 10
|
|
let longitude: CLLocationDegrees = 10
|
|
let address = "Test Address"
|
|
|
|
let locationServiceMock = MockLocationServiceProtocol()
|
|
|
|
func makeViewModel(event: VehicleEventDto) -> LocationPickerViewModel {
|
|
|
|
return LocationPickerViewModel(
|
|
locationService: locationServiceMock,
|
|
event: event
|
|
)
|
|
}
|
|
|
|
@Test("Set initial location (user)")
|
|
func setInitialLocationUser() async throws {
|
|
|
|
let viewModel = makeViewModel(event: .init(lat: 0, lon: 0, addedBy: nil))
|
|
viewModel.onAppear()
|
|
|
|
#expect(viewModel.position == .userLocation(fallback: .automatic))
|
|
}
|
|
|
|
@Test("Set initial location (custom)")
|
|
func setInitialLocationCustom() async throws {
|
|
|
|
let viewModel = makeViewModel(event: .init(lat: latitude, lon: longitude, addedBy: nil))
|
|
viewModel.onAppear()
|
|
|
|
#expect(viewModel.position.region?.center.latitude == latitude)
|
|
#expect(viewModel.position.region?.center.longitude == longitude)
|
|
}
|
|
|
|
@Test("Update event")
|
|
func updateEvent() async throws {
|
|
|
|
let viewModel = makeViewModel(event: .init(lat: 0, lon: 0, addedBy: nil))
|
|
|
|
given(locationServiceMock)
|
|
.getAddressForLocation(latitude: .value(latitude), longitude: .value(longitude))
|
|
.willReturn(address)
|
|
|
|
await viewModel.updateEvent(center: .init(latitude: latitude, longitude: longitude))
|
|
|
|
#expect(viewModel.event.latitude == latitude)
|
|
#expect(viewModel.event.longitude == longitude)
|
|
#expect(viewModel.event.address == address)
|
|
}
|
|
|
|
@Test("Update event (throttling)")
|
|
func updateEventThrottling() async throws {
|
|
|
|
let viewModel = makeViewModel(event: .init(lat: 0, lon: 0, addedBy: nil))
|
|
|
|
let initialLocation = CLLocationCoordinate2D(latitude: 0, longitude: 0)
|
|
let finalLocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
|
|
|
|
given(locationServiceMock)
|
|
.getAddressForLocation(latitude: .value(0), longitude: .value(0)).willReturn("")
|
|
.getAddressForLocation(latitude: .value(latitude), longitude: .value(longitude)).willReturn(address)
|
|
|
|
viewModel.onAppear()
|
|
|
|
async let task1: () = viewModel.updateEvent(center: initialLocation)
|
|
async let task2: () = viewModel.updateEvent(center: initialLocation)
|
|
async let task3: () = viewModel.updateEvent(center: finalLocation)
|
|
_ = await (task1, task2, task3)
|
|
|
|
verify(locationServiceMock)
|
|
.getAddressForLocation(latitude: .value(0), longitude: .value(0)).called(.never)
|
|
.getAddressForLocation(latitude: .value(latitude), longitude: .value(longitude)).called(.once)
|
|
|
|
#expect(viewModel.event.latitude == latitude)
|
|
#expect(viewModel.event.longitude == longitude)
|
|
#expect(viewModel.event.address == address)
|
|
}
|
|
}
|