2021-12-21 12:08:22 +03:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2022-03-22 13:32:35 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2022-10-10 18:29:10 +03:00
|
|
|
import { render } from "@testing-library/react";
|
2021-12-21 12:08:22 +03:00
|
|
|
|
|
|
|
import { formatFullDateNoTime } from "../../../../src/DateUtils";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { UIFeature } from "../../../../src/settings/UIFeature";
|
2022-03-22 13:32:35 +03:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { getMockClientWithEventEmitter } from "../../../test-utils";
|
2022-03-03 00:09:24 +03:00
|
|
|
import DateSeparator from "../../../../src/components/views/messages/DateSeparator";
|
2021-12-21 12:08:22 +03:00
|
|
|
|
|
|
|
jest.mock("../../../../src/settings/SettingsStore");
|
|
|
|
|
|
|
|
describe("DateSeparator", () => {
|
|
|
|
const HOUR_MS = 3600000;
|
|
|
|
const DAY_MS = HOUR_MS * 24;
|
|
|
|
// Friday Dec 17 2021, 9:09am
|
2022-12-12 14:24:14 +03:00
|
|
|
const now = "2021-12-17T08:09:00.000Z";
|
2021-12-21 12:08:22 +03:00
|
|
|
const nowMs = 1639728540000;
|
|
|
|
const defaultProps = {
|
|
|
|
ts: nowMs,
|
|
|
|
now,
|
2022-03-25 05:11:56 +03:00
|
|
|
roomId: "!unused:example.org",
|
2021-12-21 12:08:22 +03:00
|
|
|
};
|
|
|
|
const RealDate = global.Date;
|
|
|
|
class MockDate extends Date {
|
2023-02-13 14:39:16 +03:00
|
|
|
constructor(date: string | number | Date) {
|
2021-12-21 12:08:22 +03:00
|
|
|
super(date || now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:32:35 +03:00
|
|
|
const mockClient = getMockClientWithEventEmitter({});
|
2021-12-21 12:08:22 +03:00
|
|
|
const getComponent = (props = {}) =>
|
2022-12-12 14:24:14 +03:00
|
|
|
render(
|
2022-10-10 18:29:10 +03:00
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<DateSeparator {...defaultProps} {...props} />
|
2022-12-12 14:24:14 +03:00
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2021-12-21 12:08:22 +03:00
|
|
|
|
|
|
|
type TestCase = [string, number, string];
|
|
|
|
const testCases: TestCase[] = [
|
2022-12-12 14:24:14 +03:00
|
|
|
["the exact same moment", nowMs, "Today"],
|
|
|
|
["same day as current day", nowMs - HOUR_MS, "Today"],
|
|
|
|
["day before the current day", nowMs - HOUR_MS * 12, "Yesterday"],
|
|
|
|
["2 days ago", nowMs - DAY_MS * 2, "Wednesday"],
|
|
|
|
["144 hours ago", nowMs - HOUR_MS * 144, "Sat, Dec 11 2021"],
|
2021-12-21 12:08:22 +03:00
|
|
|
[
|
2022-12-12 14:24:14 +03:00
|
|
|
"6 days ago, but less than 144h",
|
|
|
|
new Date("Saturday Dec 11 2021 23:59:00 GMT+0100 (Central European Standard Time)").getTime(),
|
|
|
|
"Saturday",
|
2021-12-21 12:08:22 +03:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
global.Date = MockDate as unknown as DateConstructor;
|
2022-01-28 01:32:12 +03:00
|
|
|
(SettingsStore.getValue as jest.Mock) = jest.fn((arg) => {
|
|
|
|
if (arg === UIFeature.TimelineEnableRelativeDates) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-12-21 12:08:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
global.Date = RealDate;
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("renders the date separator correctly", () => {
|
2022-10-10 18:29:10 +03:00
|
|
|
const { asFragment } = getComponent();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2021-12-21 12:08:22 +03:00
|
|
|
expect(SettingsStore.getValue).toHaveBeenCalledWith(UIFeature.TimelineEnableRelativeDates);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each(testCases)("formats date correctly when current time is %s", (_d, ts, result) => {
|
2022-10-10 18:29:10 +03:00
|
|
|
expect(getComponent({ ts, forExport: false }).container.textContent).toEqual(result);
|
2021-12-21 12:08:22 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("when forExport is true", () => {
|
|
|
|
it.each(testCases)("formats date in full when current time is %s", (_d, ts) => {
|
|
|
|
expect(getComponent({ ts, forExport: true }).container.textContent).toEqual(
|
|
|
|
formatFullDateNoTime(new Date(ts)),
|
|
|
|
);
|
2021-12-21 12:08:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("when Settings.TimelineEnableRelativeDates is falsy", () => {
|
2021-12-21 12:08:22 +03:00
|
|
|
beforeEach(() => {
|
2022-01-28 01:32:12 +03:00
|
|
|
(SettingsStore.getValue as jest.Mock) = jest.fn((arg) => {
|
|
|
|
if (arg === UIFeature.TimelineEnableRelativeDates) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2021-12-21 12:08:22 +03:00
|
|
|
});
|
2022-12-12 14:24:14 +03:00
|
|
|
it.each(testCases)("formats date in full when current time is %s", (_d, ts) => {
|
|
|
|
expect(getComponent({ ts, forExport: false }).container.textContent).toEqual(
|
|
|
|
formatFullDateNoTime(new Date(ts)),
|
|
|
|
);
|
2021-12-21 12:08:22 +03:00
|
|
|
});
|
|
|
|
});
|
2022-01-28 01:32:12 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("when feature_jump_to_date is enabled", () => {
|
2022-01-28 01:32:12 +03:00
|
|
|
beforeEach(() => {
|
2022-11-04 13:48:08 +03:00
|
|
|
mocked(SettingsStore).getValue.mockImplementation((arg): any => {
|
2022-01-28 01:32:12 +03:00
|
|
|
if (arg === "feature_jump_to_date") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 14:24:14 +03:00
|
|
|
it("renders the date separator correctly", () => {
|
2022-10-10 18:29:10 +03:00
|
|
|
const { asFragment } = getComponent();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-01-28 01:32:12 +03:00
|
|
|
});
|
|
|
|
});
|
2021-12-21 12:08:22 +03:00
|
|
|
});
|