|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { MatrixEvent } from "matrix-js-sdk/src/models/event"; |
| 18 | + |
| 19 | +import Notifier from "../src/Notifier"; |
| 20 | +import { getLocalNotificationAccountDataEventType } from "../src/utils/notifications"; |
| 21 | +import { getMockClientWithEventEmitter, mkEvent, mkRoom, mockPlatformPeg } from "./test-utils"; |
| 22 | + |
| 23 | +describe("Notifier", () => { |
| 24 | + let MockPlatform; |
| 25 | + let accountDataStore = {}; |
| 26 | + |
| 27 | + const mockClient = getMockClientWithEventEmitter({ |
| 28 | + getUserId: jest.fn().mockReturnValue("@bob:example.org"), |
| 29 | + isGuest: jest.fn().mockReturnValue(false), |
| 30 | + getAccountData: jest.fn().mockImplementation(eventType => accountDataStore[eventType]), |
| 31 | + setAccountData: jest.fn().mockImplementation((eventType, content) => { |
| 32 | + accountDataStore[eventType] = new MatrixEvent({ |
| 33 | + type: eventType, |
| 34 | + content, |
| 35 | + }); |
| 36 | + }), |
| 37 | + }); |
| 38 | + const accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId); |
| 39 | + const roomId = "!room1:server"; |
| 40 | + const testEvent = mkEvent({ |
| 41 | + event: true, |
| 42 | + type: "m.room.message", |
| 43 | + user: "@user1:server", |
| 44 | + room: roomId, |
| 45 | + content: {}, |
| 46 | + }); |
| 47 | + const testRoom = mkRoom(mockClient, roomId); |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + accountDataStore = {}; |
| 51 | + MockPlatform = mockPlatformPeg({ |
| 52 | + supportsNotifications: jest.fn().mockReturnValue(true), |
| 53 | + maySendNotifications: jest.fn().mockReturnValue(true), |
| 54 | + displayNotification: jest.fn(), |
| 55 | + }); |
| 56 | + |
| 57 | + Notifier.isBodyEnabled = jest.fn().mockReturnValue(true); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("_displayPopupNotification", () => { |
| 61 | + it.each([ |
| 62 | + { silenced: true, count: 0 }, |
| 63 | + { silenced: false, count: 1 }, |
| 64 | + ])("does not dispatch when notifications are silenced", ({ silenced, count }) => { |
| 65 | + mockClient.setAccountData(accountDataEventKey, { is_silenced: silenced }); |
| 66 | + Notifier._displayPopupNotification(testEvent, testRoom); |
| 67 | + expect(MockPlatform.displayNotification).toHaveBeenCalledTimes(count); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("_playAudioNotification", () => { |
| 72 | + it.each([ |
| 73 | + { silenced: true, count: 0 }, |
| 74 | + { silenced: false, count: 1 }, |
| 75 | + ])("does not dispatch when notifications are silenced", ({ silenced, count }) => { |
| 76 | + // It's not ideal to only look at whether this function has been called |
| 77 | + // but avoids starting to look into DOM stuff |
| 78 | + Notifier.getSoundForRoom = jest.fn(); |
| 79 | + |
| 80 | + mockClient.setAccountData(accountDataEventKey, { is_silenced: silenced }); |
| 81 | + Notifier._playAudioNotification(testEvent, testRoom); |
| 82 | + expect(Notifier.getSoundForRoom).toHaveBeenCalledTimes(count); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments