2022-07-20 10:26:25 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-07-20 10:26:25 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-07-20 10:26:25 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import TypingStore from "../../src/stores/TypingStore";
|
|
|
|
import { LOCAL_ROOM_ID_PREFIX } from "../../src/models/LocalRoom";
|
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
2022-10-19 16:14:14 +03:00
|
|
|
import { TestSdkContext } from "../TestSdkContext";
|
2022-07-20 10:26:25 +03:00
|
|
|
|
|
|
|
jest.mock("../../src/settings/SettingsStore", () => ({
|
|
|
|
getValue: jest.fn(),
|
2022-10-19 16:14:14 +03:00
|
|
|
monitorSetting: jest.fn(),
|
2022-12-06 12:59:17 +03:00
|
|
|
watchSetting: jest.fn(),
|
2022-07-20 10:26:25 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
describe("TypingStore", () => {
|
|
|
|
let typingStore: TypingStore;
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
const roomId = "!test:example.com";
|
|
|
|
const localRoomId = LOCAL_ROOM_ID_PREFIX + "test";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient = {
|
|
|
|
sendTyping: jest.fn(),
|
|
|
|
} as unknown as MatrixClient;
|
2022-10-19 16:14:14 +03:00
|
|
|
const context = new TestSdkContext();
|
|
|
|
context.client = mockClient;
|
|
|
|
typingStore = new TypingStore(context);
|
2022-12-13 18:09:15 +03:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
|
|
|
|
return name === "sendTypingNotifications";
|
2022-07-20 10:26:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("setSelfTyping", () => {
|
|
|
|
it("shouldn't do anything for a local room", () => {
|
|
|
|
typingStore.setSelfTyping(localRoomId, null, true);
|
|
|
|
expect(mockClient.sendTyping).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("in typing state true", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
typingStore.setSelfTyping(roomId, null, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should change to false when setting false", () => {
|
|
|
|
typingStore.setSelfTyping(roomId, null, false);
|
|
|
|
expect(mockClient.sendTyping).toHaveBeenCalledWith(roomId, false, 30000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should change to true when setting true", () => {
|
|
|
|
typingStore.setSelfTyping(roomId, null, true);
|
|
|
|
expect(mockClient.sendTyping).toHaveBeenCalledWith(roomId, true, 30000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("in typing state false", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
typingStore.setSelfTyping(roomId, null, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't change when setting false", () => {
|
|
|
|
typingStore.setSelfTyping(roomId, null, false);
|
|
|
|
expect(mockClient.sendTyping).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should change to true when setting true", () => {
|
|
|
|
typingStore.setSelfTyping(roomId, null, true);
|
|
|
|
expect(mockClient.sendTyping).toHaveBeenCalledWith(roomId, true, 30000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|