AutoCat/AutoCatTests/ReportTests.swift

140 lines
4.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ReportTests.swift
// AutoCatTests
//
// Created by Selim Mustafaev on 02.12.2024.
// Copyright © 2024 Selim Mustafaev. All rights reserved.
//
import Testing
import Mockable
import AutoCatCore
@testable import AutoCat
@MainActor
class ReportTests {
let existingVehicleNumber = "А123АА761"
let nonExistingVehicleNumber = "А999АА761"
let testColor = "testColor"
var storageServiceMock: MockStorageServiceProtocol
var settingsServiceMock: MockSettingsServiceProtocol
var apiServiceMock: MockApiServiceProtocol
var viewModel: ReportViewModel
init() {
storageServiceMock = MockStorageServiceProtocol()
settingsServiceMock = MockSettingsServiceProtocol()
apiServiceMock = MockApiServiceProtocol()
ServiceContainer.shared.register(StorageServiceProtocol.self, instance: storageServiceMock)
ServiceContainer.shared.register(SettingsServiceProtocol.self, instance: settingsServiceMock)
ServiceContainer.shared.register(ApiServiceProtocol.self, instance: apiServiceMock)
viewModel = ReportViewModel(vehicle: VehicleDto(), isPersistent: true)
}
@Test("Load vehicle detail")
func loadVehicleDetail() async throws {
let incompleteVehicleModel = VehicleDto(number: existingVehicleNumber)
let fullVehicleModel = VehicleDto(number: existingVehicleNumber, color: testColor)
viewModel = ReportViewModel(vehicle: incompleteVehicleModel, isPersistent: true)
#expect(viewModel.vehicle.color == nil)
given(storageServiceMock)
.loadVehicle(number: .value(existingVehicleNumber))
.willReturn(fullVehicleModel)
await viewModel.onAppear()
verify(storageServiceMock)
.loadVehicle(number: .value(existingVehicleNumber))
.called(.once)
#expect(viewModel.vehicle.color == testColor)
#expect(viewModel.hud == nil)
}
@Test("Load vehicle error")
func loadVehicleError() async throws {
viewModel = ReportViewModel(vehicle: VehicleDto(), isPersistent: true)
given(storageServiceMock)
.loadVehicle(number: .any)
.willThrow(StorageError.vehicleNotFound)
await viewModel.onAppear()
verify(storageServiceMock)
.loadVehicle(number: .any)
.called(.once)
#expect(viewModel.hud == .error(StorageError.vehicleNotFound))
}
@Test("Show debug info", arguments: [true, false])
func showDebugInfo(value: Bool) async throws {
viewModel = ReportViewModel(vehicle: VehicleDto(), isPersistent: false)
given(settingsServiceMock)
.showDebugInfo.willReturn(value)
#expect(viewModel.showDebugInfo == value)
}
@Test("Check GB bot report", arguments: [true, false])
func checkGbBotReport(isPersistent: Bool) async throws {
let vehicle = VehicleDto(number: existingVehicleNumber)
let updatedVehicle = VehicleDto(number: existingVehicleNumber, color: testColor)
viewModel = ReportViewModel(vehicle: vehicle, isPersistent: isPersistent)
given(apiServiceMock)
.checkVehicleGb(by: .value(existingVehicleNumber))
.willReturn(updatedVehicle)
given(storageServiceMock)
.updateVehicleIfExists(dto: .value(updatedVehicle))
.willReturn()
await viewModel.checkGB()
verify(apiServiceMock)
.checkVehicleGb(by: .value(existingVehicleNumber))
.called(.once)
verify(storageServiceMock)
.updateVehicleIfExists(dto: .value(updatedVehicle))
.called(.once)
#expect(viewModel.vehicle.color == testColor)
#expect(viewModel.hud == nil)
}
@Test("Check GB error", arguments: [true, false])
func checkGbError(isPersistent: Bool) async throws {
let vehicle = VehicleDto(number: existingVehicleNumber)
viewModel = ReportViewModel(vehicle: vehicle, isPersistent: isPersistent)
given(apiServiceMock)
.checkVehicleGb(by: .value(existingVehicleNumber))
.willThrow(TestError.generic)
await viewModel.checkGB()
verify(apiServiceMock)
.checkVehicleGb(by: .value(existingVehicleNumber))
.called(.once)
#expect(viewModel.hud == .error(TestError.generic))
}
}