Skip to content

Commit cf833a4

Browse files
committed
Encapsulate dependencies into a struct
1 parent 0295e2a commit cf833a4

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/In-Person Payments/Onboarding Errors/InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel.swift

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,56 @@ import Foundation
22
import Yosemite
33

44
final class InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel: ObservableObject {
5+
// MARK: - Dependencies
6+
struct Dependencies {
7+
let stores: StoresManager
8+
let noticePresenter: NoticePresenter
9+
let analytics: Analytics
10+
11+
init(stores: StoresManager = ServiceLocator.stores,
12+
noticePresenter: NoticePresenter = ServiceLocator.noticePresenter,
13+
analytics: Analytics = ServiceLocator.analytics) {
14+
self.stores = stores
15+
self.noticePresenter = noticePresenter
16+
self.analytics = analytics
17+
}
18+
}
19+
20+
private let dependencies: Dependencies
21+
22+
private var stores: StoresManager {
23+
dependencies.stores
24+
}
25+
26+
private var noticePresenter: NoticePresenter {
27+
dependencies.noticePresenter
28+
}
29+
30+
private var analytics: Analytics {
31+
dependencies.analytics
32+
}
33+
34+
// MARK: - Output properties
535
let completion: () -> Void
6-
private let stores: StoresManager
7-
private let noticePresenter: NoticePresenter
8-
private let analytics: Analytics
9-
private let cardPresentPaymentsConfiguration: CardPresentPaymentsConfiguration
1036

1137
@Published var awaitingResponse = false
1238

39+
// MARK: - Configuration properties
40+
private let cardPresentPaymentsConfiguration: CardPresentPaymentsConfiguration
41+
1342
private var siteID: Int64? {
1443
stores.sessionManager.defaultStoreID
1544
}
1645

17-
init(stores: StoresManager = ServiceLocator.stores,
18-
noticePresenter: NoticePresenter = ServiceLocator.noticePresenter,
19-
analytics: Analytics = ServiceLocator.analytics,
46+
init(dependencies: Dependencies = Dependencies(),
2047
configuration: CardPresentPaymentsConfiguration,
2148
completion: @escaping () -> Void) {
22-
self.stores = stores
23-
self.noticePresenter = noticePresenter
24-
self.analytics = analytics
49+
self.dependencies = dependencies
2550
self.cardPresentPaymentsConfiguration = configuration
2651
self.completion = completion
2752
}
2853

54+
// MARK: - Actions
2955
func skipTapped() {
3056
trackSkipTapped()
3157

WooCommerce/WooCommerceTests/ViewRelated/Dashboard/Settings/In-Person Payments/Onboarding Errors/InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModelTests.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModelTests:
1515

1616
private var configuration: CardPresentPaymentsConfiguration!
1717

18+
private var dependencies: InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel.Dependencies!
19+
1820
private var sut: InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel!
1921

2022
override func setUp() {
@@ -24,18 +26,20 @@ final class InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModelTests:
2426
analyticsProvider = MockAnalyticsProvider()
2527
analytics = WooAnalytics(analyticsProvider: analyticsProvider)
2628
configuration = CardPresentPaymentsConfiguration.init(country: "US")
27-
sut = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel(stores: stores,
28-
noticePresenter: noticePresenter,
29-
analytics: analytics,
29+
dependencies = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel.Dependencies(
30+
stores: stores,
31+
noticePresenter: noticePresenter,
32+
analytics: analytics
33+
)
34+
sut = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel(dependencies: dependencies,
3035
configuration: configuration,
3136
completion: {})
3237
}
3338

3439
func test_skip_always_calls_completion() {
3540
// Given
3641
let completionCalled: Bool = waitFor { promise in
37-
let sut = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel(stores: self.stores,
38-
noticePresenter: self.noticePresenter,
42+
let sut = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel(dependencies: self.dependencies,
3943
configuration: self.configuration,
4044
completion: {
4145
promise(true)
@@ -81,8 +85,7 @@ final class InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModelTests:
8185
}
8286

8387
let completionCalled: Bool = waitFor { promise in
84-
let sut = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel(stores: self.stores,
85-
noticePresenter: self.noticePresenter,
88+
let sut = InPersonPaymentsCashOnDeliveryPaymentGatewayNotSetUpViewModel(dependencies: self.dependencies,
8689
configuration: self.configuration,
8790
completion: {
8891
promise(true)

0 commit comments

Comments
 (0)