Skip to content

Commit ac96d0d

Browse files
committed
Yosemite layer changes for 2 domain settings actions.
1 parent e725b3b commit ac96d0d

File tree

6 files changed

+122
-6
lines changed

6 files changed

+122
-6
lines changed

Yosemite/Yosemite/Stores/DomainStore.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ private extension DomainStore {
4848
}
4949

5050
func loadDomains(siteID: Int64, completion: @escaping (Result<[SiteDomain], Error>) -> Void) {
51-
// TODO: 8558 - fetch a site's domains from the remote.
52-
completion(.success([.init(name: "gotrees.wpcomstaging.com", isPrimary: true, renewalDate: nil)]))
51+
Task { @MainActor in
52+
let result = await Result { try await remote.loadDomains(siteID: siteID) }
53+
completion(result)
54+
}
5355
}
5456
}

Yosemite/Yosemite/Stores/PaymentStore.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ private extension PaymentStore {
6262
}
6363

6464
func loadSiteCurrentPlan(siteID: Int64,
65-
completion: (Result<WPComSitePlan, Error>) -> Void) {
66-
// TODO: 8558 - fetch site's current plan
67-
completion(.success(.init(plan: .init(productID: 0, name: "", formattedPrice: ""), hasDomainCredit: true)))
65+
completion: @escaping (Result<WPComSitePlan, Error>) -> Void) {
66+
Task { @MainActor in
67+
let result = await Result { try await remote.loadSiteCurrentPlan(siteID: siteID) }
68+
completion(result)
69+
}
6870
}
6971

7072
func createCart(productID: String,

Yosemite/YosemiteTests/Mocks/Networking/Remote/MockDomainRemote.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ final class MockDomainRemote {
77
/// The results to return in `loadDomainSuggestions`.
88
private var loadDomainSuggestionsResult: Result<[FreeDomainSuggestion], Error>?
99

10+
/// The results to return in `loadDomains`.
11+
private var loadDomainsResult: Result<[SiteDomain], Error>?
12+
1013
/// Returns the value when `loadDomainSuggestions` is called.
1114
func whenLoadingDomainSuggestions(thenReturn result: Result<[FreeDomainSuggestion], Error>) {
1215
loadDomainSuggestionsResult = result
1316
}
17+
18+
/// Returns the value when `loadDomains` is called.
19+
func whenLoadingDomains(thenReturn result: Result<[SiteDomain], Error>) {
20+
loadDomainsResult = result
21+
}
1422
}
1523

1624
extension MockDomainRemote: DomainRemoteProtocol {
@@ -21,4 +29,12 @@ extension MockDomainRemote: DomainRemoteProtocol {
2129
}
2230
return try result.get()
2331
}
32+
33+
func loadDomains(siteID: Int64) async throws -> [SiteDomain] {
34+
guard let result = loadDomainsResult else {
35+
XCTFail("Could not find result for loading domains.")
36+
throw NetworkError.notFound
37+
}
38+
return try result.get()
39+
}
2440
}

Yosemite/YosemiteTests/Mocks/Networking/Remote/MockPaymentRemote.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ final class MockPaymentRemote {
77
/// The results to return in `loadPlan`.
88
private var loadPlanResult: Result<WPComPlan, Error>?
99

10+
/// The results to return in `loadSiteCurrentPlan`.
11+
private var loadSiteCurrentPlanResult: Result<WPComSitePlan, Error>?
12+
1013
/// The results to return in `createCart`.
1114
private var createCartResult: Result<Void, Error>?
1215

@@ -15,21 +18,34 @@ final class MockPaymentRemote {
1518
loadPlanResult = result
1619
}
1720

21+
/// Returns the value when `loadSiteCurrentPlan` is called.
22+
func whenLoadingSiteCurrentPlan(thenReturn result: Result<WPComSitePlan, Error>) {
23+
loadSiteCurrentPlanResult = result
24+
}
25+
1826
/// Returns the value when `createCart` is called.
1927
func whenCreatingCart(thenReturn result: Result<Void, Error>) {
2028
createCartResult = result
2129
}
2230
}
2331

2432
extension MockPaymentRemote: PaymentRemoteProtocol {
25-
func loadPlan(thatMatchesID productID: Int64) async throws -> Networking.WPComPlan {
33+
func loadPlan(thatMatchesID productID: Int64) async throws -> WPComPlan {
2634
guard let result = loadPlanResult else {
2735
XCTFail("Could not find result for loading a plan.")
2836
throw NetworkError.notFound
2937
}
3038
return try result.get()
3139
}
3240

41+
func loadSiteCurrentPlan(siteID: Int64) async throws -> WPComSitePlan {
42+
guard let result = loadSiteCurrentPlanResult else {
43+
XCTFail("Could not find result for loading a site's current plan.")
44+
throw NetworkError.notFound
45+
}
46+
return try result.get()
47+
}
48+
3349
func createCart(siteID: Int64, productID: Int64) async throws {
3450
guard let result = createCartResult else {
3551
XCTFail("Could not find result for creating a cart.")

Yosemite/YosemiteTests/Stores/DomainStoreTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ final class DomainStoreTests: XCTestCase {
3333
super.tearDown()
3434
}
3535

36+
// MARK: - `loadFreeDomainSuggestions`
37+
3638
func test_loadFreeDomainSuggestions_returns_suggestions_on_success() throws {
3739
// Given
3840
remote.whenLoadingDomainSuggestions(thenReturn: .success([.init(name: "freedomaintesting", isFree: false)]))
@@ -68,4 +70,46 @@ final class DomainStoreTests: XCTestCase {
6870
let error = try XCTUnwrap(result.failure)
6971
XCTAssertEqual(error as? NetworkError, .timeout)
7072
}
73+
74+
// MARK: - `loadDomains`
75+
76+
func test_loadDomains_returns_domains_on_success() throws {
77+
// Given
78+
remote.whenLoadingDomains(thenReturn: .success([
79+
.init(name: "candy.land", isPrimary: true, renewalDate: .distantFuture),
80+
.init(name: "pods.pro", isPrimary: true)
81+
]))
82+
83+
// When
84+
let result: Result<[SiteDomain], Error> = waitFor { promise in
85+
self.store.onAction(DomainAction.loadDomains(siteID: 606) { result in
86+
promise(result)
87+
})
88+
}
89+
90+
// Then
91+
XCTAssertTrue(result.isSuccess)
92+
let suggestions = try XCTUnwrap(result.get())
93+
XCTAssertEqual(suggestions, [
94+
.init(name: "candy.land", isPrimary: true, renewalDate: .distantFuture),
95+
.init(name: "pods.pro", isPrimary: true)
96+
])
97+
}
98+
99+
func test_loadDomains_returns_error_on_failure() throws {
100+
// Given
101+
remote.whenLoadingDomains(thenReturn: .failure(NetworkError.timeout))
102+
103+
// When
104+
let result: Result<[SiteDomain], Error> = waitFor { promise in
105+
self.store.onAction(DomainAction.loadDomains(siteID: 606) { result in
106+
promise(result)
107+
})
108+
}
109+
110+
// Then
111+
XCTAssertTrue(result.isFailure)
112+
let error = try XCTUnwrap(result.failure)
113+
XCTAssertEqual(error as? NetworkError, .timeout)
114+
}
71115
}

Yosemite/YosemiteTests/Stores/PaymentStoreTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ final class PaymentStoreTests: XCTestCase {
7070
XCTAssertEqual(error as? NetworkError, .timeout)
7171
}
7272

73+
// MARK: - `loadSiteCurrentPlan`
74+
75+
func test_loadSiteCurrentPlan_returns_plan_on_success() throws {
76+
// Given
77+
remote.whenLoadingSiteCurrentPlan(thenReturn: .success(.init(hasDomainCredit: true)))
78+
79+
// When
80+
let result = waitFor { promise in
81+
self.store.onAction(PaymentAction.loadSiteCurrentPlan(siteID: 645) { result in
82+
promise(result)
83+
})
84+
}
85+
86+
// Then
87+
XCTAssertTrue(result.isSuccess)
88+
let plan = try XCTUnwrap(result.get())
89+
XCTAssertEqual(plan, .init(hasDomainCredit: true))
90+
}
91+
92+
func test_loadSiteCurrentPlan_returns_failure_on_error() throws {
93+
// Given
94+
remote.whenLoadingSiteCurrentPlan(thenReturn: .failure(NetworkError.timeout))
95+
96+
// When
97+
let result = waitFor { promise in
98+
self.store.onAction(PaymentAction.loadSiteCurrentPlan(siteID: 645) { result in
99+
promise(result)
100+
})
101+
}
102+
103+
// Then
104+
XCTAssertTrue(result.isFailure)
105+
let error = try XCTUnwrap(result.failure)
106+
XCTAssertEqual(error as? NetworkError, .timeout)
107+
}
108+
73109
// MARK: - `createCart`
74110

75111
func test_createCart_returns_on_success() throws {

0 commit comments

Comments
 (0)