-
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.
feat(configure): add the Configure widget (#2698)
* feat(configure): add the configure connector * chore: use jsHelper.make in tests * chore: format * refactor: add configure widget instead * test: use workaround method instead * refactor: remove connectConfigure * chore: fix imports * chore: use searchParameters key * chore: use searchParameters directly after all * chore(doc): support for unnnamed parameters in widgets * chore(doc): support for externals * chore: incorporate feedback
- Loading branch information
Showing
8 changed files
with
224 additions
and
18 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
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,85 @@ | ||
import { SearchParameters } from 'algoliasearch-helper'; | ||
import configure from '../configure'; | ||
|
||
describe('configure', () => { | ||
it('throws when you pass it a non-plain object', () => { | ||
[ | ||
() => configure(new Date()), | ||
() => configure(() => {}), | ||
() => configure(/ok/), | ||
].forEach(widget => expect(widget).toThrowError(/Usage/)); | ||
}); | ||
|
||
it('Applies searchParameters if nothing in configuration yet', () => { | ||
const widget = configure({ analytics: true }); | ||
const config = widget.getConfiguration(SearchParameters.make({})); | ||
expect(config).toEqual({ | ||
analytics: true, | ||
}); | ||
}); | ||
|
||
it('Applies searchParameters if nothing conflicting configuration', () => { | ||
const widget = configure({ analytics: true }); | ||
const config = widget.getConfiguration( | ||
SearchParameters.make({ query: 'testing' }) | ||
); | ||
expect(config).toEqual({ | ||
analytics: true, | ||
}); | ||
}); | ||
|
||
it('Applies searchParameters with a higher priority', () => { | ||
const widget = configure({ analytics: true }); | ||
{ | ||
const config = widget.getConfiguration( | ||
SearchParameters.make({ analytics: false }) | ||
); | ||
expect(config).toEqual({ | ||
analytics: true, | ||
}); | ||
} | ||
{ | ||
const config = widget.getConfiguration( | ||
SearchParameters.make({ analytics: false, extra: true }) | ||
); | ||
expect(config).toEqual({ | ||
analytics: true, | ||
}); | ||
} | ||
}); | ||
|
||
it('disposes all of the state set by configure', () => { | ||
const widget = configure({ analytics: true }); | ||
|
||
const nextState = widget.dispose({ | ||
state: SearchParameters.make({ | ||
analytics: true, | ||
somethingElse: false, | ||
}), | ||
}); | ||
|
||
expect(nextState).toEqual( | ||
SearchParameters.make({ | ||
somethingElse: false, | ||
}) | ||
); | ||
}); | ||
|
||
it('disposes all of the state set by configure in case of a conflict', () => { | ||
const widget = configure({ analytics: true }); | ||
|
||
const nextState = widget.dispose({ | ||
state: SearchParameters.make({ | ||
// even though it's different, it will be deleted | ||
analytics: false, | ||
somethingElse: false, | ||
}), | ||
}); | ||
|
||
expect(nextState).toEqual( | ||
SearchParameters.make({ | ||
somethingElse: false, | ||
}) | ||
); | ||
}); | ||
}); |
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,51 @@ | ||
import isPlainObject from 'lodash/isPlainObject'; | ||
const usage = `Usage: | ||
search.addWidget( | ||
instantsearch.widgets.configure({ | ||
// any searchParameter | ||
}) | ||
); | ||
Full documentation available at https://community.algolia.com/instantsearch.js/v2/widgets/configure.html | ||
`; | ||
|
||
/** | ||
* The **configure** widget is a headless widget that let you configure the | ||
* settings of your search using the parameters described by the | ||
* [general Algolia documentation](https://www.algolia.com/doc/api-reference/search-api-parameters/) | ||
* | ||
* This widget has no visible UI, so you should only use it for search parameters | ||
* users shouldn't expect to change. | ||
* | ||
* @type {WidgetFactory} | ||
* @category filter | ||
* @param {SearchParameters} searchParameters The Configure widget options are search parameters | ||
* @returns {Object} A new Configure widget instance. | ||
* @example | ||
* search.addWidget( | ||
* instantsearch.widgets.configure({ | ||
* analytics: true, | ||
* ruleContexts: ['desktop', 'cool-users'], | ||
* distinct: 3, | ||
* }) | ||
* ); | ||
*/ | ||
export default function configure(searchParameters = {}) { | ||
if (!isPlainObject(searchParameters)) { | ||
throw new Error(usage); | ||
} | ||
return { | ||
getConfiguration() { | ||
return searchParameters; | ||
}, | ||
init() {}, | ||
dispose({ state }) { | ||
return state.mutateMe(mutableState => { | ||
// widgetParams are assumed 'controlled', | ||
// so they override whatever other widgets give the state | ||
Object.keys(searchParameters).forEach(key => { | ||
delete mutableState[key]; | ||
}); | ||
}); | ||
}, | ||
}; | ||
} |
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