|
| 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