Skip to content

Commit 65e5ba0

Browse files
committed
fix(counts): missing formatNumber calls
Fix #560
1 parent 3be913e commit 65e5ba0

File tree

8 files changed

+41
-9
lines changed

8 files changed

+41
-9
lines changed

lib/InstantSearch.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ Usage: instantsearch({
3939
this.searchParameters = searchParameters || {};
4040
this.widgets = [];
4141
this.templatesConfig = {
42-
helpers: {
43-
formatNumber(number, render) {
44-
return Number(render(number)).toLocaleString(numberLocale);
45-
}
46-
},
42+
helpers: require('./helpers.js')({numberLocale}),
4743
compileOptions: {}
4844
};
4945
this.urlSync = urlSync;

lib/helpers.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function({numberLocale}) {
2+
return {
3+
formatNumber(number, render) {
4+
return Number(render(number)).toLocaleString(numberLocale);
5+
}
6+
};
7+
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
header: '',
3-
item: `<a class="{{cssClasses.link}}" href="{{url}}">{{name}} <span class="{{cssClasses.count}}">{{count}}</span></a>`,
3+
item: `<a class="{{cssClasses.link}}" href="{{url}}">{{name}} <span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span></a>`,
44
footer: ''
55
};
66

widgets/menu/defaultTemplates.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
header: '',
3-
item: `<a class="{{cssClasses.link}}" href="{{url}}">{{name}} <span class="{{cssClasses.count}}">{{count}}</span></a>`,
3+
item: `<a class="{{cssClasses.link}}" href="{{url}}">{{name}} <span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span></a>`,
44
footer: ''
55
};

widgets/refinement-list/__tests__/refinement-list-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
/* eslint-env mocha */
22

3+
import React from 'react';
34
import expect from 'expect';
45
import sinon from 'sinon';
56
import jsdom from 'mocha-jsdom';
7+
import {createRenderer} from 'react-addons-test-utils';
68

79
import expectJSX from 'expect-jsx';
810
expect.extend(expectJSX);
911

1012
import refinementList from '../refinement-list';
13+
import Template from '../../../components/Template.js';
14+
15+
const helpers = require('../../../lib/helpers.js')('en-US');
1116

1217
describe('refinementList()', () => {
1318
let autoHideContainer;
@@ -16,6 +21,7 @@ describe('refinementList()', () => {
1621
let options;
1722
let widget;
1823
let ReactDOM;
24+
let renderer = createRenderer();
1925

2026
jsdom({useEach: true});
2127

@@ -181,6 +187,16 @@ describe('refinementList()', () => {
181187
createURL = sinon.spy();
182188
});
183189

190+
it('formats counts', () => {
191+
const props = {
192+
templatesConfig: {helpers},
193+
templates: require('../defaultTemplates')
194+
};
195+
renderer.render(<Template data={{count: 1000}} {...props} templateKey="item" />);
196+
let out = renderer.getRenderOutput();
197+
expect(out).toEqualJSX(<div className={undefined} dangerouslySetInnerHTML={{__html: '<label class="">\n <input type="checkbox" class="" value="" />\n <span class="">1,000</span>\n</label>'}} />);
198+
});
199+
184200
context('cssClasses', () => {
185201
it('should call the component with the correct classes', () => {
186202
// Given

widgets/refinement-list/defaultTemplates.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
header: '',
33
item: `<label class="{{cssClasses.label}}">
44
<input type="checkbox" class="{{cssClasses.checkbox}}" value="{{name}}" {{#isRefined}}checked{{/isRefined}} />{{name}}
5-
<span class="{{cssClasses.count}}">{{count}}</span>
5+
<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>
66
</label>`,
77
footer: ''
88
};

widgets/toggle/__tests__/toggle-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import React from 'react';
44
import expect from 'expect';
55
import sinon from 'sinon';
66
import jsdom from 'mocha-jsdom';
7+
import {createRenderer} from 'react-addons-test-utils';
78

89
import toggle from '../toggle';
910
import RefinementList from '../../../components/RefinementList/RefinementList.js';
11+
import Template from '../../../components/Template.js';
1012

1113
import expectJSX from 'expect-jsx';
1214
expect.extend(expectJSX);
1315

16+
const helpers = require('../../../lib/helpers.js')('en-US');
17+
1418
describe('toggle()', () => {
1519
jsdom({useEach: true});
1620

21+
let renderer = createRenderer();
22+
1723
context('bad usage', () => {
1824
it('throws when no container', () => {
1925
expect(() => {
@@ -123,6 +129,13 @@ describe('toggle()', () => {
123129
expect(ReactDOM.render.secondCall.args[1]).toEqual(container);
124130
});
125131

132+
it('formats counts', () => {
133+
templateProps.templatesConfig = {helpers};
134+
renderer.render(<Template data={{count: 1000}} {...templateProps} templateKey="item" />);
135+
let out = renderer.getRenderOutput();
136+
expect(out).toEqualJSX(<div className={undefined} dangerouslySetInnerHTML={{__html: '<label class="">\n <input type="checkbox" class="" value="" />\n <span class="">1,000</span>\n</label>'}} />);
137+
});
138+
126139
it('with facet values', () => {
127140
results = {
128141
hits: [{Hello: ', world!'}],

widgets/toggle/defaultTemplates.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
header: '',
33
item: `<label class="{{cssClasses.label}}">
44
<input type="checkbox" class="{{cssClasses.checkbox}}" value="{{name}}" {{#isRefined}}checked{{/isRefined}} />{{name}}
5-
<span class="{{cssClasses.count}}">{{count}}</span>
5+
<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>
66
</label>`,
77
footer: ''
88
};

0 commit comments

Comments
 (0)