Skip to content

Commit 65f4379

Browse files
authored
Merge pull request #8388 from woocommerce/issue/8343-fix-time-range-description
2 parents b1644f7 + 3bbadc6 commit 65f4379

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Analytics Hub/Time Range/AnalyticsHubTimeRange.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ struct AnalyticsHubTimeRange {
99
return DateFormatter.Stats.createAnalyticsHubDayMonthYearFormatter(timezone: timezone).string(from: start)
1010
}
1111

12-
let startDateDescription = DateFormatter.Stats.createAnalyticsHubDayMonthFormatter(timezone: timezone).string(from: start)
12+
let startDateDescription: String
13+
if start.isSameYear(as: end) {
14+
startDateDescription = DateFormatter.Stats.createAnalyticsHubDayMonthFormatter(timezone: timezone).string(from: start)
15+
} else {
16+
startDateDescription = DateFormatter.Stats.createAnalyticsHubDayMonthYearFormatter(timezone: timezone).string(from: start)
17+
}
1318

1419
let endDateDescription: String
1520
if start.isSameMonth(as: end, using: calendar) {
1621
endDateDescription = DateFormatter.Stats.createAnalyticsHubDayYearFormatter(timezone: timezone).string(from: end)
1722
} else {
1823
endDateDescription = DateFormatter.Stats.createAnalyticsHubDayMonthYearFormatter(timezone: timezone).string(from: end)
1924
}
25+
2026
return "\(startDateDescription) - \(endDateDescription)"
2127
}
2228
}

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@
13681368
B5FD111221D3CE7700560344 /* NewNoteViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B5FD111121D3CE7700560344 /* NewNoteViewController.xib */; };
13691369
B5FD111621D3F13700560344 /* BordersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FD111521D3F13700560344 /* BordersView.swift */; };
13701370
B60B5026292D308A00178C26 /* AnalyticsTimeRangeCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = B60B5025292D308A00178C26 /* AnalyticsTimeRangeCard.swift */; };
1371+
B61C17722947981C002E9881 /* AnalyticsHubTimeRangeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B61C17712947981C002E9881 /* AnalyticsHubTimeRangeTests.swift */; };
13711372
B622BC74289CF19400B10CEC /* WaitingTimeTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B622BC73289CF19400B10CEC /* WaitingTimeTrackerTests.swift */; };
13721373
B626C71B287659D60083820C /* OrderCustomFieldsDetails.swift in Sources */ = {isa = PBXBuildFile; fileRef = B626C71A287659D60083820C /* OrderCustomFieldsDetails.swift */; };
13731374
B63AAF4B254AD2C6000B28A2 /* URL+SurveyViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B63AAF4A254AD2C6000B28A2 /* URL+SurveyViewControllerTests.swift */; };
@@ -3407,6 +3408,7 @@
34073408
B5FD111121D3CE7700560344 /* NewNoteViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = NewNoteViewController.xib; sourceTree = "<group>"; };
34083409
B5FD111521D3F13700560344 /* BordersView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BordersView.swift; sourceTree = "<group>"; };
34093410
B60B5025292D308A00178C26 /* AnalyticsTimeRangeCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsTimeRangeCard.swift; sourceTree = "<group>"; };
3411+
B61C17712947981C002E9881 /* AnalyticsHubTimeRangeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnalyticsHubTimeRangeTests.swift; sourceTree = "<group>"; };
34103412
B622BC73289CF19400B10CEC /* WaitingTimeTrackerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaitingTimeTrackerTests.swift; sourceTree = "<group>"; };
34113413
B626C71A287659D60083820C /* OrderCustomFieldsDetails.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderCustomFieldsDetails.swift; sourceTree = "<group>"; };
34123414
B63AAF4A254AD2C6000B28A2 /* URL+SurveyViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+SurveyViewControllerTests.swift"; sourceTree = "<group>"; };
@@ -7540,6 +7542,7 @@
75407542
children = (
75417543
AE4CCCEA29365CFD00B47EE8 /* AnalyticsHubViewModelTests.swift */,
75427544
B6440FB8292E74230012D506 /* AnalyticsHubTimeRangeSelectionTests.swift */,
7545+
B61C17712947981C002E9881 /* AnalyticsHubTimeRangeTests.swift */,
75437546
);
75447547
path = "Analytics Hub";
75457548
sourceTree = "<group>";
@@ -11497,6 +11500,7 @@
1149711500
022C658C2863BBAC00EC35A9 /* ProductFormViewController+ProductImageUploaderTests.swift in Sources */,
1149811501
093B265927DF15100026F92D /* BulkUpdatePriceViewControllerTests.swift in Sources */,
1149911502
2614EB1C24EB611200968D4B /* TopBannerViewTests.swift in Sources */,
11503+
B61C17722947981C002E9881 /* AnalyticsHubTimeRangeTests.swift in Sources */,
1150011504
B5DBF3C320E1484400B53AED /* StoresManagerTests.swift in Sources */,
1150111505
DE3404EA28B4C1D000CF0D97 /* NonAtomicSiteViewModelTests.swift in Sources */,
1150211506
02E493EF245C1087000AEA9E /* ProductFormBottomSheetListSelectorCommandTests.swift in Sources */,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import XCTest
2+
@testable import WooCommerce
3+
4+
final class AnalyticsHubTimeRangeTests: XCTestCase {
5+
private var testTimezone: TimeZone = {
6+
TimeZone(abbreviation: "UTC") ?? TimeZone.current
7+
}()
8+
9+
private var testCalendar: Calendar = {
10+
var calendar = Calendar(identifier: .iso8601)
11+
calendar.timeZone = TimeZone(abbreviation: "UTC") ?? TimeZone.current
12+
return calendar
13+
}()
14+
15+
func test_when_time_range_format_is_simplified_then_describes_only_first_date() {
16+
// Given
17+
let startDate = startDate(from: "2022-07-01")!
18+
let endDate = endDate(from: "2022-07-02")!
19+
let timeRange = AnalyticsHubTimeRange(start: startDate, end: endDate)
20+
21+
// When
22+
let description = timeRange.formatToString(simplified: true, timezone: testTimezone, calendar: testCalendar)
23+
24+
//Then
25+
XCTAssertEqual(description, "Jul 1, 2022")
26+
}
27+
28+
func test_when_time_range_format_is_in_different_months_then_describes_both_dates() {
29+
// Given
30+
let startDate = startDate(from: "2022-07-01")!
31+
let endDate = endDate(from: "2022-08-02")!
32+
let timeRange = AnalyticsHubTimeRange(start: startDate, end: endDate)
33+
34+
// When
35+
let description = timeRange.formatToString(simplified: false, timezone: testTimezone, calendar: testCalendar)
36+
37+
//Then
38+
XCTAssertEqual(description, "Jul 1 - Aug 2, 2022")
39+
}
40+
41+
func test_when_time_range_is_in_the_same_month_then_describe_month_only_in_the_start_date() {
42+
// Given
43+
let startDate = startDate(from: "2022-07-01")!
44+
let endDate = endDate(from: "2022-07-05")!
45+
let timeRange = AnalyticsHubTimeRange(start: startDate, end: endDate)
46+
47+
// When
48+
let description = timeRange.formatToString(simplified: false, timezone: testTimezone, calendar: testCalendar)
49+
50+
//Then
51+
XCTAssertEqual(description, "Jul 1 - 5, 2022")
52+
}
53+
54+
func test_when_time_range_is_in_different_years_then_fully_describe_both_dates() {
55+
// Given
56+
let startDate = startDate(from: "2021-04-15")!
57+
let endDate = endDate(from: "2022-04-15")!
58+
let timeRange = AnalyticsHubTimeRange(start: startDate, end: endDate)
59+
60+
// When
61+
let description = timeRange.formatToString(simplified: false, timezone: testTimezone, calendar: testCalendar)
62+
63+
//Then
64+
XCTAssertEqual(description, "Apr 15, 2021 - Apr 15, 2022")
65+
}
66+
67+
private func startDate(from date: String) -> Date? {
68+
let dateFormatter = DateFormatter()
69+
dateFormatter.dateFormat = "yyyy-MM-dd"
70+
dateFormatter.timeZone = testTimezone
71+
return dateFormatter.date(from: date)?.startOfDay(timezone: testTimezone)
72+
}
73+
74+
private func endDate(from date: String) -> Date? {
75+
let dateFormatter = DateFormatter()
76+
dateFormatter.dateFormat = "yyyy-MM-dd"
77+
dateFormatter.timeZone = testTimezone
78+
return dateFormatter.date(from: date)?.endOfDay(timezone: testTimezone)
79+
}
80+
}

0 commit comments

Comments
 (0)