-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexandre Stanislawski
committed
Feb 21, 2017
1 parent
9caab02
commit 89c86a5
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-env mocha */ | ||
|
||
import expect from 'expect'; | ||
import sinon from 'sinon'; | ||
|
||
import jsHelper from 'algoliasearch-helper'; | ||
const SearchResults = jsHelper.SearchResults; | ||
|
||
import connectHits from '../connectHits.js'; | ||
|
||
const fakeClient = {addAlgoliaAgent: () => {}}; | ||
|
||
describe('connectHits', () => { | ||
it('Renders during init and render', () => { | ||
const container = document.createElement('div'); | ||
// test that the dummyRendering is called with the isFirstRendering | ||
// flag set accordingly | ||
const rendering = sinon.stub(); | ||
const makeWidget = connectHits(rendering); | ||
const widget = makeWidget({ | ||
container, | ||
hitsPerPage: 10, | ||
}); | ||
|
||
const config = widget.getConfiguration(); | ||
expect(config).toEqual({hitsPerPage: 10}); | ||
|
||
// test if widget is not rendered yet at this point | ||
expect(rendering.callCount).toBe(0); | ||
|
||
const helper = jsHelper(fakeClient, '', config); | ||
helper.search = sinon.stub(); | ||
|
||
widget.init({ | ||
helper, | ||
state: helper.state, | ||
createURL: () => '#', | ||
onHistoryChange: () => {}, | ||
}); | ||
|
||
// test that rendering has been called during init with isFirstRendering = true | ||
expect(rendering.callCount).toBe(1); | ||
// test if isFirstRendering is true during init | ||
expect(rendering.lastCall.args[1]).toBe(true); | ||
|
||
const firstRenderingOptions = rendering.lastCall.args[0]; | ||
expect(firstRenderingOptions.containerNode).toBe(container); | ||
|
||
widget.render({ | ||
results: new SearchResults(helper.state, [{}]), | ||
state: helper.state, | ||
helper, | ||
createURL: () => '#', | ||
}); | ||
|
||
// test that rendering has been called during init with isFirstRendering = false | ||
expect(rendering.callCount).toBe(2); | ||
expect(rendering.lastCall.args[1]).toBe(false); | ||
|
||
const secondRenderingOptions = rendering.lastCall.args[0]; | ||
expect(secondRenderingOptions.containerNode).toBe(container); | ||
}); | ||
|
||
it('Provides the hits and the whole results', () => { | ||
const container = document.createElement('div'); | ||
const rendering = sinon.stub(); | ||
const makeWidget = connectHits(rendering); | ||
const widget = makeWidget({ | ||
container, | ||
}); | ||
|
||
const helper = jsHelper(fakeClient, '', {}); | ||
helper.search = sinon.stub(); | ||
|
||
widget.init({ | ||
helper, | ||
state: helper.state, | ||
createURL: () => '#', | ||
onHistoryChange: () => {}, | ||
}); | ||
|
||
const firstRenderingOptions = rendering.lastCall.args[0]; | ||
expect(firstRenderingOptions.hits).toEqual([]); | ||
expect(firstRenderingOptions.results).toBe(undefined); | ||
|
||
const hits = [ | ||
{fake: 'data'}, | ||
{sample: 'infos'}, | ||
]; | ||
const results = new SearchResults(helper.state, [{hits}]); | ||
widget.render({ | ||
results, | ||
state: helper.state, | ||
helper, | ||
createURL: () => '#', | ||
}); | ||
|
||
const secondRenderingOptions = rendering.lastCall.args[0]; | ||
expect(secondRenderingOptions.hits).toEqual(hits); | ||
expect(secondRenderingOptions.results).toEqual(results); | ||
}); | ||
}); |