|
| 1 | +import Combine |
| 2 | +import Foundation |
| 3 | +import Networking |
| 4 | + |
| 5 | +import XCTest |
| 6 | + |
| 7 | +/// Mock for `AccountRemote`. |
| 8 | +/// |
| 9 | +final class MockAccountRemote { |
| 10 | + /// Returns the value as a publisher when `loadSites` is called. |
| 11 | + var loadSitesResult: Result<[Site], Error> = .success([]) |
| 12 | + |
| 13 | + /// Returns the requests that have been made to `AccountRemoteProtocol`. |
| 14 | + var invocations = [Invocation]() |
| 15 | + |
| 16 | + /// The results to return based on the given site ID in `checkIfWooCommerceIsActive` |
| 17 | + private var checkIfWooCommerceIsActiveResultsBySiteID = [Int64: Result<Bool, Error>]() |
| 18 | + |
| 19 | + /// The results to return based on the given site ID in `fetchWordPressSiteSettings` |
| 20 | + private var fetchWordPressSiteSettingsResultsBySiteID = [Int64: Result<WordPressSiteSettings, Error>]() |
| 21 | + |
| 22 | + /// Returns the value as a publisher when `checkIfWooCommerceIsActive` is called. |
| 23 | + func whenCheckingIfWooCommerceIsActive(siteID: Int64, thenReturn result: Result<Bool, Error>) { |
| 24 | + checkIfWooCommerceIsActiveResultsBySiteID[siteID] = result |
| 25 | + } |
| 26 | + |
| 27 | + /// Returns the value as a publisher when `fetchWordPressSiteSettings` is called. |
| 28 | + func whenFetchingWordPressSiteSettings(siteID: Int64, thenReturn result: Result<WordPressSiteSettings, Error>) { |
| 29 | + fetchWordPressSiteSettingsResultsBySiteID[siteID] = result |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension MockAccountRemote { |
| 34 | + enum Invocation: Equatable { |
| 35 | + case loadSites |
| 36 | + case checkIfWooCommerceIsActive(siteID: Int64) |
| 37 | + case fetchWordPressSiteSettings(siteID: Int64) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// MARK: - AccountRemoteProtocol |
| 42 | + |
| 43 | +extension MockAccountRemote: AccountRemoteProtocol { |
| 44 | + func loadAccount(completion: @escaping (Result<Account, Error>) -> Void) { |
| 45 | + // no-op |
| 46 | + } |
| 47 | + |
| 48 | + func loadAccountSettings(for userID: Int64, completion: @escaping (Result<AccountSettings, Error>) -> Void) { |
| 49 | + // no-op |
| 50 | + } |
| 51 | + |
| 52 | + func updateAccountSettings(for userID: Int64, tracksOptOut: Bool, completion: @escaping (Result<AccountSettings, Error>) -> Void) { |
| 53 | + // no-op |
| 54 | + } |
| 55 | + |
| 56 | + func loadSites() -> AnyPublisher<Result<[Site], Error>, Never> { |
| 57 | + invocations.append(.loadSites) |
| 58 | + return Just<Result<[Site], Error>>(loadSitesResult).eraseToAnyPublisher() |
| 59 | + } |
| 60 | + |
| 61 | + func checkIfWooCommerceIsActive(for siteID: Int64) -> AnyPublisher<Result<Bool, Error>, Never> { |
| 62 | + invocations.append(.checkIfWooCommerceIsActive(siteID: siteID)) |
| 63 | + if let result = checkIfWooCommerceIsActiveResultsBySiteID[siteID] { |
| 64 | + return Just<Result<Bool, Error>>(result).eraseToAnyPublisher() |
| 65 | + } else { |
| 66 | + XCTFail("\(String(describing: self)) Could not find result for site ID: \(siteID)") |
| 67 | + return Empty<Result<Bool, Error>, Never>().eraseToAnyPublisher() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + func fetchWordPressSiteSettings(for siteID: Int64) -> AnyPublisher<Result<WordPressSiteSettings, Error>, Never> { |
| 72 | + invocations.append(.fetchWordPressSiteSettings(siteID: siteID)) |
| 73 | + if let result = fetchWordPressSiteSettingsResultsBySiteID[siteID] { |
| 74 | + return Just<Result<WordPressSiteSettings, Error>>(result).eraseToAnyPublisher() |
| 75 | + } else { |
| 76 | + XCTFail("\(String(describing: self)) Could not find result for site ID: \(siteID)") |
| 77 | + return Empty<Result<WordPressSiteSettings, Error>, Never>().eraseToAnyPublisher() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func loadSitePlan(for siteID: Int64, completion: @escaping (Result<SitePlan, Error>) -> Void) { |
| 82 | + // no-op |
| 83 | + } |
| 84 | +} |
0 commit comments