Skip to content

Commit 61231ee

Browse files
author
Kerry
authored
Unit test settings controllers pt 1 (matrix-org#8155)
* test FontSizeController Signed-off-by: Kerry Archibald <[email protected]> * test IncompatibleController Signed-off-by: Kerry Archibald <[email protected]> * test SystemFontController * test ThemeController Signed-off-by: Kerry Archibald <[email protected]> * test UseSystemFontController Signed-off-by: Kerry Archibald <[email protected]> * fix themecontroller Signed-off-by: Kerry Archibald <[email protected]>
1 parent e844938 commit 61231ee

5 files changed

+281
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { Action } from "../../../src/dispatcher/actions";
18+
import dis from "../../../src/dispatcher/dispatcher";
19+
import FontSizeController from "../../../src/settings/controllers/FontSizeController";
20+
import { SettingLevel } from "../../../src/settings/SettingLevel";
21+
22+
const dispatchSpy = jest.spyOn(dis, 'dispatch');
23+
24+
describe('FontSizeController', () => {
25+
it('dispatches a font size action on change', () => {
26+
const controller = new FontSizeController();
27+
28+
controller.onChange(SettingLevel.ACCOUNT, '$room:server', 12);
29+
30+
expect(dispatchSpy).toHaveBeenCalledWith({
31+
action: Action.UpdateFontSize,
32+
size: 12,
33+
});
34+
});
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 { Action } from "../../../src/dispatcher/actions";
18+
import dis from "../../../src/dispatcher/dispatcher";
19+
import SystemFontController from "../../../src/settings/controllers/SystemFontController";
20+
import { SettingLevel } from "../../../src/settings/SettingLevel";
21+
import SettingsStore from "../../../src/settings/SettingsStore";
22+
23+
const dispatchSpy = jest.spyOn(dis, 'dispatch');
24+
25+
describe('SystemFontController', () => {
26+
it('dispatches a font size action on change', () => {
27+
const getValueSpy = jest.spyOn(SettingsStore, 'getValue').mockReturnValue(true);
28+
const controller = new SystemFontController();
29+
30+
controller.onChange(SettingLevel.ACCOUNT, '$room:server', 12);
31+
32+
expect(dispatchSpy).toHaveBeenCalledWith({
33+
action: Action.UpdateSystemFont,
34+
useSystemFont: true,
35+
font: 12,
36+
});
37+
38+
expect(getValueSpy).toHaveBeenCalledWith("useSystemFont");
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 ThemeController from "../../../src/settings/controllers/ThemeController";
18+
import { SettingLevel } from "../../../src/settings/SettingLevel";
19+
import SettingsStore from "../../../src/settings/SettingsStore";
20+
import { DEFAULT_THEME } from "../../../src/theme";
21+
22+
describe('ThemeController', () => {
23+
jest.spyOn(SettingsStore, 'getValue').mockReturnValue([]);
24+
25+
afterEach(() => {
26+
// reset
27+
ThemeController.isLogin = false;
28+
});
29+
30+
it('returns null when calculatedValue is falsy', () => {
31+
const controller = new ThemeController();
32+
33+
expect(controller.getValueOverride(
34+
SettingLevel.ACCOUNT,
35+
'$room:server',
36+
undefined, /* calculatedValue */
37+
SettingLevel.ACCOUNT,
38+
)).toEqual(null);
39+
});
40+
41+
it('returns light when login flag is set', () => {
42+
const controller = new ThemeController();
43+
44+
ThemeController.isLogin = true;
45+
46+
expect(controller.getValueOverride(
47+
SettingLevel.ACCOUNT,
48+
'$room:server',
49+
'dark',
50+
SettingLevel.ACCOUNT,
51+
)).toEqual('light');
52+
});
53+
54+
it('returns default theme when value is not a valid theme', () => {
55+
const controller = new ThemeController();
56+
57+
expect(controller.getValueOverride(
58+
SettingLevel.ACCOUNT,
59+
'$room:server',
60+
'my-test-theme',
61+
SettingLevel.ACCOUNT,
62+
)).toEqual(DEFAULT_THEME);
63+
});
64+
65+
it('returns null when value is a valid theme', () => {
66+
const controller = new ThemeController();
67+
68+
expect(controller.getValueOverride(
69+
SettingLevel.ACCOUNT,
70+
'$room:server',
71+
'dark',
72+
SettingLevel.ACCOUNT,
73+
)).toEqual(null);
74+
});
75+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 { Action } from "../../../src/dispatcher/actions";
18+
import dis from "../../../src/dispatcher/dispatcher";
19+
import UseSystemFontController from "../../../src/settings/controllers/UseSystemFontController";
20+
import { SettingLevel } from "../../../src/settings/SettingLevel";
21+
import SettingsStore from "../../../src/settings/SettingsStore";
22+
23+
const dispatchSpy = jest.spyOn(dis, 'dispatch');
24+
25+
describe('UseSystemFontController', () => {
26+
it('dispatches a font size action on change', () => {
27+
const getValueSpy = jest.spyOn(SettingsStore, 'getValue').mockReturnValue(12);
28+
const controller = new UseSystemFontController();
29+
30+
controller.onChange(SettingLevel.ACCOUNT, '$room:server', true);
31+
32+
expect(dispatchSpy).toHaveBeenCalledWith({
33+
action: Action.UpdateSystemFont,
34+
useSystemFont: true,
35+
font: 12,
36+
});
37+
38+
expect(getValueSpy).toHaveBeenCalledWith("systemFont");
39+
});
40+
});

0 commit comments

Comments
 (0)