|
| 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 IncompatibleController from "../../../src/settings/controllers/IncompatibleController"; |
| 18 | +import { SettingLevel } from "../../../src/settings/SettingLevel"; |
| 19 | +import SettingsStore from "../../../src/settings/SettingsStore"; |
| 20 | + |
| 21 | +describe('IncompatibleController', () => { |
| 22 | + const settingsGetValueSpy = jest.spyOn(SettingsStore, 'getValue'); |
| 23 | + beforeEach(() => { |
| 24 | + settingsGetValueSpy.mockClear(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('incompatibleSetting', () => { |
| 28 | + describe('when incompatibleValue is not set', () => { |
| 29 | + it('returns true when setting value is true', () => { |
| 30 | + // no incompatible value set, defaulted to true |
| 31 | + const controller = new IncompatibleController("feature_spotlight", { key: null }); |
| 32 | + settingsGetValueSpy.mockReturnValue(true); |
| 33 | + // true === true |
| 34 | + expect(controller.incompatibleSetting).toBe(true); |
| 35 | + expect(controller.settingDisabled).toEqual(true); |
| 36 | + expect(settingsGetValueSpy).toHaveBeenCalledWith("feature_spotlight"); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns false when setting value is not true', () => { |
| 40 | + // no incompatible value set, defaulted to true |
| 41 | + const controller = new IncompatibleController("feature_spotlight", { key: null }); |
| 42 | + settingsGetValueSpy.mockReturnValue('test'); |
| 43 | + expect(controller.incompatibleSetting).toBe(false); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('when incompatibleValue is set to a value', () => { |
| 48 | + it('returns true when setting value matches incompatible value', () => { |
| 49 | + const controller = new IncompatibleController("feature_spotlight", { key: null }, 'test'); |
| 50 | + settingsGetValueSpy.mockReturnValue('test'); |
| 51 | + expect(controller.incompatibleSetting).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns false when setting value is not true', () => { |
| 55 | + const controller = new IncompatibleController("feature_spotlight", { key: null }, 'test'); |
| 56 | + settingsGetValueSpy.mockReturnValue('not test'); |
| 57 | + expect(controller.incompatibleSetting).toBe(false); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('when incompatibleValue is set to a function', () => { |
| 62 | + it('returns result from incompatibleValue function', () => { |
| 63 | + const incompatibleValueFn = jest.fn().mockReturnValue(false); |
| 64 | + const controller = new IncompatibleController("feature_spotlight", { key: null }, incompatibleValueFn); |
| 65 | + settingsGetValueSpy.mockReturnValue('test'); |
| 66 | + expect(controller.incompatibleSetting).toBe(false); |
| 67 | + expect(incompatibleValueFn).toHaveBeenCalledWith('test'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('getValueOverride()', () => { |
| 73 | + it('returns forced value when setting is incompatible', () => { |
| 74 | + settingsGetValueSpy.mockReturnValue(true); |
| 75 | + const forcedValue = { key: null }; |
| 76 | + const controller = new IncompatibleController("feature_spotlight", forcedValue); |
| 77 | + expect(controller.getValueOverride( |
| 78 | + SettingLevel.ACCOUNT, '$room:server', true, SettingLevel.ACCOUNT, |
| 79 | + )).toEqual(forcedValue); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns null when setting is not incompatible', () => { |
| 83 | + settingsGetValueSpy.mockReturnValue(false); |
| 84 | + const forcedValue = { key: null }; |
| 85 | + const controller = new IncompatibleController("feature_spotlight", forcedValue); |
| 86 | + expect(controller.getValueOverride( |
| 87 | + SettingLevel.ACCOUNT, '$room:server', true, SettingLevel.ACCOUNT, |
| 88 | + )).toEqual(null); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments