2022-09-29 01:13:09 +08: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 { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
|
|
|
import {
|
2022-09-29 22:23:02 +08:00
|
|
|
localNotificationsAreSilenced,
|
2022-09-29 01:13:09 +08:00
|
|
|
getLocalNotificationAccountDataEventType,
|
2022-10-10 22:18:38 +08:00
|
|
|
createLocalNotificationSettingsIfNeeded,
|
|
|
|
deviceNotificationSettingsKeys,
|
2022-09-29 01:13:09 +08:00
|
|
|
} from "../../src/utils/notifications";
|
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import { getMockClientWithEventEmitter } from "../test-utils/client";
|
|
|
|
|
|
|
|
jest.mock("../../src/settings/SettingsStore");
|
|
|
|
|
|
|
|
describe('notifications', () => {
|
2022-09-29 22:23:02 +08:00
|
|
|
let accountDataStore = {};
|
2022-10-13 20:15:34 +08:00
|
|
|
let mockClient;
|
|
|
|
let accountDataEventKey;
|
2022-09-29 01:13:09 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-10-13 20:15:34 +08:00
|
|
|
jest.clearAllMocks();
|
|
|
|
mockClient = getMockClientWithEventEmitter({
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
getAccountData: jest.fn().mockImplementation(eventType => accountDataStore[eventType]),
|
|
|
|
setAccountData: jest.fn().mockImplementation((eventType, content) => {
|
|
|
|
accountDataStore[eventType] = new MatrixEvent({
|
|
|
|
type: eventType,
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
});
|
2022-09-29 22:23:02 +08:00
|
|
|
accountDataStore = {};
|
2022-10-13 20:15:34 +08:00
|
|
|
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId);
|
2022-09-29 01:13:09 +08:00
|
|
|
mocked(SettingsStore).getValue.mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
2022-10-10 22:18:38 +08:00
|
|
|
describe('createLocalNotification', () => {
|
|
|
|
it('creates account data event', async () => {
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event?.getContent().is_silenced).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-10-13 20:15:34 +08:00
|
|
|
it('does not do anything for guests', async () => {
|
|
|
|
mockClient.isGuest.mockReset().mockReturnValue(true);
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-10-10 22:18:38 +08:00
|
|
|
it.each(deviceNotificationSettingsKeys)(
|
|
|
|
'unsilenced for existing sessions when %s setting is truthy',
|
|
|
|
async (settingKey) => {
|
|
|
|
mocked(SettingsStore)
|
|
|
|
.getValue
|
|
|
|
.mockImplementation((key) => {
|
|
|
|
return key === settingKey;
|
|
|
|
});
|
|
|
|
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event?.getContent().is_silenced).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not override an existing account event data", async () => {
|
|
|
|
mockClient.setAccountData(accountDataEventKey, {
|
|
|
|
is_silenced: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
await createLocalNotificationSettingsIfNeeded(mockClient);
|
|
|
|
const event = mockClient.getAccountData(accountDataEventKey);
|
|
|
|
expect(event?.getContent().is_silenced).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-29 22:23:02 +08:00
|
|
|
describe('localNotificationsAreSilenced', () => {
|
2022-10-12 00:11:11 +08:00
|
|
|
it('defaults to false when no setting exists', () => {
|
|
|
|
expect(localNotificationsAreSilenced(mockClient)).toBeFalsy();
|
2022-09-29 22:23:02 +08:00
|
|
|
});
|
|
|
|
it('checks the persisted value', () => {
|
|
|
|
mockClient.setAccountData(accountDataEventKey, { is_silenced: true });
|
|
|
|
expect(localNotificationsAreSilenced(mockClient)).toBeTruthy();
|
|
|
|
|
|
|
|
mockClient.setAccountData(accountDataEventKey, { is_silenced: false });
|
|
|
|
expect(localNotificationsAreSilenced(mockClient)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2022-09-29 01:13:09 +08:00
|
|
|
});
|