Skip to content

Commit 89c86a5

Browse files
author
Alexandre Stanislawski
committed
feat(connector): test connectHits
1 parent 9caab02 commit 89c86a5

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 connectHits from '../connectHits.js';
10+
11+
const fakeClient = {addAlgoliaAgent: () => {}};
12+
13+
describe('connectHits', () => {
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 = connectHits(rendering);
20+
const widget = makeWidget({
21+
container,
22+
hitsPerPage: 10,
23+
});
24+
25+
const config = widget.getConfiguration();
26+
expect(config).toEqual({hitsPerPage: 10});
27+
28+
// test if widget is not rendered yet at this point
29+
expect(rendering.callCount).toBe(0);
30+
31+
const helper = jsHelper(fakeClient, '', config);
32+
helper.search = sinon.stub();
33+
34+
widget.init({
35+
helper,
36+
state: helper.state,
37+
createURL: () => '#',
38+
onHistoryChange: () => {},
39+
});
40+
41+
// test that rendering has been called during init with isFirstRendering = true
42+
expect(rendering.callCount).toBe(1);
43+
// test if isFirstRendering is true during init
44+
expect(rendering.lastCall.args[1]).toBe(true);
45+
46+
const firstRenderingOptions = rendering.lastCall.args[0];
47+
expect(firstRenderingOptions.containerNode).toBe(container);
48+
49+
widget.render({
50+
results: new SearchResults(helper.state, [{}]),
51+
state: helper.state,
52+
helper,
53+
createURL: () => '#',
54+
});
55+
56+
// test that rendering has been called during init with isFirstRendering = false
57+
expect(rendering.callCount).toBe(2);
58+
expect(rendering.lastCall.args[1]).toBe(false);
59+
60+
const secondRenderingOptions = rendering.lastCall.args[0];
61+
expect(secondRenderingOptions.containerNode).toBe(container);
62+
});
63+
64+
it('Provides the hits and the whole results', () => {
65+
const container = document.createElement('div');
66+
const rendering = sinon.stub();
67+
const makeWidget = connectHits(rendering);
68+
const widget = makeWidget({
69+
container,
70+
});
71+
72+
const helper = jsHelper(fakeClient, '', {});
73+
helper.search = sinon.stub();
74+
75+
widget.init({
76+
helper,
77+
state: helper.state,
78+
createURL: () => '#',
79+
onHistoryChange: () => {},
80+
});
81+
82+
const firstRenderingOptions = rendering.lastCall.args[0];
83+
expect(firstRenderingOptions.hits).toEqual([]);
84+
expect(firstRenderingOptions.results).toBe(undefined);
85+
86+
const hits = [
87+
{fake: 'data'},
88+
{sample: 'infos'},
89+
];
90+
const results = new SearchResults(helper.state, [{hits}]);
91+
widget.render({
92+
results,
93+
state: helper.state,
94+
helper,
95+
createURL: () => '#',
96+
});
97+
98+
const secondRenderingOptions = rendering.lastCall.args[0];
99+
expect(secondRenderingOptions.hits).toEqual(hits);
100+
expect(secondRenderingOptions.results).toEqual(results);
101+
});
102+
});

0 commit comments

Comments
 (0)