11import XCTest
22@testable import WooCommerce
33
4- class StoreStatsV4ChartAxisHelperTests : XCTestCase {
4+ final class StoreStatsV4ChartAxisHelperTests : XCTestCase {
55 private let helper = StoreStatsV4ChartAxisHelper ( )
66
7- func testDatesAcrossTwoMonths( ) {
7+ // MARK: - Today
8+
9+ func test_generateLabelText_for_today_contains_hour_text( ) throws {
10+ // Given
11+ // GMT: Wednesday, January 5, 2022 12:50:05 AM
12+ let dateAt12AM = Date ( timeIntervalSince1970: 1641343805 )
13+ // GMT: Wednesday, January 5, 2022 5:50:05 PM
14+ let dateAt5PM = Date ( timeIntervalSince1970: 1641405005 )
15+ let dates = [ dateAt12AM, dateAt5PM]
16+ let timezone = try XCTUnwrap ( TimeZone ( identifier: " GMT " ) )
17+
18+ // When
19+ let text = helper. generateLabelText ( for: dates, timeRange: . today, siteTimezone: timezone)
20+
21+ // Then
22+ // "12 AM" in en-US locale.
23+ XCTAssertEqual ( text [ 0 ] , todayXAxisFormatter ( timezone: timezone) . string ( from: dateAt12AM) )
24+ // "5 PM" in en-US locale.
25+ XCTAssertEqual ( text [ 1 ] , todayXAxisFormatter ( timezone: timezone) . string ( from: dateAt5PM) )
26+ }
27+
28+ // MARK: - This Week
29+
30+ func test_generateLabelText_for_thisWeek_with_dates_across_two_months_contains_month_text_for_first_different_month( ) {
31+ // Given
832 // GMT: Saturday, June 1, 2019 12:29:29 AM
933 let dateInJune = Date ( timeIntervalSince1970: 1559348969 )
1034 // GMT: Monday, June 10, 2019 12:29:29 AM
@@ -13,7 +37,11 @@ class StoreStatsV4ChartAxisHelperTests: XCTestCase {
1337 let dateInAugust = Date ( timeIntervalSince1970: 1564619369 )
1438 let dates = [ dateInAugust, dateInJune, secondDateInJune]
1539 let timezone = TimeZone ( identifier: " GMT " ) !
40+
41+ // When
1642 let text = helper. generateLabelText ( for: dates, timeRange: . thisWeek, siteTimezone: timezone)
43+
44+ // Then
1745 let textOfDateInAugust = text [ 0 ]
1846 let textOfFirstDateInJune = text [ 1 ]
1947 let textOfSecondDateInJune = text [ 2 ]
@@ -22,17 +50,94 @@ class StoreStatsV4ChartAxisHelperTests: XCTestCase {
2250 XCTAssertEqual ( textOfSecondDateInJune, dayOfMonthFormatter ( timezone: timezone) . string ( from: secondDateInJune) )
2351 }
2452
25- private func dayOfMonthFormatter( timezone: TimeZone ) -> DateFormatter {
53+ func test_generateLabelText_for_thisWeek_with_dates_in_the_same_month_only_contains_month_text_for_the_first_date( ) throws {
54+ // Given
55+ // GMT: Wednesday, January 5, 2022 12:50:05 AM
56+ let dateOnJan5 = Date ( timeIntervalSince1970: 1641343805 )
57+ // GMT: Wednesday, January 12, 2022 5:50:05 PM
58+ let dateOnJan12 = Date ( timeIntervalSince1970: 1642009805 )
59+ let dates = [ dateOnJan12, dateOnJan5]
60+ let timezone = try XCTUnwrap ( TimeZone ( identifier: " GMT " ) )
61+
62+ // When
63+ let text = helper. generateLabelText ( for: dates, timeRange: . thisWeek, siteTimezone: timezone)
64+
65+ // Then
66+ // "Jan 12" in en-US locale.
67+ XCTAssertEqual ( text [ 0 ] , dayMonthFormatter ( timezone: timezone) . string ( from: dateOnJan12) )
68+ // "5" in en-US locale (no month text).
69+ XCTAssertEqual ( text [ 1 ] , dayOfMonthFormatter ( timezone: timezone) . string ( from: dateOnJan5) )
70+ }
71+
72+ // MARK: - This Month
73+
74+ func test_generateLabelText_for_thisMonth_only_contains_month_text_for_the_first_date( ) throws {
75+ // Given
76+ // GMT: Wednesday, January 5, 2022 12:50:05 AM
77+ let dateOnJan5 = Date ( timeIntervalSince1970: 1641343805 )
78+ // GMT: Wednesday, January 12, 2022 5:50:05 PM
79+ let dateOnJan12 = Date ( timeIntervalSince1970: 1642009805 )
80+ let dates = [ dateOnJan12, dateOnJan5]
81+ let timezone = try XCTUnwrap ( TimeZone ( identifier: " GMT " ) )
82+
83+ // When
84+ let text = helper. generateLabelText ( for: dates, timeRange: . thisMonth, siteTimezone: timezone)
85+
86+ // Then
87+ // "Jan 12" in en-US locale.
88+ XCTAssertEqual ( text [ 0 ] , dayMonthFormatter ( timezone: timezone) . string ( from: dateOnJan12) )
89+ // "5" in en-US locale (no month text).
90+ XCTAssertEqual ( text [ 1 ] , dayOfMonthFormatter ( timezone: timezone) . string ( from: dateOnJan5) )
91+ }
92+
93+ // MARK: - This Year
94+
95+ func test_generateLabelText_for_thisYear_has_month_text_for_all_dates( ) throws {
96+ // Given
97+ // GMT: Friday, March 11, 2022 5:50:05 PM
98+ let dateInMarch = Date ( timeIntervalSince1970: 1647021005 )
99+ // GMT: Wednesday, January 12, 2022 5:50:05 PM
100+ let dateInJan = Date ( timeIntervalSince1970: 1642009805 )
101+ let dates = [ dateInMarch, dateInJan]
102+ let timezone = try XCTUnwrap ( TimeZone ( identifier: " GMT " ) )
103+
104+ // When
105+ let text = helper. generateLabelText ( for: dates, timeRange: . thisYear, siteTimezone: timezone)
106+
107+ // Then
108+ // "Mar" in en-US locale.
109+ XCTAssertEqual ( text [ 0 ] , monthFormatter ( timezone: timezone) . string ( from: dateInMarch) )
110+ // "Jan" in en-US locale (no month text).
111+ XCTAssertEqual ( text [ 1 ] , monthFormatter ( timezone: timezone) . string ( from: dateInJan) )
112+ }
113+ }
114+
115+ private extension StoreStatsV4ChartAxisHelperTests {
116+ func dayOfMonthFormatter( timezone: TimeZone ) -> DateFormatter {
26117 let formatter = DateFormatter ( )
27118 formatter. setLocalizedDateFormatFromTemplate ( " d " )
28119 formatter. timeZone = timezone
29120 return formatter
30121 }
31122
32- private func dayMonthFormatter( timezone: TimeZone ) -> DateFormatter {
123+ func dayMonthFormatter( timezone: TimeZone ) -> DateFormatter {
33124 let formatter = DateFormatter ( )
34125 formatter. setLocalizedDateFormatFromTemplate ( " MMM d " )
35126 formatter. timeZone = timezone
36127 return formatter
37128 }
129+
130+ func monthFormatter( timezone: TimeZone ) -> DateFormatter {
131+ let formatter = DateFormatter ( )
132+ formatter. setLocalizedDateFormatFromTemplate ( " MMM " )
133+ formatter. timeZone = timezone
134+ return formatter
135+ }
136+
137+ func todayXAxisFormatter( timezone: TimeZone ) -> DateFormatter {
138+ let formatter = DateFormatter ( )
139+ formatter. setLocalizedDateFormatFromTemplate ( " ha " )
140+ formatter. timeZone = timezone
141+ return formatter
142+ }
38143}
0 commit comments