1
0
Fork 0

Merge pull request #22 from algolia/test/source

test(getAutocompleteSource): Add tests for source
This commit is contained in:
Sylvain Utard 2015-12-18 18:25:20 +01:00
commit 698ee1b603
2 changed files with 81 additions and 22 deletions

View file

@ -50,7 +50,11 @@ class DocSearch {
source: this.getAutocompleteSource(),
templates: {
suggestion: this.getSuggestionTemplate(),
footer: '<div class="ads-footer">Search by <a class="ads-footer--logo" href="https://www.algolia.com/docsearch">Algolia</a></div>'
footer: `
<div class="ads-footer">
Search by <a class="ads-footer--logo" href="https://www.algolia.com/docsearch">Algolia</a>
</div>
`
}
}]);
this.autocomplete.on('autocomplete:selected', this.handleSelected);

View file

@ -34,10 +34,6 @@ describe('DocSearch', () => {
let algoliasearch;
let AutoComplete;
let autocomplete;
let checkArguments;
let getInputFromSelector;
let checkArgumentsInitial;
let getInputFromSelectorInitial;
let defaultOptions;
beforeEach(() => {
@ -50,28 +46,24 @@ describe('DocSearch', () => {
on: sinon.spy()
};
AutoComplete = sinon.stub().returns(autocomplete);
checkArgumentsInitial = DocSearch.checkArguments;
checkArguments = sinon.spy();
getInputFromSelectorInitial = DocSearch.getInputFromSelector;
getInputFromSelector = sinon.stub();
defaultOptions = {
indexName: 'indexName',
apiKey: 'apiKey',
inputSelector: '#input'
};
DocSearch.checkArguments = checkArguments;
DocSearch.getInputFromSelector = getInputFromSelector;
sinon.spy(DocSearch, 'checkArguments');
sinon.stub(DocSearch, 'getInputFromSelector').returns(true);
DocSearch.__Rewire__('algoliasearch', AlgoliaSearch);
DocSearch.__Rewire__('autocomplete', AutoComplete);
});
afterEach(() => {
// Cleanup the stubs on static methods
DocSearch.checkArguments = checkArgumentsInitial;
DocSearch.getInputFromSelector = getInputFromSelectorInitial;
DocSearch.checkArguments.restore();
DocSearch.getInputFromSelector.restore();
DocSearch.__ResetDependency__('algoliasearch');
DocSearch.__ResetDependency__('autocomplete');
});
it('should call checkArguments', () => {
@ -82,7 +74,7 @@ describe('DocSearch', () => {
new DocSearch(options);
// Then
expect(checkArguments.calledOnce).toBe(true);
expect(DocSearch.checkArguments.calledOnce).toBe(true);
});
it('should pass main options as instance properties', () => {
// Given
@ -98,7 +90,7 @@ describe('DocSearch', () => {
it('should pass the input element as an instance property', () => {
// Given
let options = defaultOptions;
getInputFromSelector.returns($('<span>foo</span>'));
DocSearch.getInputFromSelector.returns($('<span>foo</span>'));
// When
let actual = new DocSearch(options);
@ -123,7 +115,7 @@ describe('DocSearch', () => {
expect(actual.algoliaOptions).toEqual('algoliaOptions');
expect(actual.autocompleteOptions).toEqual('autocompleteOptions');
});
it('should instanciate algoliasearch with the correct values', () => {
it('should instantiate algoliasearch with the correct values', () => {
// Given
let options = defaultOptions;
@ -144,14 +136,14 @@ describe('DocSearch', () => {
// Then
expect(algoliasearch.addAlgoliaAgent.calledOnce).toBe(true);
});
it('should instanciate autocomplete.js', () => {
it('should instantiate autocomplete.js', () => {
// Given
let options = {
...defaultOptions,
autocompleteOptions: 'bar'
};
let $input = $('<input name="foo" />');
getInputFromSelector.returns($input);
DocSearch.getInputFromSelector.returns($input);
// When
new DocSearch(options);
@ -179,6 +171,12 @@ describe('DocSearch', () => {
checkArguments = DocSearch.checkArguments;
});
afterEach(() => {
if (DocSearch.getInputFromSelector.restore) {
DocSearch.getInputFromSelector.restore();
}
});
it('should throw an error if no apiKey defined', () => {
// Given
let options = {
@ -207,8 +205,7 @@ describe('DocSearch', () => {
apiKey: 'apiKey',
indexName: 'indexName'
};
let getInputFromSelector = sinon.stub().returns(false);
DocSearch.prototype.getInputFromSelector = getInputFromSelector;
sinon.stub(DocSearch, 'getInputFromSelector').returns(false);
// When
expect(() => {
@ -254,4 +251,62 @@ describe('DocSearch', () => {
expect($.zepto.isZ(actual)).toBe(true);
});
});
describe('getAutocompleteSource', () => {
let client;
let AlgoliaSearch;
let docsearch;
beforeEach(() => {
client = {
algolia: 'client',
addAlgoliaAgent: sinon.spy(),
search: sinon.stub().returns({
then: sinon.spy()
})
};
AlgoliaSearch = sinon.stub().returns(client);
DocSearch.__Rewire__('algoliasearch', AlgoliaSearch);
docsearch = new DocSearch({
indexName: 'indexName',
apiKey: 'apiKey',
inputSelector: '#input',
algoliaOptions: 'algoliaOptions'
});
});
afterEach(() => {
DocSearch.__ResetDependency__('algoliasearch');
});
it('returns a function', () => {
// Given
let actual = docsearch.getAutocompleteSource();
// When
// Then
expect(actual).toBeA('function');
});
describe('the returned function', () => {
it('calls the Agolia client with the correct parameters', () => {
// Given
let actual = docsearch.getAutocompleteSource();
// When
actual('query');
// Then
expect(client.search.calledOnce).toBe(true);
// expect(resolvedQuery.calledOnce).toBe(true);
let expectedArguments = {
indexName: 'indexName',
query: 'query',
params: 'algoliaOptions'
};
expect(client.search.calledWith([expectedArguments])).toBe(true);
});
});
});
});