-
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.
Merge pull request #204 from algolia/test/port-old-tests
test(lib/InstantSearch): test using mocha, expect
- Loading branch information
Showing
11 changed files
with
195 additions
and
286 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
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
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
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,179 @@ | ||
/* eslint-env mocha */ | ||
import EventEmitter from 'events'; | ||
|
||
import expect from 'expect'; | ||
import range from 'lodash/utility/range'; | ||
import sinon from 'sinon'; | ||
|
||
import InstantSearch from '../InstantSearch'; | ||
|
||
describe('InstantSearch lifecycle', () => { | ||
var algoliasearch; | ||
var algoliasearchHelper; | ||
var client; | ||
var helper; | ||
var appId; | ||
var apiKey; | ||
var indexName; | ||
var searchParameters; | ||
var search; | ||
|
||
beforeEach(() => { | ||
client = {algolia: 'client'}; | ||
helper = new EventEmitter(); | ||
|
||
helper.search = sinon.spy(); | ||
helper.state = 'state'; | ||
|
||
algoliasearch = sinon.stub().returns(client); | ||
algoliasearchHelper = sinon.stub().returns(helper); | ||
|
||
appId = 'appId'; | ||
apiKey = 'apiKey'; | ||
indexName = 'lifeycle'; | ||
|
||
searchParameters = {some: 'configuration', values: [-2, -1], another: {config: 'parameter'}}; | ||
|
||
InstantSearch.__Rewire__('algoliasearch', algoliasearch); | ||
InstantSearch.__Rewire__('algoliasearchHelper', algoliasearchHelper); | ||
|
||
search = new InstantSearch({ | ||
appId: appId, | ||
apiKey: apiKey, | ||
indexName: indexName, | ||
searchParameters: searchParameters | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
InstantSearch.__ResetDependency__('algoliasearch'); | ||
InstantSearch.__ResetDependency__('algoliasearchHelper'); | ||
}); | ||
|
||
it('calls algoliasearch(appId, apiKey)', () => { | ||
expect(algoliasearch.calledOnce).toBe(true, 'algoliasearch called once'); | ||
expect(algoliasearch.args[0]) | ||
.toEqual([appId, apiKey]); | ||
}); | ||
|
||
it('does not call algoliasearchHelper', () => { | ||
expect(algoliasearchHelper.notCalled).toBe(true, 'algoliasearchHelper not yet called'); | ||
}); | ||
|
||
context('when adding a widget', () => { | ||
var widget; | ||
|
||
beforeEach(() => { | ||
widget = { | ||
getConfiguration: sinon.stub().returns({some: 'modified', another: {different: 'parameter'}}), | ||
init: sinon.spy(), | ||
render: sinon.spy() | ||
}; | ||
search.addWidget(widget); | ||
}); | ||
|
||
it('does not call widget.getConfiguration', () => { | ||
expect(widget.getConfiguration.notCalled).toBe(true); | ||
}); | ||
|
||
context('when we call search.start', () => { | ||
beforeEach(() => { | ||
search.start(); | ||
}); | ||
|
||
it('calls widget.getConfiguration(searchParameters)', () => { | ||
expect(widget.getConfiguration.args[0]).toEqual([searchParameters]); | ||
}); | ||
|
||
it('calls algoliasearchHelper(client, indexName, searchParameters)', () => { | ||
expect(algoliasearchHelper.calledOnce).toBe(true, 'algoliasearchHelper called once'); | ||
expect(algoliasearchHelper.args[0]) | ||
.toEqual([ | ||
client, | ||
indexName, | ||
{some: 'modified', values: [-2, -1], another: {different: 'parameter', config: 'parameter'}} | ||
]); | ||
}); | ||
|
||
it('calls helper.search()', () => { | ||
expect(helper.search.calledOnce).toBe(true); | ||
}); | ||
|
||
it('calls widget.init(helper.state, helper, templatesConfig)', () => { | ||
expect(widget.init.calledOnce).toBe(true, 'widget.init called once'); | ||
expect(widget.init.calledAfter(widget.getConfiguration)) | ||
.toBe(true, 'widget.init() was called after widget.getConfiguration()'); | ||
expect(widget.init.args[0]).toEqual([helper.state, helper, search.templatesConfig]); | ||
}); | ||
|
||
it('does not call widget.render', () => { | ||
expect(widget.render.notCalled).toBe(true); | ||
}); | ||
|
||
context('when we have results', () => { | ||
var results; | ||
|
||
beforeEach(() => { | ||
results = {some: 'data'}; | ||
helper.emit('result', results, helper.state); | ||
}); | ||
|
||
it('calls widget.render({results, state, helper, templatesConfig})', () => { | ||
expect(widget.render.calledOnce).toBe(true, 'widget.render called once'); | ||
expect(widget.render.args[0]) | ||
.toEqual([{ | ||
results, | ||
state: helper.state, | ||
helper, | ||
templatesConfig: search.templatesConfig | ||
}]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
context('when we have 5 widgets, the third one having __initLast = true', () => { | ||
var widgets; | ||
|
||
beforeEach(() => { | ||
widgets = range(5); | ||
widgets = widgets.map((widget, widgetIndex) => { | ||
widget = { | ||
getConfiguration: sinon.stub().returns({values: [widgetIndex]}) | ||
}; | ||
|
||
if (widgetIndex === 2) widget.__initLast = true; | ||
|
||
return widget; | ||
}); | ||
widgets.forEach(search.addWidget, search); | ||
search.start(); | ||
}); | ||
|
||
it('calls widget[x].getConfiguration in the orders the widgets were added', () => { | ||
let order = widgets | ||
.filter((widget, widgetIndex) => widgetIndex !== 2) | ||
.every((widget, widgetIndex, filteredWidgets) => { | ||
if (widgetIndex === 0) { | ||
return widget.getConfiguration.calledOnce && | ||
widget.getConfiguration.calledBefore(filteredWidgets[1].getConfiguration); | ||
} | ||
let previousWidget = filteredWidgets[widgetIndex - 1]; | ||
return widget.getConfiguration.calledOnce && | ||
widget.getConfiguration.calledAfter(previousWidget.getConfiguration); | ||
}); | ||
|
||
expect(order).toBe(true); | ||
}); | ||
|
||
it('calls widget[2].getConfiguration before any other widget, because of __initLast', () => { | ||
let initLastWidget = widgets[2]; | ||
let lastWidget = widgets[widgets.length - 1]; | ||
expect(initLastWidget.getConfiguration.calledAfter(lastWidget.getConfiguration)).toBe(true); | ||
}); | ||
|
||
it('recursevly merges searchParameters.values array', () => { | ||
expect(algoliasearchHelper.args[0][2].values).toEqual([-2, -1, 0, 1, 3, 4, 2]); | ||
}); | ||
}); | ||
}); |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.