Skip to content

Commit

Permalink
add local time tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mltbnz committed Sep 28, 2024
1 parent 519362f commit 3676186
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 90 deletions.
1 change: 1 addition & 0 deletions CriticalMapsKit/Sources/Helpers/Timezone+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public extension TimeZone {
static let spain = TimeZone(identifier: "Europe/Madrid")!
static let france = TimeZone(identifier: "Europe/Paris")!
static let greece = TimeZone(identifier: "Europe/Athens")!
static let london = TimeZone(identifier: "Europe/London")!

// America
static let ecuador = TimeZone(identifier: "America/Guayaquil")!
Expand Down
46 changes: 46 additions & 0 deletions CriticalMapsKit/Sources/SharedModels/NextRideFeature/Ride.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@ extension Ride {
let id: Int
let name: String
let timezone: String

public init(
id: Int,
name: String,
timezone: String
) {
self.id = id
self.name = name
self.timezone = timezone
}
}

}

public extension Ride {
Expand Down Expand Up @@ -159,3 +170,38 @@ private extension Date.FormatStyle {
)
}
}

enum Helper {
static func displayEventDateTimeForUser(
eventDateTime: TimeInterval,
eventTimeZoneID: String,
userTimeZoneID: String
) -> String? {

// Create Date object from event's Unix timestamp
let eventDate = Date(timeIntervalSince1970: eventDateTime)

// Get event's timezone
guard let eventTimeZone = TimeZone(identifier: eventTimeZoneID) else {

Check warning on line 185 in CriticalMapsKit/Sources/SharedModels/NextRideFeature/Ride.swift

View workflow job for this annotation

GitHub Actions / Unit Tests (iOS)

value 'eventTimeZone' was defined but never used; consider replacing with boolean test
print("Invalid event timezone")
return nil
}

// Get user's timezone (e.g., London timezone)
guard let userTimeZone = TimeZone(identifier: userTimeZoneID) else {
print("Invalid user timezone")
return nil
}

// Formatter to display time in local timezone
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .short

// Convert event time to user's local timezone
dateFormatter.timeZone = userTimeZone
let formattedDateForUser = dateFormatter.string(from: eventDate)

return formattedDateForUser
}
}
90 changes: 0 additions & 90 deletions CriticalMapsKit/Tests/NextRideFeatureTests/RideTests.swift

This file was deleted.

63 changes: 63 additions & 0 deletions CriticalMapsKit/Tests/NextRideFeatureTests/RideTimeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import ComposableArchitecture
import Helpers
import SharedModels
import Foundation
import Testing

struct RideTimeTests {
@Test("Ride in new york timezone")
func rideWithNewYorkTimezone() {
withDependencies {
$0.locale = Locale(identifier: "en_US")
} operation: {
let ride = Ride.mock(timeZone: .newYork, timestamp: 1727478000)
let rideTime = ride.rideTime
#expect(rideTime == "7:00 PM")
}
}

@Test("Ride in berlin timezone")
func rideWithBerlinTimezone() {
withDependencies {
$0.locale = Locale(identifier: "de_DE")
} operation: {
let ride = Ride.mock(timeZone: .germany, timestamp: 1725192000)
let rideTime = ride.rideTime
#expect(rideTime == "14:00")
}
}

@Test("Ride in GMT timezone")
func rideWithGMTTimezone() {
withDependencies {
$0.locale = Locale(identifier: "pt_PT")
} operation: {
let ride = Ride.mock(timeZone: .gmt, timestamp: 1727452800)
let rideTime = ride.rideTime
#expect(rideTime == "16:00")
}
}
}

private extension Ride {
static func mock(timeZone: TimeZone, timestamp: TimeInterval) -> Self {
Self(
id: 0,
city: Ride.City(id: 1, name: "Berlin", timezone: timeZone.identifier),
slug: nil,
title: "CriticalMaps Berlin",
description: nil,
dateTime: Date(timeIntervalSince1970: timestamp),
location: nil,
latitude: 53.1235,
longitude: 13.4234,
estimatedParticipants: nil,
estimatedDistance: nil,
estimatedDuration: nil,
enabled: true,
disabledReason: nil,
disabledReasonMessage: nil,
rideType: .criticalMass
)
}
}

0 comments on commit 3676186

Please sign in to comment.