Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Yosemite/Yosemite/Stores/DomainStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
8 changes: 5 additions & 3 deletions Yosemite/Yosemite/Stores/PaymentStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ private extension PaymentStore {
}

func loadSiteCurrentPlan(siteID: Int64,
completion: (Result<WPComSitePlan, Error>) -> Void) {
// TODO: 8558 - fetch site's current plan
completion(.success(.init(hasDomainCredit: true)))
completion: @escaping (Result<WPComSitePlan, Error>) -> Void) {
Task { @MainActor in
let result = await Result { try await remote.loadSiteCurrentPlan(siteID: siteID) }
completion(result)
}
}

func createCart(productID: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ final class MockPaymentRemote {
/// The results to return in `loadPlan`.
private var loadPlanResult: Result<WPComPlan, Error>?

/// The results to return in `loadSiteCurrentPlan`.
private var loadSiteCurrentPlanResult: Result<WPComSitePlan, Error>?

/// The results to return in `createCart`.
private var createCartResult: Result<Void, Error>?

Expand All @@ -15,14 +18,19 @@ final class MockPaymentRemote {
loadPlanResult = result
}

/// Returns the value when `loadSiteCurrentPlan` is called.
func whenLoadingSiteCurrentPlan(thenReturn result: Result<WPComSitePlan, Error>) {
loadSiteCurrentPlanResult = result
}

/// Returns the value when `createCart` is called.
func whenCreatingCart(thenReturn result: Result<Void, Error>) {
createCartResult = result
}
}

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
Expand All @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions Yosemite/YosemiteTests/Stores/DomainStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)]))
Expand Down Expand Up @@ -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)
}
}
36 changes: 36 additions & 0 deletions Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down