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

Commit d294dad

Browse files
EECvisionturt2live
andauthored
fix timeline search with empty text box should do nothing (#8262)
* fix timeline search with empty text box should do nothing * test SearchBar component * fix lint error * Update SearchBar-test.tsx Co-authored-by: Travis Ralston <[email protected]>
1 parent cea75fd commit d294dad

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

src/components/views/rooms/SearchBar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default class SearchBar extends React.Component<IProps, IState> {
7878
}
7979

8080
private onSearch = (): void => {
81+
if (!this.searchTerm.current.value.trim()) return;
8182
this.props.onSearch(this.searchTerm.current.value, this.state.scope);
8283
};
8384

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright 2022 Emmanuel Ezeka <[email protected]>
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 DesktopBuildsNotice from "../../../../src/components/views/elements/DesktopBuildsNotice";
21+
import { PosthogScreenTracker } from "../../../../src/PosthogTrackers";
22+
import SearchBar, { SearchScope } from "../../../../src/components/views/rooms/SearchBar";
23+
import { KeyBindingAction } from "../../../../src/accessibility/KeyboardShortcuts";
24+
25+
let mockCurrentEvent = KeyBindingAction.Enter;
26+
const mockWarningKind = true;
27+
let wrapper: any = null;
28+
29+
const searchProps = {
30+
onCancelClick: jest.fn(),
31+
onSearch: jest.fn(),
32+
searchInProgress: false,
33+
isRoomEncrypted: false,
34+
};
35+
36+
jest.mock("../../../../src/KeyBindingsManager", () => ({
37+
__esModule: true,
38+
getKeyBindingsManager: jest.fn(() => (
39+
{ getAccessibilityAction: jest.fn(() => mockCurrentEvent) })),
40+
}));
41+
42+
/** mock out DesktopBuildsNotice component so it doesn't affect the result of our test */
43+
jest.mock('../../../../src/components/views/elements/DesktopBuildsNotice', () => ({
44+
__esModule: true,
45+
WarningKind: {
46+
get Search() { // eslint-disable-line @typescript-eslint/naming-convention
47+
return mockWarningKind;
48+
},
49+
},
50+
default: jest.fn(({ children }) => (
51+
<div>{ children }</div>
52+
)),
53+
}));
54+
55+
/** mock out PosthogTrackers component so it doesn't affect the result of our test */
56+
jest.mock('../../../../src/PosthogTrackers', () => ({
57+
__esModule: true,
58+
PosthogScreenTracker: jest.fn(({ children }) => (
59+
<div>{ children }</div>
60+
)),
61+
}));
62+
63+
describe("SearchBar", () => {
64+
beforeEach(() => {
65+
wrapper = mount(<SearchBar {...searchProps} />);
66+
});
67+
68+
afterEach(() => {
69+
wrapper.unmount();
70+
searchProps.onCancelClick.mockClear();
71+
searchProps.onSearch.mockClear();
72+
});
73+
74+
it("must render child components and pass necessary props", () => {
75+
const postHogScreenTracker = wrapper.find(PosthogScreenTracker);
76+
const desktopBuildNotice = wrapper.find(DesktopBuildsNotice);
77+
78+
expect(postHogScreenTracker.length).toBe(1);
79+
expect(desktopBuildNotice.length).toBe(1);
80+
expect(postHogScreenTracker.props().screenName).toEqual("RoomSearch");
81+
expect(desktopBuildNotice.props().isRoomEncrypted).toEqual(searchProps.isRoomEncrypted);
82+
expect(desktopBuildNotice.props().kind).toEqual(mockWarningKind);
83+
});
84+
85+
it("must not search when input value is empty", () => {
86+
const roomButtons = wrapper.find(".mx_SearchBar_button");
87+
const searchButton = wrapper.find(".mx_SearchBar_searchButton");
88+
89+
expect(roomButtons.length).toEqual(4);
90+
91+
searchButton.at(0).simulate("click");
92+
roomButtons.at(0).simulate("click");
93+
roomButtons.at(2).simulate("click");
94+
95+
expect(searchProps.onSearch).not.toHaveBeenCalled();
96+
});
97+
98+
it("must trigger onSearch when value is not empty", () => {
99+
const searchValue = "abcd";
100+
101+
const roomButtons = wrapper.find(".mx_SearchBar_button");
102+
const searchButton = wrapper.find(".mx_SearchBar_searchButton");
103+
const input = wrapper.find(".mx_SearchBar_input input");
104+
input.instance().value = searchValue;
105+
106+
expect(roomButtons.length).toEqual(4);
107+
108+
searchButton.at(0).simulate("click");
109+
110+
expect(searchProps.onSearch).toHaveBeenCalledTimes(1);
111+
expect(searchProps.onSearch).toHaveBeenNthCalledWith(1, searchValue, SearchScope.Room);
112+
113+
roomButtons.at(0).simulate("click");
114+
115+
expect(searchProps.onSearch).toHaveBeenCalledTimes(2);
116+
expect(searchProps.onSearch).toHaveBeenNthCalledWith(2, searchValue, SearchScope.Room);
117+
118+
roomButtons.at(2).simulate("click");
119+
120+
expect(searchProps.onSearch).toHaveBeenCalledTimes(3);
121+
expect(searchProps.onSearch).toHaveBeenNthCalledWith(3, searchValue, SearchScope.All);
122+
});
123+
124+
it("cancel button and esc key should trigger onCancelClick", () => {
125+
mockCurrentEvent = KeyBindingAction.Escape;
126+
const cancelButton = wrapper.find(".mx_SearchBar_cancel");
127+
const input = wrapper.find(".mx_SearchBar_input input");
128+
input.simulate("focus");
129+
input.simulate("keydown", { key: "ESC" });
130+
cancelButton.at(0).simulate("click");
131+
132+
expect(searchProps.onCancelClick).toHaveBeenCalledTimes(2);
133+
});
134+
});

0 commit comments

Comments
 (0)