Skip to content

Commit

Permalink
fix(hitsPerPageSelector): Be more tolerant in options
Browse files Browse the repository at this point in the history
Removes the dependency on hitsPerPage.
Follow-up of #450.
  • Loading branch information
Jerska committed Nov 5, 2015
1 parent 67406ed commit e14a344
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ describe('hitsPerPageSelector()', () => {
let helper;
let results;
let autoHideContainer;
let consoleLog;

beforeEach(() => {
autoHideContainer = sinon.stub().returns(Selector);
ReactDOM = {render: sinon.spy()};

hitsPerPageSelector.__Rewire__('ReactDOM', ReactDOM);
hitsPerPageSelector.__Rewire__('autoHideContainer', autoHideContainer);
consoleLog = sinon.spy(window.console, 'log');

container = document.createElement('div');
options = [
Expand Down Expand Up @@ -90,16 +92,16 @@ describe('hitsPerPageSelector()', () => {
it('should throw if there is no name attribute in a passed object', () => {
options.length = 0;
options.push({label: 'Label without a value'});
expect(() => {
widget.init(helper.state, helper);
}).toThrow(/No option in `options` with `value: 20`/);
widget.init(helper.state, helper);
expect(consoleLog.calledOnce).toBe(true, 'console.log called once');
expect(consoleLog.firstCall.args[0]).toMatch(/No option in `options` with `value: hitsPerPage` \(hitsPerPage: 20\)/);
});

it('must include the current hitsPerPage at initialization time', () => {
helper.state.hitsPerPage = -1;
expect(() => {
widget.init(helper.state, helper);
}).toThrow(/No option in `options` with `value: -1`/);
widget.init(helper.state, helper);
expect(consoleLog.calledOnce).toBe(true, 'console.log called once');
expect(consoleLog.firstCall.args[0]).toMatch(/No option in `options` with `value: hitsPerPage` \(hitsPerPage: -1\)/);
});

it('should not throw an error if state does not have a `hitsPerPage`', () => {
Expand All @@ -112,5 +114,6 @@ describe('hitsPerPageSelector()', () => {
afterEach(() => {
hitsPerPageSelector.__ResetDependency__('ReactDOM');
hitsPerPageSelector.__ResetDependency__('autoHideContainer');
consoleLog.restore();
});
});
8 changes: 4 additions & 4 deletions widgets/hits-per-page-selector/hits-per-page-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ function hitsPerPageSelector({
return {
init: function(state) {
let isCurrentInOptions = reduce(options, function(res, option) {
if (state.hitsPerPage === undefined) {
return true;
}
return res || +state.hitsPerPage === +option.value;
}, false);

if (!isCurrentInOptions) {
throw new Error('[hitsPerPageSelector]: No option in `options` with `value: ' + state.hitsPerPage + '`');
options = [{value: undefined, label: ''}].concat(options);
if (window.console) {
window.console.log('[Warning][hitsPerPageSelector] No option in `options` with `value: hitsPerPage` (hitsPerPage: ' + state.hitsPerPage + ')');
}
}
},

Expand Down

0 comments on commit e14a344

Please sign in to comment.