refactor: Remove current_date arg from relative_time_string_from_date.

The current_date argument was only used in tests and now we're using
MockDate to set the current date.
This commit is contained in:
Shubham Padia
2024-01-15 13:50:32 +07:00
committed by Tim Abbott
parent 3a233d73c1
commit d5de26ff3b
4 changed files with 18 additions and 25 deletions

View File

@@ -480,9 +480,7 @@ function format_conversation(conversation_data) {
context.full_last_msg_date_time = timerender.get_full_datetime_clarification(time); context.full_last_msg_date_time = timerender.get_full_datetime_clarification(time);
context.conversation_key = recent_view_util.get_key_from_message(last_msg); context.conversation_key = recent_view_util.get_key_from_message(last_msg);
context.unread_count = message_to_conversation_unread_count(last_msg); context.unread_count = message_to_conversation_unread_count(last_msg);
context.last_msg_time = timerender.relative_time_string_from_date({ context.last_msg_time = timerender.relative_time_string_from_date(time);
date: time,
});
context.is_private = last_msg.type === "private"; context.is_private = last_msg.type === "private";
let all_senders; let all_senders;
let senders; let senders;

View File

@@ -59,15 +59,15 @@ export function populate_exports_table(exports: RealmExport[]): void {
let deleted_timestamp = null; let deleted_timestamp = null;
if (data.failed_timestamp !== null) { if (data.failed_timestamp !== null) {
failed_timestamp = timerender.relative_time_string_from_date({ failed_timestamp = timerender.relative_time_string_from_date(
date: new Date(data.failed_timestamp * 1000), new Date(data.failed_timestamp * 1000),
}); );
} }
if (data.deleted_timestamp !== null) { if (data.deleted_timestamp !== null) {
deleted_timestamp = timerender.relative_time_string_from_date({ deleted_timestamp = timerender.relative_time_string_from_date(
date: new Date(data.deleted_timestamp * 1000), new Date(data.deleted_timestamp * 1000),
}); );
} }
return render_admin_export_list({ return render_admin_export_list({
@@ -75,9 +75,9 @@ export function populate_exports_table(exports: RealmExport[]): void {
id: data.id, id: data.id,
acting_user: people.get_full_name(data.acting_user_id), acting_user: people.get_full_name(data.acting_user_id),
// Convert seconds -> milliseconds // Convert seconds -> milliseconds
event_time: timerender.relative_time_string_from_date({ event_time: timerender.relative_time_string_from_date(
date: new Date(data.export_time * 1000), new Date(data.export_time * 1000),
}), ),
url: data.export_url, url: data.export_url,
time_failed: failed_timestamp, time_failed: failed_timestamp,
pending: data.pending, pending: data.pending,

View File

@@ -213,15 +213,8 @@ export function render_now(time: Date, today = new Date()): TimeRender {
} }
// Relative time rendering for use in most screens like Recent conversations. // Relative time rendering for use in most screens like Recent conversations.
// export function relative_time_string_from_date(date: Date): string {
// Current date is passed as an argument for unit testing const current_date = new Date();
export function relative_time_string_from_date({
date,
current_date = new Date(),
}: {
date: Date;
current_date?: Date;
}): string {
const minutes = differenceInMinutes(current_date, date); const minutes = differenceInMinutes(current_date, date);
if (minutes <= 2) { if (minutes <= 2) {
return $t({defaultMessage: "Just now"}); return $t({defaultMessage: "Just now"});

View File

@@ -531,13 +531,11 @@ run_test("last_seen_status_from_date", () => {
run_test("relative_time_string_from_date", () => { run_test("relative_time_string_from_date", () => {
// Set base_date to March 1 2016 12.30 AM (months are zero based) // Set base_date to March 1 2016 12.30 AM (months are zero based)
let base_date = new Date(2016, 2, 1, 0, 30); let base_date = new Date(2016, 2, 1, 0, 30);
MockDate.set(base_date.getTime());
function assert_same(duration, expected_status) { function assert_same(duration, expected_status) {
const past_date = add(base_date, duration); const past_date = add(base_date, duration);
const actual_status = timerender.relative_time_string_from_date({ const actual_status = timerender.relative_time_string_from_date(past_date);
date: past_date,
current_date: base_date,
});
assert.equal(actual_status, expected_status); assert.equal(actual_status, expected_status);
} }
@@ -571,11 +569,13 @@ run_test("relative_time_string_from_date", () => {
// Set base_date to May 1 2016 12.30 AM (months are zero based) // Set base_date to May 1 2016 12.30 AM (months are zero based)
base_date = new Date(2016, 4, 1, 0, 30); base_date = new Date(2016, 4, 1, 0, 30);
MockDate.set(base_date.getTime());
assert_same({days: -91}, "Jan 31"); assert_same({days: -91}, "Jan 31");
// Set base_date to May 1 2016 10.30 PM (months are zero based) // Set base_date to May 1 2016 10.30 PM (months are zero based)
base_date = new Date(2016, 4, 2, 23, 30); base_date = new Date(2016, 4, 2, 23, 30);
MockDate.set(base_date.getTime());
assert_same({hours: -1}, $t({defaultMessage: "An hour ago"})); assert_same({hours: -1}, $t({defaultMessage: "An hour ago"}));
@@ -584,6 +584,8 @@ run_test("relative_time_string_from_date", () => {
assert_same({hours: -12}, $t({defaultMessage: "12 hours ago"})); assert_same({hours: -12}, $t({defaultMessage: "12 hours ago"}));
assert_same({hours: -24}, $t({defaultMessage: "Yesterday"})); assert_same({hours: -24}, $t({defaultMessage: "Yesterday"}));
MockDate.reset();
}); });
run_test("set_full_datetime", () => { run_test("set_full_datetime", () => {