From 498b9dd1c00228beb39206e1bfbe5e30becb1dfd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 18 Aug 2023 10:04:34 +0100 Subject: [PATCH] Fix tests --- src/DateUtils.ts | 8 ++++---- src/components/views/dialogs/ForwardDialog.tsx | 1 + src/languageHandler.tsx | 2 +- .../structures/__snapshots__/MatrixChat-test.tsx.snap | 2 +- test/components/views/rooms/SearchResultTile-test.tsx | 2 +- .../views/settings/devices/DeviceDetails-test.tsx | 1 + test/test-utils/beacon.ts | 1 + test/test-utils/location.ts | 2 ++ 8 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/DateUtils.ts b/src/DateUtils.ts index 28743d2a3918..5f0b6babc82e 100644 --- a/src/DateUtils.ts +++ b/src/DateUtils.ts @@ -25,7 +25,7 @@ const MILLIS_IN_DAY = 86400000; export function getDaysArray(weekday: Intl.DateTimeFormatOptions["weekday"] = "short"): string[] { const now = new Date(); const { format } = new Intl.DateTimeFormat(getUserLanguage(), { weekday }); - return [...Array(7).keys()].map((day) => format(new Date().getTime() - (now.getDay() - day) * MILLIS_IN_DAY)); + return [...Array(7).keys()].map((day) => format(now.getTime() - (now.getDay() - day) * MILLIS_IN_DAY)); } function getMonthsArray(month: Intl.DateTimeFormatOptions["month"] = "short"): string[] { @@ -44,7 +44,7 @@ export function formatDate(date: Date, showTwelveHour = false, locale?: string): hour: "numeric", minute: "2-digit", hour12: showTwelveHour, - }).format(new Date()); + }).format(date); } else if (now.getFullYear() === date.getFullYear()) { return new Intl.DateTimeFormat(_locale, { weekday: "short", @@ -53,7 +53,7 @@ export function formatDate(date: Date, showTwelveHour = false, locale?: string): hour: "numeric", minute: "2-digit", hour12: showTwelveHour, - }).format(new Date()); + }).format(date); } return formatFullDate(date, showTwelveHour, true, _locale); } @@ -214,7 +214,7 @@ export function formatFullDateNoDayNoTime(date: Date, locale?: string): string { } export function formatRelativeTime(date: Date, showTwelveHour = false): string { - const now = new Date(Date.now()); + const now = new Date(); if (withinCurrentDay(date, now)) { return formatTime(date, showTwelveHour); } else { diff --git a/src/components/views/dialogs/ForwardDialog.tsx b/src/components/views/dialogs/ForwardDialog.tsx index 13d84b99e7e0..a9b351be309a 100644 --- a/src/components/views/dialogs/ForwardDialog.tsx +++ b/src/components/views/dialogs/ForwardDialog.tsx @@ -216,6 +216,7 @@ const ForwardDialog: React.FC = ({ matrixClient: cli, event, permalinkCr }, event_id: "$9999999999999999999999999999999999999999999", room_id: event.getRoomId(), + origin_server_ts: event.getTs(), }); mockEvent.sender = { name: profileInfo.displayname || userId, diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index c8a8c1d4b99f..3f0a6ca4cdbe 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -93,7 +93,7 @@ export class UserFriendlyError extends Error { export function getUserLanguage(): string { const language = SettingsStore.getValue("language", null, /*excludeDefault:*/ true); - if (language) { + if (typeof language === "string" && !!language) { return language; } else { return normalizeLanguageKey(getLanguageFromBrowser()); diff --git a/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap b/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap index 5bd7b22f4e4f..9b8a77dfe887 100644 --- a/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap +++ b/test/components/structures/__snapshots__/MatrixChat-test.tsx.snap @@ -37,7 +37,7 @@ exports[` with a soft-logged-out session should show the soft-logo Matrix
{ room_id: ROOM_ID, content: { body: `Message #${i}` }, event_id: `$${i}:server`, - origin_server_ts: undefined, + origin_server_ts: i, }), ), }); diff --git a/test/components/views/settings/devices/DeviceDetails-test.tsx b/test/components/views/settings/devices/DeviceDetails-test.tsx index a3c359cdf50e..03068b0b3d1d 100644 --- a/test/components/views/settings/devices/DeviceDetails-test.tsx +++ b/test/components/views/settings/devices/DeviceDetails-test.tsx @@ -45,6 +45,7 @@ describe("", () => { beforeEach(() => { jest.setSystemTime(now); + jest.spyOn(Date, "now").mockImplementation(() => now); }); it("renders device without metadata", () => { diff --git a/test/test-utils/beacon.ts b/test/test-utils/beacon.ts index 0cea7820bc04..e519e791d401 100644 --- a/test/test-utils/beacon.ts +++ b/test/test-utils/beacon.ts @@ -103,6 +103,7 @@ export const makeBeaconEvent = ( room_id: roomId, sender, content: ContentHelpers.makeBeaconContent(geoUri, timestamp, beaconInfoId, description), + origin_server_ts: 0, }); }; diff --git a/test/test-utils/location.ts b/test/test-utils/location.ts index 548e13136040..4d22d98364c6 100644 --- a/test/test-utils/location.ts +++ b/test/test-utils/location.ts @@ -27,6 +27,7 @@ export const makeLegacyLocationEvent = (geoUri: string): MatrixEvent => { msgtype: "m.location", geo_uri: geoUri, }, + origin_server_ts: 0, }); }; @@ -41,6 +42,7 @@ export const makeLocationEvent = (geoUri: string, assetType?: LocationAssetType) "Human-readable label", assetType, ), + origin_server_ts: 0, }); };