Skip to content

Commit b4d7e1b

Browse files
author
Alexandre Stanislawski
committed
feat(connector): test connectSearchBox
1 parent 4f6c180 commit b4d7e1b

File tree

2 files changed

+190
-1
lines changed

2 files changed

+190
-1
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/* eslint-env mocha */
2+
3+
import expect from 'expect';
4+
import sinon from 'sinon';
5+
6+
import jsHelper from 'algoliasearch-helper';
7+
const SearchResults = jsHelper.SearchResults;
8+
9+
import connectSearchBox from '../connectSearchBox.js';
10+
11+
const fakeClient = {addAlgoliaAgent: () => {}};
12+
13+
describe('connectSearchBox', () => {
14+
it('Renders during init and render', () => {
15+
const container = document.createElement('div');
16+
// test that the dummyRendering is called with the isFirstRendering
17+
// flag set accordingly
18+
const rendering = sinon.stub();
19+
const makeWidget = connectSearchBox(rendering);
20+
21+
const widget = makeWidget({
22+
container,
23+
});
24+
25+
expect(widget.getConfiguration).toBe(undefined);
26+
27+
const helper = jsHelper(fakeClient);
28+
helper.search = sinon.stub();
29+
30+
widget.init({
31+
helper,
32+
state: helper.state,
33+
createURL: () => '#',
34+
onHistoryChange: () => {},
35+
});
36+
37+
{ // should call the rendering once with isFirstRendering to true
38+
expect(rendering.callCount).toBe(1);
39+
const isFirstRendering = rendering.lastCall.args[1];
40+
expect(isFirstRendering).toBe(true);
41+
42+
// should provide good values for the first rendering
43+
const {query, containerNode, poweredBy,
44+
autofocus, searchOnEnterKeyPressOnly, placeholder} = rendering.lastCall.args[0];
45+
expect(containerNode).toBe(container);
46+
expect(query).toBe(helper.state.query);
47+
expect(poweredBy).toBe(false);
48+
expect(autofocus).toBe('auto');
49+
expect(searchOnEnterKeyPressOnly).toBe(false);
50+
expect(placeholder).toBe('');
51+
}
52+
53+
widget.render({
54+
results: new SearchResults(helper.state, [{}]),
55+
state: helper.state,
56+
helper,
57+
createURL: () => '#',
58+
});
59+
60+
{ // Should call the rendering a second time, with isFirstRendering to false
61+
expect(rendering.callCount).toBe(2);
62+
const isFirstRendering = rendering.lastCall.args[1];
63+
expect(isFirstRendering).toBe(false);
64+
65+
// should provide good values after the first search
66+
const {query, containerNode, poweredBy,
67+
autofocus, searchOnEnterKeyPressOnly, placeholder} = rendering.lastCall.args[0];
68+
expect(containerNode).toBe(container);
69+
expect(query).toBe(helper.state.query);
70+
expect(poweredBy).toBe(false);
71+
expect(autofocus).toBe('auto');
72+
expect(searchOnEnterKeyPressOnly).toBe(false);
73+
expect(placeholder).toBe('');
74+
}
75+
});
76+
77+
it('Provides a function to update the refinements at each step', () => {
78+
const container = document.createElement('div');
79+
const rendering = sinon.stub();
80+
const makeWidget = connectSearchBox(rendering);
81+
82+
const widget = makeWidget({
83+
container,
84+
});
85+
86+
const helper = jsHelper(fakeClient);
87+
helper.search = sinon.stub();
88+
89+
widget.init({
90+
helper,
91+
state: helper.state,
92+
createURL: () => '#',
93+
onHistoryChange: () => {},
94+
});
95+
96+
{ // first rendering
97+
expect(helper.state.query).toBe('');
98+
const renderOptions = rendering.lastCall.args[0];
99+
const {search} = renderOptions;
100+
search('bip');
101+
expect(helper.state.query).toBe('bip');
102+
expect(helper.search.callCount).toBe(1);
103+
}
104+
105+
widget.render({
106+
results: new SearchResults(helper.state, [{}]),
107+
state: helper.state,
108+
helper,
109+
createURL: () => '#',
110+
});
111+
112+
{ // Second rendering
113+
expect(helper.state.query).toBe('bip');
114+
const renderOptions = rendering.lastCall.args[0];
115+
const {search, query} = renderOptions;
116+
expect(query).toBe('bip');
117+
search('bop');
118+
expect(helper.state.query).toBe('bop');
119+
expect(helper.search.callCount).toBe(2);
120+
}
121+
});
122+
123+
it('queryHook parameter let the dev control the behavior of the search', () => {
124+
const container = document.createElement('div');
125+
const rendering = sinon.stub();
126+
const makeWidget = connectSearchBox(rendering);
127+
128+
// letSearchThrough will control if the provided function should be called
129+
let letSearchThrough = false;
130+
const queryHook = sinon.spy((q, search) => { if (letSearchThrough) search(q); });
131+
132+
const widget = makeWidget({
133+
container,
134+
queryHook,
135+
});
136+
137+
const helper = jsHelper(fakeClient);
138+
helper.search = sinon.stub();
139+
140+
widget.init({
141+
helper,
142+
state: helper.state,
143+
createURL: () => '#',
144+
onHistoryChange: () => {},
145+
});
146+
147+
{ // first rendering
148+
const renderOptions = rendering.lastCall.args[0];
149+
const {search} = renderOptions;
150+
151+
search('bip');
152+
expect(queryHook.callCount).toBe(1);
153+
expect(helper.state.query).toBe('');
154+
expect(helper.search.callCount).toBe(0);
155+
156+
letSearchThrough = true;
157+
search('bip');
158+
expect(queryHook.callCount).toBe(2);
159+
expect(helper.state.query).toBe('bip');
160+
expect(helper.search.callCount).toBe(1);
161+
}
162+
163+
// reset the hook behavior
164+
letSearchThrough = false;
165+
166+
widget.render({
167+
results: new SearchResults(helper.state, [{}]),
168+
state: helper.state,
169+
helper,
170+
createURL: () => '#',
171+
});
172+
173+
{ // Second rendering
174+
const renderOptions = rendering.lastCall.args[0];
175+
const {search} = renderOptions;
176+
177+
search('bop');
178+
expect(queryHook.callCount).toBe(3);
179+
expect(helper.state.query).toBe('bip');
180+
expect(helper.search.callCount).toBe(1);
181+
182+
letSearchThrough = true;
183+
search('bop');
184+
expect(queryHook.callCount).toBe(4);
185+
expect(helper.state.query).toBe('bop');
186+
expect(helper.search.callCount).toBe(2);
187+
}
188+
});
189+
});

src/connectors/search-box/connectSearchBox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const connectSearchBox = searchBoxRendering => ({
8787
searchBoxRendering({
8888
query: helper.state.query,
8989
containerNode,
90-
onHistoryChange,
90+
onHistoryChange: this._onHistoryChange,
9191
poweredBy,
9292
wrapInput,
9393
autofocus,

0 commit comments

Comments
 (0)