124 lines
4.1 KiB
Swift
124 lines
4.1 KiB
Swift
//
|
|
// NotesTests.swift
|
|
// AutoCatTests
|
|
//
|
|
// Created by Selim Mustafaev on 13.07.2024.
|
|
// Copyright © 2024 Selim Mustafaev. All rights reserved.
|
|
//
|
|
|
|
import Testing
|
|
import AutoCatCore
|
|
import MockableTest
|
|
@testable import AutoCat
|
|
|
|
@MainActor
|
|
final class NotesTests {
|
|
|
|
var storageServiceMock: StorageServiceMock
|
|
var apiServiceMock: ApiServiceMock
|
|
|
|
var viewModel: NotesViewModel
|
|
|
|
let noteText = "Test note text"
|
|
let noteTextModified = "Test note text modified"
|
|
|
|
lazy var vehicleWithNote: VehicleDto = .normal.addNote(text: noteText)
|
|
lazy var unrecognizedVehicleWithNote: VehicleDto = .unrecognized.addNote(text: noteText)
|
|
|
|
init() {
|
|
storageServiceMock = StorageServiceMock()
|
|
apiServiceMock = ApiServiceMock()
|
|
|
|
ServiceContainer.shared.register(StorageServiceProtocol.self, instance: storageServiceMock)
|
|
ServiceContainer.shared.register(ApiServiceProtocol.self, instance: apiServiceMock)
|
|
viewModel = NotesViewModel(vehicle: VehicleDto())
|
|
}
|
|
|
|
@Test("Add note (normal vehicle)")
|
|
func addNote() async throws {
|
|
|
|
// given(apiServiceMock)
|
|
// .add(notes: .any, to: .any).willReturn(vehicleWithNote)
|
|
|
|
await apiServiceMock.setVehicle(vehicleWithNote)
|
|
viewModel.vehicle = .normal
|
|
|
|
await viewModel.addNote(text: noteText)
|
|
|
|
// verify(apiServiceMock)
|
|
// .add(notes: .any, to: .any).called(.once)
|
|
//
|
|
// verify(storageServiceMock)
|
|
// .addNote(text: .any, to: .any).called(.never)
|
|
// .updateVehicleIfExists(dto: .any).called(.once)
|
|
|
|
#expect(viewModel.vehicle.notes.contains { $0.text == noteText })
|
|
#expect(viewModel.hud == nil)
|
|
}
|
|
|
|
@Test("Add note (unrecognized vehicle)")
|
|
func addNoteUnrecognized() async throws {
|
|
|
|
await storageServiceMock.setVehicle(vehicleWithNote)
|
|
viewModel.vehicle = .unrecognized
|
|
|
|
await viewModel.addNote(text: noteText)
|
|
|
|
#expect(viewModel.vehicle.notes.contains { $0.text == noteText })
|
|
#expect(viewModel.hud == nil)
|
|
}
|
|
|
|
@Test("Edit note (normal vehicle)")
|
|
func editNote() async throws {
|
|
|
|
let noteId = try #require(vehicleWithNote.notes.first?.id)
|
|
await apiServiceMock.setVehicle(.normal.addNote(text: noteTextModified, id: noteId))
|
|
viewModel.vehicle = vehicleWithNote
|
|
|
|
await viewModel.editNote(id: noteId, text: noteTextModified)
|
|
|
|
#expect(viewModel.vehicle.notes.contains { $0.text == noteTextModified })
|
|
#expect(viewModel.hud == nil)
|
|
}
|
|
|
|
@Test("Edit note (unrecognized vehicle)")
|
|
func editNoteUnrecognized() async throws {
|
|
|
|
let vehicle: VehicleDto = .unrecognized.addNote(text: noteText)
|
|
let noteId = try #require(vehicle.notes.first?.id)
|
|
await storageServiceMock.setVehicle(.unrecognized.addNote(text: noteTextModified, id: noteId))
|
|
viewModel.vehicle = vehicle
|
|
|
|
await viewModel.editNote(id: noteId, text: noteTextModified)
|
|
|
|
#expect(viewModel.vehicle.notes.contains { $0.text == noteTextModified })
|
|
#expect(viewModel.hud == nil)
|
|
}
|
|
|
|
@Test("Delete note (normal vehicle)")
|
|
func deleteNote() async throws {
|
|
|
|
await apiServiceMock.setVehicle(.normal)
|
|
viewModel.vehicle = vehicleWithNote
|
|
let noteId = try #require(vehicleWithNote.notes.first?.id)
|
|
|
|
await viewModel.deleteNote(id: noteId)
|
|
|
|
#expect(!viewModel.vehicle.notes.contains { $0.text == noteText })
|
|
#expect(viewModel.hud == nil)
|
|
}
|
|
|
|
@Test("Delete note (unrecognized vehicle)")
|
|
func deleteNoteUnrecognized() async throws {
|
|
|
|
await storageServiceMock.setVehicle(.unrecognized)
|
|
viewModel.vehicle = unrecognizedVehicleWithNote
|
|
let noteId = try #require(unrecognizedVehicleWithNote.notes.first?.id)
|
|
|
|
await viewModel.deleteNote(id: noteId)
|
|
|
|
#expect(!viewModel.vehicle.notes.contains { $0.text == noteText })
|
|
#expect(viewModel.hud == nil)
|
|
}
|
|
}
|