Skip to content

Commit 618dca2

Browse files
author
Alexandre Stanislawski
committed
feat(connector): refactor search function
1 parent 70f8e1f commit 618dca2

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed

src/connectors/search-box/connectSearchBox.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,25 @@ const connectSearchBox = searchBoxRendering => ({
6464
poweredBy = {};
6565
}
6666

67+
const search = helper => {
68+
let previousQuery;
69+
70+
const setQueryAndSearch = (q, doSearch = true) => {
71+
if (q !== helper.state.query) {
72+
previousQuery = helper.state.query;
73+
helper.setQuery(q);
74+
}
75+
if (doSearch && previousQuery !== undefined && previousQuery !== q) helper.search();
76+
};
77+
78+
return queryHook ?
79+
q => queryHook(q, setQueryAndSearch) :
80+
setQueryAndSearch;
81+
};
82+
6783
return {
6884
init({state, helper, onHistoryChange}) {
85+
this._search = search(helper);
6986
searchBoxRendering({
7087
query: state.query,
7188
containerNode,
@@ -79,6 +96,7 @@ const connectSearchBox = searchBoxRendering => ({
7996
placeholder,
8097
cssClasses,
8198
templates: defaultTemplates,
99+
search: this._search,
82100
}, true);
83101
},
84102
render({helper}) {
@@ -95,6 +113,7 @@ const connectSearchBox = searchBoxRendering => ({
95113
placeholder,
96114
cssClasses,
97115
templates: defaultTemplates,
116+
search: this._search,
98117
}, false);
99118
},
100119
};

src/widgets/search-box/__tests__/search-box-test.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,11 @@ describe('searchBox()', () => {
412412
});
413413

414414
it('updates the query', () => {
415-
expect(helper.setQuery.calledOnce).toBe(true);
415+
expect(helper.setQuery.callCount).toBe(1);
416416
});
417417

418418
it('does not search', () => {
419-
expect(helper.search.called).toBe(false);
419+
expect(helper.search.callCount).toBe(0);
420420
});
421421
});
422422

@@ -475,18 +475,40 @@ describe('searchBox()', () => {
475475
context('non-instant search', () => {
476476
beforeEach(() => {
477477
widget = searchBox({container, searchOnEnterKeyPressOnly: true});
478+
helper.state.query = 'tes';
479+
widget.init({state: helper.state, helper, onHistoryChange});
478480
});
479481

480482
it('performs the search on keyup if <ENTER>', () => {
481-
simulateInputEvent('test', 'tes', widget, helper, state, container);
482-
simulateKeyUpEvent({keyCode: 13}, widget, helper, state, container);
483-
expect(helper.search.calledOnce).toBe(true);
483+
// simulateInputEvent('test', 'tes', widget, helper, state, container);
484+
// simulateKeyUpEvent({keyCode: 13}, widget, helper, state, container);
485+
container.value = 'test';
486+
const e1 = new window.Event('input');
487+
container.dispatchEvent(e1);
488+
489+
expect(helper.setQuery.callCount).toBe(1);
490+
expect(helper.search.callCount).toBe(0);
491+
492+
// setQuery is mocked and does not apply the modification of the helper
493+
// we have to set it ourselves
494+
helper.state.query = container.value;
495+
496+
const e2 = new window.Event('keyup', {keyCode: 13});
497+
Object.defineProperty(e2, 'keyCode', {get: () => 13});
498+
container.dispatchEvent(e2);
499+
500+
expect(helper.setQuery.callCount).toBe(1);
501+
expect(helper.search.callCount).toBe(1);
484502
});
485503

486504
it('doesn\'t perform the search on keyup if not <ENTER>', () => {
487-
simulateKeyUpEvent({}, widget, helper, state, container);
488-
expect(helper.setQuery.called).toBe(false);
489-
expect(helper.search.called).toBe(false);
505+
container.value = 'test';
506+
const event = new window.Event('keyup', {keyCode: 42});
507+
Object.defineProperty(event, 'keyCode', {get: () => 42});
508+
container.dispatchEvent(event);
509+
510+
expect(helper.setQuery.callCount).toBe(0);
511+
expect(helper.search.callCount).toBe(0);
490512
});
491513
});
492514
});
@@ -637,7 +659,8 @@ function simulateInputEvent(query, stateQuery, widget, helper, state, container)
637659
}
638660

639661
// When
640-
widget.init({state, helper, onHistoryChange});
662+
widget.init({state: helper.state, helper, onHistoryChange});
663+
641664
// Then
642665
container.value = query;
643666
const event = new window.Event('input');

src/widgets/search-box/search-box.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function defaultRendering({
5656
placeholder,
5757
cssClasses,
5858
templates,
59+
search,
5960
}, isFirstRendering) {
6061
if (isFirstRendering) {
6162
const INPUT_EVENT = window.addEventListener ?
@@ -102,38 +103,21 @@ function defaultRendering({
102103
input.setSelectionRange(helper.state.query.length, helper.state.query.length);
103104
}
104105

105-
let previousQuery;
106-
const search = q => {
107-
if (previousQuery !== undefined && previousQuery !== q) helper.search();
108-
};
109-
const setQuery = q => {
110-
if (q !== helper.state.query) {
111-
previousQuery = helper.state.query;
112-
helper.setQuery(q);
113-
}
114-
};
115-
const setQueryAndSearch = q => {
116-
setQuery(q);
117-
search(q);
118-
};
119-
const maybeSearch = queryHook ? q => queryHook(q, setQueryAndSearch) : search;
120-
121-
// always set the query every keystrokes when there's no queryHook
122-
if (!queryHook) {
123-
addListener(input, INPUT_EVENT, getInputValueAndCall(setQuery));
124-
}
125-
126106
// search on enter
127107
if (searchOnEnterKeyPressOnly) {
128-
addListener(input, 'keyup', ifKey(KEY_ENTER, getInputValueAndCall(maybeSearch)));
108+
addListener(input, INPUT_EVENT, e => {
109+
search(getValue(e), false);
110+
});
111+
addListener(input, 'keyup', e => {
112+
if (e.keyCode === KEY_ENTER) search(getValue(e));
113+
});
129114
} else {
130-
addListener(input, INPUT_EVENT, getInputValueAndCall(maybeSearch));
115+
addListener(input, INPUT_EVENT, getInputValueAndCall(search));
131116

132117
// handle IE8 weirdness where BACKSPACE key will not trigger an input change..
133118
// can be removed as soon as we remove support for it
134119
if (INPUT_EVENT === 'propertychange' || window.attachEvent) {
135-
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, getInputValueAndCall(setQuery)));
136-
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, getInputValueAndCall(maybeSearch)));
120+
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, getInputValueAndCall(search)));
137121
}
138122
}
139123
} else {

0 commit comments

Comments
 (0)