Test A SwiftUI Feature With A Fixed Locale And Calendar
A date label looks right in your preview and wrong on a teammate’s machine. Both previews use the same view, but one inherits a different calendar or time zone. Changing only the language does not make the fixture reproducible.
A useful date fixture controls four inputs: the instant, the locale, the calendar, and the time zone. The first is data. The other three describe how the interface interprets and presents it.
Use a context that reaches both layers
SwiftUI exposes locale, calendar, and timeZone environment values. Foundation formatting outside the view also needs an explicit context. Setting an environment value does not rewrite Locale.current throughout your model layer.
import Foundation
import SwiftUI
struct DateContext {
let locale: Locale
let calendar: Calendar
let timeZone: TimeZone
init(locale: String, calendar identifier: Calendar.Identifier, zone: String) {
self.locale = Locale(identifier: locale)
self.timeZone = TimeZone(identifier: zone)!
var calendar = Calendar(identifier: identifier)
calendar.locale = self.locale
calendar.timeZone = self.timeZone
self.calendar = calendar
}
var style: Date.FormatStyle {
Date.FormatStyle(
date: .abbreviated, time: .shortened,
locale: locale, calendar: calendar, timeZone: timeZone)
}
func label(for date: Date) -> String {
date.formatted(style)
}
}
struct AppointmentFixture: View {
let context: DateContext
let date: Date
var body: some View {
VStack {
Text(date, format: context.style)
Text(verbatim: context.label(for: date))
}
.environment(\.locale, context.locale)
.environment(\.calendar, context.calendar)
.environment(\.timeZone, context.timeZone)
}
}
Both labels receive the same explicitly configured format style. The first lets Text perform formatting; the second receives a string already formatted by Foundation. The environment values also configure other descendants that consult them. This avoids assuming that a format style constructed elsewhere will inherit every calendar and time-zone setting from the view.
The force unwrap is appropriate only for these known fixture identifiers. A user-imported time zone string needs a validation path and a deliberate fallback policy.
Freeze the instant as well
Use a fixed date when creating the fixture:
let instant = Date(timeIntervalSince1970: 1_788_307_200)
let context = DateContext(
locale: "en_US",
calendar: .gregorian,
zone: "America/Los_Angeles"
)
let fixture = AppointmentFixture(context: context, date: instant)
This instant falls on September 2 in UTC and September 1 in Los Angeles. That boundary is useful: a test that only uses lunchtime in its local zone can miss an accidental switch to UTC.
For a feature that calculates “today,” inject a current-date provider or pass a reference date into its pure calculation. Freezing presentation while the model still calls Date.now leaves a moving dependency in the test.
Do not change the device clock as the first solution. A fixed input gives the test a much smaller surface area.
Choose cases that reveal different assumptions
Start with a few purposeful contexts. English with a Gregorian calendar in UTC is a baseline. Keep the locale and calendar but switch to Los Angeles to expose day boundaries. Then use Thai with the Buddhist calendar in Bangkok to expose a hardcoded Gregorian year.
The companion checks that the Buddhist calendar’s year for the fixed instant is 543 greater than the Gregorian year. That is a semantic assertion. It also prints formatted labels for inspection, but does not treat punctuation, spacing, or localized abbreviations as eternal constants across operating-system releases.
If exact typography matters to a screenshot test, pin the simulator runtime and record that dependency. An updated locale database can legitimately change a formatted string.
Keep translations and formatting separate
A French formatting locale does not prove that every app string has a French translation. String Catalog resources, plural forms, and fallback language are another layer; our plural localization example covers that boundary.
Likewise, a fixed calendar does not exercise right-to-left layout or large Dynamic Type. Add those as deliberate view cases rather than assuming one “international” screenshot covers all of them.
For a booking or reporting feature, define the business calendar too. The user’s display zone and the server’s reporting zone may be intentionally different. Give those concepts different names instead of allowing a convenient environment value to decide business rules accidentally.
September 2026 review: the complete view type-checked for an iOS 17 deployment target. The Foundation fixture produced the expected UTC/Los Angeles day difference and Buddhist year difference on the host. SwiftUI rendering and screenshot baselines still need verification on the app’s chosen simulator runtimes.