2022-07-18 14:00:56 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 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 { mocked } from "jest-mock";
|
2022-12-13 15:59:52 +01:00
|
|
|
import { MatrixEvent, EventType, MsgType } from "matrix-js-sdk/src/matrix";
|
2022-07-18 14:00:56 +02:00
|
|
|
|
|
|
|
import { haveRendererForEvent } from "../src/events/EventTileFactory";
|
2022-12-13 15:59:52 +01:00
|
|
|
import { getMockClientWithEventEmitter, makeBeaconEvent, mockClientMethodsUser } from "./test-utils";
|
|
|
|
import { eventTriggersUnreadCount } from "../src/Unread";
|
2022-07-18 14:00:56 +02:00
|
|
|
|
|
|
|
jest.mock("../src/events/EventTileFactory", () => ({
|
|
|
|
haveRendererForEvent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
describe("eventTriggersUnreadCount()", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const aliceId = "@alice:server.org";
|
2022-12-13 15:59:52 +01:00
|
|
|
const bobId = "@bob:server.org";
|
2022-07-18 14:00:56 +02:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
// mock user credentials
|
|
|
|
getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(bobId),
|
2022-07-18 16:23:24 +02:00
|
|
|
});
|
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
// setup events
|
|
|
|
const alicesMessage = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: aliceId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
|
|
|
body: "Hello from Alice",
|
|
|
|
},
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
const bobsMessage = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: bobId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
|
|
|
body: "Hello from Bob",
|
|
|
|
},
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
const redactedEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: aliceId,
|
|
|
|
});
|
|
|
|
redactedEvent.makeRedacted(redactedEvent);
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
mocked(haveRendererForEvent).mockClear().mockReturnValue(false);
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
it("returns false when the event was sent by the current user", () => {
|
|
|
|
expect(eventTriggersUnreadCount(bobsMessage)).toBe(false);
|
|
|
|
// returned early before checking renderer
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
it("returns false for a redacted event", () => {
|
|
|
|
expect(eventTriggersUnreadCount(redactedEvent)).toBe(false);
|
|
|
|
// returned early before checking renderer
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
it("returns false for an event without a renderer", () => {
|
|
|
|
mocked(haveRendererForEvent).mockReturnValue(false);
|
|
|
|
expect(eventTriggersUnreadCount(alicesMessage)).toBe(false);
|
|
|
|
expect(haveRendererForEvent).toHaveBeenCalledWith(alicesMessage, false);
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
it("returns true for an event with a renderer", () => {
|
|
|
|
mocked(haveRendererForEvent).mockReturnValue(true);
|
|
|
|
expect(eventTriggersUnreadCount(alicesMessage)).toBe(true);
|
|
|
|
expect(haveRendererForEvent).toHaveBeenCalledWith(alicesMessage, false);
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
it("returns false for beacon locations", () => {
|
|
|
|
const beaconLocationEvent = makeBeaconEvent(aliceId);
|
|
|
|
expect(eventTriggersUnreadCount(beaconLocationEvent)).toBe(false);
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-12-12 15:34:05 +01:00
|
|
|
|
2022-12-13 15:59:52 +01:00
|
|
|
const noUnreadEventTypes = [
|
|
|
|
EventType.RoomMember,
|
|
|
|
EventType.RoomThirdPartyInvite,
|
|
|
|
EventType.CallAnswer,
|
|
|
|
EventType.CallHangup,
|
|
|
|
EventType.RoomCanonicalAlias,
|
|
|
|
EventType.RoomServerAcl,
|
|
|
|
];
|
|
|
|
|
|
|
|
it.each(noUnreadEventTypes)("returns false without checking for renderer for events with type %s", (eventType) => {
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: eventType,
|
|
|
|
sender: aliceId,
|
2022-07-18 14:00:56 +02:00
|
|
|
});
|
2022-12-13 15:59:52 +01:00
|
|
|
expect(eventTriggersUnreadCount(event)).toBe(false);
|
|
|
|
expect(haveRendererForEvent).not.toHaveBeenCalled();
|
2022-07-18 14:00:56 +02:00
|
|
|
});
|
|
|
|
});
|