Skip to content

Commit f8e0e00

Browse files
authored
fix(connectors): prefer wrappers over bind (#2575)
* fix(connectors): prefer wrappers over bind Previously we were using bind, which will be applied at each init. This is ok for one time mount, but since we can mount and unmount dynamically now, it's a problem. * chore: fix test for price-range and toggle widgets * refactor: remove unnecessary spread
1 parent 3915d4d commit f8e0e00

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/connectors/price-ranges/connectPriceRanges.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ export default function connectPriceRanges(renderFn, unmountFn) {
158158
},
159159

160160
init({ helper, instantSearchInstance }) {
161-
this._refine = this._refine.bind(this, helper);
161+
this.refine = opts => {
162+
this._refine(helper, opts);
163+
};
162164

163165
renderFn(
164166
{
165167
instantSearchInstance,
166168
items: [],
167-
refine: this._refine,
169+
refine: this.refine,
168170
widgetParams,
169171
},
170172
true
@@ -209,7 +211,7 @@ export default function connectPriceRanges(renderFn, unmountFn) {
209211
renderFn(
210212
{
211213
items: facetValues,
212-
refine: this._refine,
214+
refine: this.refine,
213215
widgetParams,
214216
instantSearchInstance,
215217
},

src/connectors/toggle/connectToggle.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export default function connectToggle(renderFn, unmountFn) {
128128
};
129129
},
130130

131-
toggleRefinement(helper, { isRefined } = {}) {
131+
_toggleRefinement(helper, { isRefined } = {}) {
132132
// Checking
133133
if (!isRefined) {
134134
if (hasAnOffValue) {
@@ -160,7 +160,9 @@ export default function connectToggle(renderFn, unmountFn) {
160160
)
161161
);
162162

163-
this.toggleRefinement = this.toggleRefinement.bind(this, helper);
163+
this.toggleRefinement = opts => {
164+
this._toggleRefinement(helper, opts);
165+
};
164166

165167
const isRefined = state.isDisjunctiveFacetRefined(attributeName, on);
166168

src/widgets/price-ranges/__tests__/price-ranges-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('priceRanges()', () => {
146146
});
147147

148148
it('refines on the lower bound', () => {
149-
widget._refine({ from: 10, to: undefined });
149+
widget.refine({ from: 10, to: undefined });
150150
expect(helper.clearRefinements.calledOnce).toBe(
151151
true,
152152
'helper.clearRefinements called once'
@@ -159,7 +159,7 @@ describe('priceRanges()', () => {
159159
});
160160

161161
it('refines on the upper bound', () => {
162-
widget._refine({ fromt: undefined, to: 10 });
162+
widget.refine({ fromt: undefined, to: 10 });
163163
expect(helper.clearRefinements.calledOnce).toBe(
164164
true,
165165
'helper.clearRefinements called once'
@@ -168,7 +168,7 @@ describe('priceRanges()', () => {
168168
});
169169

170170
it('refines on the 2 bounds', () => {
171-
widget._refine({ from: 10, to: 20 });
171+
widget.refine({ from: 10, to: 20 });
172172
expect(helper.clearRefinements.calledOnce).toBe(
173173
true,
174174
'helper.clearRefinements called once'

src/widgets/toggle/__tests__/currentToggle-test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ describe('currentToggle()', () => {
400400
let helper;
401401

402402
function toggleOn() {
403-
widget.toggleRefinement(helper, { isRefined: false });
403+
widget.toggleRefinement({ isRefined: false });
404404
}
405405
function toggleOff() {
406-
widget.toggleRefinement(helper, { isRefined: true });
406+
widget.toggleRefinement({ isRefined: true });
407407
}
408408

409409
beforeEach(() => {
@@ -424,6 +424,11 @@ describe('currentToggle()', () => {
424424
userValues,
425425
});
426426
widget.getConfiguration();
427+
const state = {
428+
isDisjunctiveFacetRefined: sinon.stub().returns(false),
429+
};
430+
const createURL = () => '#';
431+
widget.init({ state, helper, createURL, instantSearchInstance });
427432

428433
// When
429434
toggleOn();
@@ -443,6 +448,11 @@ describe('currentToggle()', () => {
443448
userValues,
444449
});
445450
widget.getConfiguration();
451+
const state = {
452+
isDisjunctiveFacetRefined: sinon.stub().returns(true),
453+
};
454+
const createURL = () => '#';
455+
widget.init({ state, helper, createURL, instantSearchInstance });
446456

447457
// When
448458
toggleOff();
@@ -468,6 +478,11 @@ describe('currentToggle()', () => {
468478
values: userValues,
469479
});
470480
widget.getConfiguration();
481+
const state = {
482+
isDisjunctiveFacetRefined: sinon.stub().returns(false),
483+
};
484+
const createURL = () => '#';
485+
widget.init({ state, helper, createURL, instantSearchInstance });
471486

472487
// When
473488
toggleOn();
@@ -494,6 +509,11 @@ describe('currentToggle()', () => {
494509
values: userValues,
495510
});
496511
widget.getConfiguration();
512+
const state = {
513+
isDisjunctiveFacetRefined: sinon.stub().returns(true),
514+
};
515+
const createURL = () => '#';
516+
widget.init({ state, helper, createURL, instantSearchInstance });
497517

498518
// When
499519
toggleOff();

0 commit comments

Comments
 (0)