AutoCat/AutoCatTests/FiltersTests.swift

141 lines
4.9 KiB
Swift

//
// FiltersTests.swift
// AutoCatTests
//
// Created by Selim Mustafaev on 12.11.2024.
// Copyright © 2024 Selim Mustafaev. All rights reserved.
//
import Foundation
import Testing
import AutoCatCore
import Mockable
@testable import AutoCat
@MainActor
struct FiltersTests {
var settingsServiceMock = MockSettingsServiceProtocol()
var apiServiceMock = MockApiServiceProtocol()
var viewModel: FiltersViewModel
let testBrand = "testBrand"
let testColor = "testColor"
let testModel = "testModel"
let testRegion = VehicleRegion(name: "testRegion", codes: [111])
let testYear = 2222
init() {
viewModel = FiltersViewModel(apiService: apiServiceMock, filter: Filter())
}
@Test("Main filters data loaded")
func mainDataLoaded() async {
given(apiServiceMock)
.getBrands().willReturn([testBrand])
.getColors().willReturn([testColor])
.getRegions().willReturn([testRegion])
.getYears().willReturn([testYear])
.getModels(of: .value(testBrand)).willReturn([testModel])
await viewModel.loadData()
await viewModel.loadModels(brand: testBrand)
verify(apiServiceMock)
.getBrands().called(.once)
.getColors().called(.once)
.getYears().called(.once)
.getModels(of: .value(testBrand)).called(.once)
.getRegions().called(.never)
#expect(viewModel.brands == [.any, .value(testBrand)])
#expect(viewModel.colors == [.any, .value(testColor)])
#expect(viewModel.years == [.any, .value(String(testYear))])
#expect(viewModel.models == [.any, .value(testModel)])
}
@Test("Try second load")
func trySecondLoad() async throws {
viewModel.brands.append(.value(testBrand))
viewModel.colors.append(.value(testColor))
viewModel.years.append(.value(String(testYear)))
viewModel.models.append(.value(testModel))
await viewModel.loadData()
verify(apiServiceMock)
.getBrands().called(.never)
.getColors().called(.never)
.getYears().called(.never)
.getModels(of: .any).called(.never)
.getRegions().called(.never)
#expect(viewModel.brands == [.any, .value(testBrand)])
#expect(viewModel.colors == [.any, .value(testColor)])
#expect(viewModel.years == [.any, .value(String(testYear))])
#expect(viewModel.models == [.any, .value(testModel)])
}
@Test("Load data error")
func loadDataError() async throws {
given(apiServiceMock)
.getBrands().willThrow(TestError.generic)
.getColors().willThrow(TestError.generic)
.getRegions().willThrow(TestError.generic)
.getYears().willThrow(TestError.generic)
.getModels(of: .any).willThrow(TestError.generic)
await viewModel.loadData()
await viewModel.loadModels(brand: testBrand)
#expect(viewModel.brands == [.any])
#expect(viewModel.colors == [.any])
#expect(viewModel.years == [.any])
#expect(viewModel.models == [.any])
}
@Test("Clear all filters")
func clearAllFilters() async throws {
viewModel.currentBrand = .value(testBrand)
viewModel.filter.brand = .value(testBrand)
viewModel.filter.color = .value(testColor)
viewModel.filter.year = .value(String(testYear))
viewModel.filter.model = .value(testModel)
viewModel.clearAllFilters()
#expect(viewModel.filter.brand == .any)
#expect(viewModel.filter.color == .any)
#expect(viewModel.filter.year == .any)
#expect(viewModel.filter.model == .any)
}
@Test("Nullify time in dates")
func nullifyTimeInDates() async throws {
let testTimestamp: TimeInterval = 1732958508
let nullifiedTimestamp: TimeInterval = 1732914000
// Date with non-zero time components
let date = Date(timeIntervalSince1970: testTimestamp)
viewModel.filter.fromDate = date
viewModel.filter.toDate = date
viewModel.filter.fromDateUpdated = date
viewModel.filter.toDateUpdated = date
viewModel.filter.fromLocationDate = date
viewModel.filter.toLocationDate = date
#expect(viewModel.filter.fromDate?.timeIntervalSince1970 == nullifiedTimestamp)
#expect(viewModel.filter.toDate?.timeIntervalSince1970 == nullifiedTimestamp)
#expect(viewModel.filter.fromDateUpdated?.timeIntervalSince1970 == nullifiedTimestamp)
#expect(viewModel.filter.toDateUpdated?.timeIntervalSince1970 == nullifiedTimestamp)
#expect(viewModel.filter.fromLocationDate?.timeIntervalSince1970 == nullifiedTimestamp)
#expect(viewModel.filter.toLocationDate?.timeIntervalSince1970 == nullifiedTimestamp)
}
}