Skip to content

Commit 7d48d5a

Browse files
adborbasclaude
andcommitted
Add CheckPluginError to distinguish plugin outdated from other failures
Show "Update plugin" button only when the plugin is actually outdated, otherwise show "Try again" for generic plugin check failures. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent bfb4265 commit 7d48d5a

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

WooCommerce/Classes/Authentication/WPComLogin/WPComConnectionSetup/WPComConnectionSetupViewModel.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import SwiftUI
55
@MainActor
66
final class WPComConnectionSetupViewModel: ObservableObject {
77

8+
enum CheckPluginError: Equatable {
9+
case outdated
10+
case other
11+
}
12+
813
private enum SetupState: Equatable {
914
case inProgress
1015
case completed
11-
case failed(step: SetupStep)
16+
case failed(step: SetupStep, checkPluginError: CheckPluginError? = nil)
1217
}
1318

1419
@Published private(set) var steps: [WPComConnectionSetupStep] = []
@@ -20,12 +25,12 @@ final class WPComConnectionSetupViewModel: ObservableObject {
2025
switch setupState {
2126
case .inProgress, .completed:
2227
return Localization.goToMyStore
23-
case .failed(let step):
28+
case .failed(let step, let checkPluginError):
2429
switch step {
2530
case .connect, .enablePush:
2631
return Localization.tryAgain
2732
case .checkPlugin:
28-
return Localization.updatePlugin
33+
return checkPluginError == .outdated ? Localization.updatePlugin : Localization.tryAgain
2934
}
3035
}
3136
}
@@ -35,7 +40,10 @@ final class WPComConnectionSetupViewModel: ObservableObject {
3540
}
3641

3742
var isShowingSecondaryButton: Bool {
38-
setupState == .failed(step: .checkPlugin)
43+
if case .failed(step: .checkPlugin, checkPluginError: .outdated) = setupState {
44+
return true
45+
}
46+
return false
3947
}
4048

4149
var secondaryButtonTitle: String {
@@ -87,12 +95,16 @@ final class WPComConnectionSetupViewModel: ObservableObject {
8795
switch setupState {
8896
case .completed:
8997
onGoToStore()
90-
case .failed(let step):
98+
case .failed(let step, let checkPluginError):
9199
switch step {
92100
case .connect, .enablePush:
93101
retrySetup()
94102
case .checkPlugin:
95-
onUpdatePlugin()
103+
if checkPluginError == .outdated {
104+
onUpdatePlugin()
105+
} else {
106+
retrySetup()
107+
}
96108
}
97109
case .inProgress:
98110
break
@@ -139,11 +151,17 @@ extension WPComConnectionSetupViewModel: WPComConnectionSetupHandlerDelegate {
139151
func stepDidUpdate(_ step: SetupStep, status: WPComConnectionSetupStep.Status) {
140152
updateStep(step, status: status)
141153

142-
if case .failure = status {
143-
setupState = .failed(step: step)
154+
if case .failure(let reason) = status {
155+
let checkPluginError: CheckPluginError? = step == .checkPlugin ? checkPluginError(from: reason) : nil
156+
setupState = .failed(step: step, checkPluginError: checkPluginError)
144157
}
145158
}
146159

160+
private func checkPluginError(from reason: String) -> CheckPluginError {
161+
// TODO: Update condition based on actual error identifier from handler
162+
reason.contains("outdated") ? .outdated : .other
163+
}
164+
147165
func setupDidComplete() {
148166
setupState = .completed
149167
}

WooCommerce/WooCommerceTests/ViewRelated/JetpackSetup/WPComLogin/WPComConnectionSetup/WPComConnectionSetupViewModelTests.swift

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class WPComConnectionSetupViewModelTests: XCTestCase {
6565
XCTAssertFalse(viewModel.isShowingSecondaryButton)
6666
}
6767

68-
func test_plugin_failure_shows_updatePlugin_and_tryAgain_buttons() {
68+
func test_plugin_failure_outdated_shows_updatePlugin_and_tryAgain_buttons() {
6969
// Given
7070
let viewModel = makeViewModel()
7171

@@ -79,6 +79,19 @@ final class WPComConnectionSetupViewModelTests: XCTestCase {
7979
XCTAssertEqual(viewModel.secondaryButtonTitle, "Try again")
8080
}
8181

82+
func test_plugin_failure_other_shows_tryAgain_button() {
83+
// Given
84+
let viewModel = makeViewModel()
85+
86+
// When
87+
mockHandler.simulateStepUpdate(.checkPlugin, status: .failure(reason: "Network error"))
88+
89+
// Then
90+
XCTAssertEqual(viewModel.primaryButtonTitle, "Try again")
91+
XCTAssertTrue(viewModel.isPrimaryButtonEnabled)
92+
XCTAssertFalse(viewModel.isShowingSecondaryButton)
93+
}
94+
8295
func test_setupDidComplete_enables_goToMyStore_button() {
8396
// Given
8497
let viewModel = makeViewModel()
@@ -106,7 +119,7 @@ final class WPComConnectionSetupViewModelTests: XCTestCase {
106119
XCTAssertTrue(goToStoreCalled)
107120
}
108121

109-
func test_primaryButtonTapped_on_plugin_failure_calls_updatePlugin() {
122+
func test_primaryButtonTapped_on_plugin_failure_outdated_calls_updatePlugin() {
110123
// Given
111124
let viewModel = makeViewModel()
112125
mockHandler.simulateStepUpdate(.checkPlugin, status: .failure(reason: "Plugin outdated"))
@@ -118,6 +131,18 @@ final class WPComConnectionSetupViewModelTests: XCTestCase {
118131
XCTAssertTrue(updatePluginCalled)
119132
}
120133

134+
func test_primaryButtonTapped_on_plugin_failure_other_retries() {
135+
// Given
136+
let viewModel = makeViewModel()
137+
mockHandler.simulateStepUpdate(.checkPlugin, status: .failure(reason: "Network error"))
138+
139+
// When
140+
viewModel.primaryButtonTapped()
141+
142+
// Then
143+
XCTAssertEqual(mockHandler.retryCallCount, 1)
144+
}
145+
121146
// MARK: - Dismiss Tests
122147

123148
func test_cancelTapped_calls_handler_cancel_and_dismiss() {

0 commit comments

Comments
 (0)