From bd63acd026327806dc4718b5bc74ce1c6f663226 Mon Sep 17 00:00:00 2001 From: Pixelastic Date: Fri, 30 Oct 2015 10:37:59 +0100 Subject: [PATCH] feat(priceRanges): Add BEM classes and tests Fixes #387 BREAKING CHANGE: `ais-price-ranges--range` are now named `ais-price-ranges--item` and are wrapped in a `ais-price-ranges--list`. I've moved the bottom form into it's own PriceRangesForm component, along with its own tests. I've fixed a minor typo where the component was internally named PriceRange (without the final __s__). I factorize some logic form the render in individual methods and manage to individually test them. This was not an easy task. I had to mock the default `render` (so it does nothing) before instanciating the component. Then, I was able to call each inner method individually. This requires to stub prototype methods in beforeEach, then restore them in afterEach. I've added a few helper methods, this can surely be simplified again but this gives nice granularity in testing. I've renamed the `range` items to `item` and wrapped them in a `list`. I've also added classes to all elements we add (`label`, `separator`, etc). I've removed the empty `span`s. --- README.md | 82 +++++- components/PriceRanges.js | 83 ------ components/PriceRanges/PriceRanges.js | 98 +++++++ components/PriceRanges/PriceRangesForm.js | 59 ++++ .../PriceRanges/__tests__/PriceRanges-test.js | 257 ++++++++++++++++++ .../__tests__/PriceRangesForm-test.js | 85 ++++++ components/Template.js | 5 +- components/__tests__/PriceRanges-test.js | 122 --------- themes/default/_price-ranges.scss | 14 +- .../__tests__/price-ranges-test.js | 19 +- widgets/price-ranges/defaultTemplates.js | 4 +- widgets/price-ranges/price-ranges.js | 96 ++++--- 12 files changed, 651 insertions(+), 273 deletions(-) delete mode 100644 components/PriceRanges.js create mode 100644 components/PriceRanges/PriceRanges.js create mode 100644 components/PriceRanges/PriceRangesForm.js create mode 100644 components/PriceRanges/__tests__/PriceRanges-test.js create mode 100644 components/PriceRanges/__tests__/PriceRangesForm-test.js delete mode 100644 components/__tests__/PriceRanges-test.js diff --git a/README.md b/README.md index c74d0423612..6a65a4e3efa 100644 --- a/README.md +++ b/README.md @@ -1047,20 +1047,27 @@ search.addWidget( * Instantiate a price ranges on a numerical facet * @param {string|DOMElement} options.container Valid CSS Selector as a string or DOMElement * @param {string} options.facetName Name of the attribute for faceting - * @param {Object} [options.cssClasses] CSS classes to add to the wrapping elements: root, range - * @param {string|string[]} [options.cssClasses.root] CSS class to add to the root element - * @param {string|string[]} [options.cssClasses.header] CSS class to add to the header element - * @param {string|string[]} [options.cssClasses.body] CSS class to add to the body element - * @param {string|string[]} [options.cssClasses.footer] CSS class to add to the footer element - * @param {string|string[]} [options.cssClasses.range] CSS class to add to the range element - * @param {string|string[]} [options.cssClasses.input] CSS class to add to the min/max input elements - * @param {string|string[]} [options.cssClasses.button] CSS class to add to the button element + * @param {Object} [options.cssClasses] CSS classes to add + * @param {string} [options.cssClasses.root] CSS class to add to the root element + * @param {string} [options.cssClasses.header] CSS class to add to the header element + * @param {string} [options.cssClasses.body] CSS class to add to the body element + * @param {string} [options.cssClasses.list] CSS class to add to the wrapping list element + * @param {string} [options.cssClasses.item] CSS class to add to each item element + * @param {string} [options.cssClasses.active] CSS class to add to the active item element + * @param {string} [options.cssClasses.link] CSS class to add to each link element + * @param {string} [options.cssClasses.form] CSS class to add to the form element + * @param {string} [options.cssClasses.label] CSS class to add to each wrapping label of the form + * @param {string} [options.cssClasses.input] CSS class to add to each input of the form + * @param {string} [options.cssClasses.currency] CSS class to add to each currency element of the form + * @param {string} [options.cssClasses.separator] CSS class to add to the separator of the form + * @param {string} [options.cssClasses.button] CSS class to add to the submit button of the form + * @param {string} [options.cssClasses.footer] CSS class to add to the footer element * @param {Object} [options.templates] Templates to use for the widget - * @param {string|Function} [options.templates.range] Range template + * @param {string|Function} [options.templates.item] Item template * @param {Object} [options.labels] Labels to use for the widget - * @param {string|Function} [options.labels.button] Button label * @param {string|Function} [options.labels.currency] Currency label - * @param {string|Function} [options.labels.to] To label + * @param {string|Function} [options.labels.separator] Separator labe, between min and max + * @param {string|Function} [options.labels.button] Button label * @param {boolean} [hideContainerWhenNoResults=true] Hide the container when no results match * @return {Object} */ @@ -1080,10 +1087,61 @@ search.addWidget( #### Styling ```html - +
+
Header
+
+
+ $3 - $13 +
+
+ $13 - $40 +
+
+ + to + + +
+
+ +
``` ```css +.ais-price-ranges { +} +.ais-price-ranges--header { +} +.ais-price-ranges--body { +} +.ais-price-ranges--list { +} +.ais-price-ranges--item { +} +.ais-price-ranges--item__active { +} +.ais-price-ranges--link { +} +.ais-price-ranges--form { +} +.ais-price-ranges--label { +} +.ais-price-ranges--currency { +} +.ais-price-ranges--input { +} +.ais-price-ranges--separator { +} +.ais-price-ranges--button { +} +.ais-price-ranges--footer { +} ``` diff --git a/components/PriceRanges.js b/components/PriceRanges.js deleted file mode 100644 index f5104ab523b..00000000000 --- a/components/PriceRanges.js +++ /dev/null @@ -1,83 +0,0 @@ -var React = require('react'); - -var Template = require('./Template'); -var cx = require('classnames'); - -class PriceRange extends React.Component { - refine(from, to, event) { - event.preventDefault(); - this.refs.from.value = this.refs.to.value = ''; - this.props.refine(from, to); - } - - _handleSubmit(e) { - this.refine(+this.refs.from.value || undefined, +this.refs.to.value || undefined, e); - } - - render() { - return ( -
- {this.props.facetValues.map(facetValue => { - var key = facetValue.from + '_' + facetValue.to; - var url; - if (this.props.createURL) { - url = this.props.createURL(facetValue.from, facetValue.to, facetValue.isRefined); - } else { - url = '#'; - } - return ( - -