From db8dd912f95a8bf2d0b947f03e30a14d7d688a9e Mon Sep 17 00:00:00 2001 From: Diogo Autilio Date: Mon, 18 Mar 2024 07:54:44 -0300 Subject: [PATCH] Add SearchListInteractor --- .../Interactor/SearchListInteractor.swift | 26 ++++++++++ .../SearchListInteractorTests.swift | 49 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 SWDestinyTrades/Classes/Search/Interactor/SearchListInteractor.swift create mode 100644 SWDestinyTradesTests/Screens/Search/Interactor/SearchListInteractorTests.swift diff --git a/SWDestinyTrades/Classes/Search/Interactor/SearchListInteractor.swift b/SWDestinyTrades/Classes/Search/Interactor/SearchListInteractor.swift new file mode 100644 index 00000000..ad53c507 --- /dev/null +++ b/SWDestinyTrades/Classes/Search/Interactor/SearchListInteractor.swift @@ -0,0 +1,26 @@ +// +// SearchListInteractor.swift +// SWDestinyTrades +// +// Created by Diogo Autilio on 18/03/24. +// Copyright © 2024 Diogo Autilio. All rights reserved. +// + +import Foundation + +protocol SearchListInteractorProtocol { + func search(query: String) async throws -> [CardDTO] +} + +final class SearchListInteractor: SearchListInteractorProtocol { + + private let service: SWDestinyServiceProtocol + + init(service: SWDestinyServiceProtocol = SWDestinyService()) { + self.service = service + } + + func search(query: String) async throws -> [CardDTO] { + return try await service.search(query: query) + } +} diff --git a/SWDestinyTradesTests/Screens/Search/Interactor/SearchListInteractorTests.swift b/SWDestinyTradesTests/Screens/Search/Interactor/SearchListInteractorTests.swift new file mode 100644 index 00000000..16cca26e --- /dev/null +++ b/SWDestinyTradesTests/Screens/Search/Interactor/SearchListInteractorTests.swift @@ -0,0 +1,49 @@ +// +// SearchListInteractorTests.swift +// SWDestinyTradesTests +// +// Created by Diogo Autilio on 18/03/24. +// Copyright © 2024 Diogo Autilio. All rights reserved. +// + +import Foundation +import XCTest + +@testable import SWDestinyTrades + +final class SearchListInteractorTests: XCTestCase { + + private var sut: SearchListInteractor! + private var service: SWDestinyService! + private var client: HttpClientMock! + + override func setUpWithError() throws { + client = HttpClientMock() + service = SWDestinyService(client: client) + sut = SearchListInteractor(service: service) + } + + override func tearDownWithError() throws { + client = nil + service = nil + sut = nil + } + + func test_search_cards_with_success() async throws { + client.fileName = "card-list" + let cardList = try await sut.search(query: "panda") + + XCTAssertEqual(cardList.count, 22) + } + + func test_fail_to_search_cards() async throws { + client.error = true + + do { + _ = try await sut.search(query: "panda") + XCTFail("Expected to throw while awaiting, but succeeded") + } catch { + XCTAssertNotNil(error, "error must be not-nil") + } + } +}