diff --git a/Yosemite/Yosemite/Stores/DomainStore.swift b/Yosemite/Yosemite/Stores/DomainStore.swift index e3e4b8d3a7e..166786ca846 100644 --- a/Yosemite/Yosemite/Stores/DomainStore.swift +++ b/Yosemite/Yosemite/Stores/DomainStore.swift @@ -48,7 +48,9 @@ private extension DomainStore { } func loadDomains(siteID: Int64, completion: @escaping (Result<[SiteDomain], Error>) -> Void) { - // TODO: 8558 - fetch a site's domains from the remote. - completion(.success([.init(name: "gotrees.wpcomstaging.com", isPrimary: true, renewalDate: nil)])) + Task { @MainActor in + let result = await Result { try await remote.loadDomains(siteID: siteID) } + completion(result) + } } } diff --git a/Yosemite/Yosemite/Stores/PaymentStore.swift b/Yosemite/Yosemite/Stores/PaymentStore.swift index 45ef7c0af31..34e629ed31a 100644 --- a/Yosemite/Yosemite/Stores/PaymentStore.swift +++ b/Yosemite/Yosemite/Stores/PaymentStore.swift @@ -62,9 +62,11 @@ private extension PaymentStore { } func loadSiteCurrentPlan(siteID: Int64, - completion: (Result) -> Void) { - // TODO: 8558 - fetch site's current plan - completion(.success(.init(hasDomainCredit: true))) + completion: @escaping (Result) -> Void) { + Task { @MainActor in + let result = await Result { try await remote.loadSiteCurrentPlan(siteID: siteID) } + completion(result) + } } func createCart(productID: String, diff --git a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockDomainRemote.swift b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockDomainRemote.swift index 97874da9932..2bdbfdd8c68 100644 --- a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockDomainRemote.swift +++ b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockDomainRemote.swift @@ -7,10 +7,18 @@ final class MockDomainRemote { /// The results to return in `loadDomainSuggestions`. private var loadDomainSuggestionsResult: Result<[FreeDomainSuggestion], Error>? + /// The results to return in `loadDomains`. + private var loadDomainsResult: Result<[SiteDomain], Error>? + /// Returns the value when `loadDomainSuggestions` is called. func whenLoadingDomainSuggestions(thenReturn result: Result<[FreeDomainSuggestion], Error>) { loadDomainSuggestionsResult = result } + + /// Returns the value when `loadDomains` is called. + func whenLoadingDomains(thenReturn result: Result<[SiteDomain], Error>) { + loadDomainsResult = result + } } extension MockDomainRemote: DomainRemoteProtocol { @@ -23,7 +31,10 @@ extension MockDomainRemote: DomainRemoteProtocol { } func loadDomains(siteID: Int64) async throws -> [SiteDomain] { - // TODO: 8558 - Yosemite layer - throw NetworkError.notFound + guard let result = loadDomainsResult else { + XCTFail("Could not find result for loading domains.") + throw NetworkError.notFound + } + return try result.get() } } diff --git a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockPaymentRemote.swift b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockPaymentRemote.swift index 9d92e08f907..bd76c87cb28 100644 --- a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockPaymentRemote.swift +++ b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockPaymentRemote.swift @@ -7,6 +7,9 @@ final class MockPaymentRemote { /// The results to return in `loadPlan`. private var loadPlanResult: Result? + /// The results to return in `loadSiteCurrentPlan`. + private var loadSiteCurrentPlanResult: Result? + /// The results to return in `createCart`. private var createCartResult: Result? @@ -15,6 +18,11 @@ final class MockPaymentRemote { loadPlanResult = result } + /// Returns the value when `loadSiteCurrentPlan` is called. + func whenLoadingSiteCurrentPlan(thenReturn result: Result) { + loadSiteCurrentPlanResult = result + } + /// Returns the value when `createCart` is called. func whenCreatingCart(thenReturn result: Result) { createCartResult = result @@ -22,7 +30,7 @@ final class MockPaymentRemote { } extension MockPaymentRemote: PaymentRemoteProtocol { - func loadPlan(thatMatchesID productID: Int64) async throws -> Networking.WPComPlan { + func loadPlan(thatMatchesID productID: Int64) async throws -> WPComPlan { guard let result = loadPlanResult else { XCTFail("Could not find result for loading a plan.") throw NetworkError.notFound @@ -31,8 +39,11 @@ extension MockPaymentRemote: PaymentRemoteProtocol { } func loadSiteCurrentPlan(siteID: Int64) async throws -> WPComSitePlan { - // TODO: 8558 - Yosemite layer - throw NetworkError.notFound + guard let result = loadSiteCurrentPlanResult else { + XCTFail("Could not find result for loading a site's current plan.") + throw NetworkError.notFound + } + return try result.get() } func createCart(siteID: Int64, productID: Int64) async throws { diff --git a/Yosemite/YosemiteTests/Stores/DomainStoreTests.swift b/Yosemite/YosemiteTests/Stores/DomainStoreTests.swift index 1f214142e01..14e7584fca6 100644 --- a/Yosemite/YosemiteTests/Stores/DomainStoreTests.swift +++ b/Yosemite/YosemiteTests/Stores/DomainStoreTests.swift @@ -33,6 +33,8 @@ final class DomainStoreTests: XCTestCase { super.tearDown() } + // MARK: - `loadFreeDomainSuggestions` + func test_loadFreeDomainSuggestions_returns_suggestions_on_success() throws { // Given remote.whenLoadingDomainSuggestions(thenReturn: .success([.init(name: "freedomaintesting", isFree: false)])) @@ -68,4 +70,46 @@ final class DomainStoreTests: XCTestCase { let error = try XCTUnwrap(result.failure) XCTAssertEqual(error as? NetworkError, .timeout) } + + // MARK: - `loadDomains` + + func test_loadDomains_returns_domains_on_success() throws { + // Given + remote.whenLoadingDomains(thenReturn: .success([ + .init(name: "candy.land", isPrimary: true, renewalDate: .distantFuture), + .init(name: "pods.pro", isPrimary: true) + ])) + + // When + let result: Result<[SiteDomain], Error> = waitFor { promise in + self.store.onAction(DomainAction.loadDomains(siteID: 606) { result in + promise(result) + }) + } + + // Then + XCTAssertTrue(result.isSuccess) + let suggestions = try XCTUnwrap(result.get()) + XCTAssertEqual(suggestions, [ + .init(name: "candy.land", isPrimary: true, renewalDate: .distantFuture), + .init(name: "pods.pro", isPrimary: true) + ]) + } + + func test_loadDomains_returns_error_on_failure() throws { + // Given + remote.whenLoadingDomains(thenReturn: .failure(NetworkError.timeout)) + + // When + let result: Result<[SiteDomain], Error> = waitFor { promise in + self.store.onAction(DomainAction.loadDomains(siteID: 606) { result in + promise(result) + }) + } + + // Then + XCTAssertTrue(result.isFailure) + let error = try XCTUnwrap(result.failure) + XCTAssertEqual(error as? NetworkError, .timeout) + } } diff --git a/Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift b/Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift index c4d3e347b82..95a497394fc 100644 --- a/Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift +++ b/Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift @@ -70,6 +70,42 @@ final class PaymentStoreTests: XCTestCase { XCTAssertEqual(error as? NetworkError, .timeout) } + // MARK: - `loadSiteCurrentPlan` + + func test_loadSiteCurrentPlan_returns_plan_on_success() throws { + // Given + remote.whenLoadingSiteCurrentPlan(thenReturn: .success(.init(hasDomainCredit: true))) + + // When + let result = waitFor { promise in + self.store.onAction(PaymentAction.loadSiteCurrentPlan(siteID: 645) { result in + promise(result) + }) + } + + // Then + XCTAssertTrue(result.isSuccess) + let plan = try XCTUnwrap(result.get()) + XCTAssertEqual(plan, .init(hasDomainCredit: true)) + } + + func test_loadSiteCurrentPlan_returns_failure_on_error() throws { + // Given + remote.whenLoadingSiteCurrentPlan(thenReturn: .failure(NetworkError.timeout)) + + // When + let result = waitFor { promise in + self.store.onAction(PaymentAction.loadSiteCurrentPlan(siteID: 645) { result in + promise(result) + }) + } + + // Then + XCTAssertTrue(result.isFailure) + let error = try XCTUnwrap(result.failure) + XCTAssertEqual(error as? NetworkError, .timeout) + } + // MARK: - `createCart` func test_createCart_returns_on_success() throws {