Skip to content

Commit f4e316b

Browse files
committed
Fix delta calculation when previous and current values are zero
1 parent 669059a commit f4e316b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Factories/StatsDataTextFormatter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ extension StatsDataTextFormatter {
123123
/// Creates the `DeltaPercentage` for the percent change from the previous `Decimal` value to the current `Decimal` value
124124
///
125125
static func createDeltaPercentage(from previousValue: Decimal?, to currentValue: Decimal?) -> DeltaPercentage {
126-
guard let previousValue, let currentValue else {
127-
return DeltaPercentage(value: 0) // Missing data: 0% change
126+
guard let previousValue, let currentValue, previousValue != currentValue else {
127+
return DeltaPercentage(value: 0) // Missing or equal values: 0% change
128128
}
129129

130130
guard previousValue > 0 else {

WooCommerce/WooCommerceTests/ViewRelated/Dashboard/StatsDataTextFormatterTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,19 @@ final class StatsDataTextFormatterTests: XCTestCase {
294294
XCTAssertEqual(delta.direction, .zero)
295295
}
296296

297+
func test_createDeltaPercentage_returns_expected_zero_delta_for_zero_values() {
298+
// Given
299+
let previousValue: Double = 0
300+
let currentValue: Double = 0
301+
302+
// When
303+
let delta = StatsDataTextFormatter.createDeltaPercentage(from: previousValue, to: currentValue)
304+
305+
// Then
306+
XCTAssertEqual(delta.string, "+0%")
307+
XCTAssertEqual(delta.direction, .zero)
308+
}
309+
297310
func test_createDeltaPercentage_returns_100_percent_change_when_previous_value_is_zero() {
298311
// Given
299312
let previousValue: Double = 0

0 commit comments

Comments
 (0)