Skip to content

Commit bcf7f3c

Browse files
committed
tests: display user's local time
1 parent 49fa871 commit bcf7f3c

File tree

2 files changed

+93
-11
lines changed

2 files changed

+93
-11
lines changed

test/example_data.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ User user({
216216
bool? isActive,
217217
bool? isBot,
218218
UserRole? role,
219+
String? timezone,
219220
String? avatarUrl,
220221
Map<int, ProfileFieldUserData>? profileData,
221222
}) {
@@ -233,7 +234,7 @@ User user({
233234
botType: null,
234235
botOwnerId: null,
235236
role: role ?? UserRole.member,
236-
timezone: 'UTC',
237+
timezone: timezone ?? 'UTC',
237238
avatarUrl: avatarUrl,
238239
avatarVersion: 0,
239240
profileData: profileData,

test/widgets/profile_test.dart

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ void resetTimezones() {
7676
tz.initializeDatabase([]);
7777
}
7878

79+
Subject<String> checkUserLocalTimeText(WidgetTester tester) =>
80+
check(
81+
tester.widget<Text>(find.descendant(of: find.byType(UserLocalTimeText), matching: find.byType(Text))).data).isNotNull();
82+
7983
void main() {
8084
TestZulipBinding.ensureInitialized();
8185

@@ -328,18 +332,95 @@ void main() {
328332
check(find.textContaining(longString).evaluate()).length.equals(7);
329333
});
330334

331-
test('assets; ensure the timezone database used to display users\' local time is up-to-date', () async {
332-
tz.initializeTimeZones();
333-
final latestTimezones = tz.tzdbSerialize(tz.timeZoneDatabase);
335+
group('UserLocalTimeText', () {
336+
setUp(() async {
337+
await UserLocalTimeText.initializeTimezonesUsingAssets();
338+
});
339+
340+
test('assets; ensure the timezone database used to display users\' local time is up-to-date', () async {
341+
tz.initializeTimeZones();
342+
final latestTimezones = tz.tzdbSerialize(tz.timeZoneDatabase);
334343

335-
await UserLocalTimeText.initializeTimezonesUsingAssets();
336-
final currentTimezones = tz.tzdbSerialize(tz.timeZoneDatabase);
344+
await UserLocalTimeText.initializeTimezonesUsingAssets();
345+
final currentTimezones = tz.tzdbSerialize(tz.timeZoneDatabase);
346+
347+
check(
348+
listEquals(currentTimezones, latestTimezones),
349+
because:
350+
'the timezone database used to display users\' local time is not up-to-date, please copy `package:timezone/data/latest_all.tzf` to `assets/timezone/latest_all.tzf`',
351+
).isTrue();
352+
});
337353

338-
check(
339-
listEquals(currentTimezones, latestTimezones),
340-
because:
341-
'the timezone database used to display users\' local time is not up-to-date, please copy `package:timezone/data/latest_all.tzf` to `assets/timezone/latest_all.tzf`',
342-
).isTrue();
354+
final testCases = [(
355+
description: 'simple usecase',
356+
currentTimezone: 'America/Los_Angeles',
357+
currentYear: 2025, currentMonth: 02, currentDay: 01, currentHour: 12, currentMinute: 00,
358+
userTimezone: 'America/New_York',
359+
equalsTo: '3:00 PM local time'
360+
), (
361+
description: 'abbreviation usecase',
362+
currentTimezone: 'Europe/Brussels',
363+
currentYear: 2025, currentMonth: 02, currentDay: 01, currentHour: 12, currentMinute: 00,
364+
userTimezone: 'CET',
365+
equalsTo: '12:00 PM local time'
366+
), (
367+
description: 'DST usecase',
368+
currentTimezone: 'Europe/London',
369+
currentYear: 2025, currentMonth: 08, currentDay: 01, currentHour: 12, currentMinute: 00,
370+
userTimezone: 'UTC',
371+
equalsTo: '11:00 AM local time'
372+
)
373+
];
374+
375+
for (
376+
final (
377+
:description,
378+
:currentTimezone,
379+
:currentYear,
380+
:currentMonth,
381+
:currentDay,
382+
:currentHour,
383+
:currentMinute, :userTimezone, :equalsTo) in testCases) {
384+
testWidgets('page builds; $description', (tester) async {
385+
final currentTime = tz.TZDateTime(
386+
tz.getLocation(currentTimezone),
387+
currentYear,
388+
currentMonth,
389+
currentDay,
390+
currentHour,
391+
currentMinute
392+
);
393+
resetTimezones();
394+
395+
await withClock(Clock.fixed(currentTime), () async {
396+
final user = eg.user(userId: 1, timezone: userTimezone);
397+
await setupPage(tester, pageUserId: user.userId, users: [user]);
398+
399+
checkUserLocalTimeText(tester).equals(equalsTo);
400+
});
401+
});
402+
}
403+
404+
testWidgets('page builds; keep "current" local time current', (tester) async {
405+
withClock(Clock.fixed(tz.TZDateTime(tz.getLocation('Europe/London'), 2025, 02, 01, 12, 00)), () {
406+
FakeAsync().run((async) {
407+
final user = eg.user(userId: 1, timezone: 'Europe/London');
408+
setupPage(tester, pageUserId: user.userId, users: [user]);
409+
async.flushMicrotasks();
410+
checkUserLocalTimeText(tester).equals('12:00 PM local time');
411+
412+
async.elapse(Duration(minutes: 1));
413+
tester.pumpAndSettle();
414+
async.flushMicrotasks();
415+
checkUserLocalTimeText(tester).equals('12:01 PM local time');
416+
417+
async.elapse(Duration(minutes: 1));
418+
tester.pumpAndSettle();
419+
async.flushMicrotasks();
420+
checkUserLocalTimeText(tester).equals('12:02 PM local time');
421+
});
422+
});
423+
});
343424
});
344425
});
345426
}

0 commit comments

Comments
 (0)