Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit b481fc0

Browse files
author
Kerry
authored
Wrapping component to show/hide UI based on UIFeature setting (#7585)
* add wrapping component for hiding UI Signed-off-by: Kerry Archibald <[email protected]> * rename and move to components Signed-off-by: Kerry Archibald <[email protected]> * prefix interface and add missing copyright Signed-off-by: Kerry Archibald <[email protected]> * remove roomId prop Signed-off-by: Kerry Archibald <[email protected]>
1 parent 6ac3a92 commit b481fc0

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 React from 'react';
18+
19+
import SettingsStore from '../../../settings/SettingsStore';
20+
import { UIFeature } from '../../../settings/UIFeature';
21+
22+
interface IProps {
23+
uiFeature: UIFeature;
24+
}
25+
const UiFeatureSettingWrapper: React.FC<IProps> = ({ children, uiFeature }) => {
26+
const settingValue = SettingsStore.getValue(uiFeature);
27+
return settingValue && children ? <>{ children }</> : null;
28+
};
29+
30+
export default UiFeatureSettingWrapper;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 React from 'react';
18+
import { mount } from 'enzyme';
19+
20+
import SettingsStore from '../../../../src/settings/SettingsStore';
21+
import UiFeatureSettingWrapper from '../../../../src/components/views/settings/UiFeatureSettingWrapper';
22+
import { UIFeature } from '../../../../src/settings/UIFeature';
23+
24+
jest.mock('../../../../src/settings/SettingsStore');
25+
26+
describe('<UiFeatureSettingWrapper>', () => {
27+
const defaultProps = {
28+
uiFeature: UIFeature.Flair,
29+
children: <div>test</div>,
30+
};
31+
const getComponent = (props = {}) => mount(<UiFeatureSettingWrapper {...defaultProps} {...props} />);
32+
33+
beforeEach(() => {
34+
(SettingsStore.getValue as jest.Mock).mockClear().mockReturnValue(true);
35+
});
36+
37+
it('renders children when setting is truthy', () => {
38+
const component = getComponent();
39+
40+
expect(component).toMatchSnapshot();
41+
expect(SettingsStore.getValue).toHaveBeenCalledWith(defaultProps.uiFeature);
42+
});
43+
44+
it('returns null when setting is truthy but children are undefined', () => {
45+
const component = getComponent({ children: undefined });
46+
47+
expect(component.html()).toBeNull();
48+
});
49+
50+
it('returns null when setting is falsy', () => {
51+
(SettingsStore.getValue as jest.Mock).mockReturnValue(false);
52+
const component = getComponent();
53+
54+
expect(component.html()).toBeNull();
55+
});
56+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<UiFeatureSettingWrapper> renders children when setting is truthy 1`] = `
4+
<UiFeatureSettingWrapper
5+
uiFeature="UIFeature.flair"
6+
>
7+
<div>
8+
test
9+
</div>
10+
</UiFeatureSettingWrapper>
11+
`;

0 commit comments

Comments
 (0)