From aba45fb61024bcc48cdf899ace4f67bfb66af3cf Mon Sep 17 00:00:00 2001 From: Grisha Angelov Date: Mon, 15 Jan 2024 09:42:37 +0200 Subject: [PATCH] Remove FormSpy from Shipping component due unresolved bug in react-final-form library. https://github.com/final-form/react-final-form/issues/809 Due to inconsistent behavior from react-final-form resulting in the provision of old values, the Shipping component will apply the selected shipping method on mount. The call of handleShippingMethodChange(), which was previously used by FormSpy, will be performed when we click on the given radio button. --- .../Checkout/CheckoutSteps/CheckoutSteps.js | 3 +- .../CheckoutSteps/CheckoutSteps.spec.js | 8 +++-- .../CheckoutSteps/Shipping/Shipping.js | 35 +++++++++++-------- .../CheckoutSteps/Shipping/Shipping.spec.js | 32 ++++++++--------- .../Pages/Checkout/CheckoutSteps/Step/Step.js | 6 ++-- 5 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/components/Pages/Checkout/CheckoutSteps/CheckoutSteps.js b/src/components/Pages/Checkout/CheckoutSteps/CheckoutSteps.js index b96effd..002d7b3 100644 --- a/src/components/Pages/Checkout/CheckoutSteps/CheckoutSteps.js +++ b/src/components/Pages/Checkout/CheckoutSteps/CheckoutSteps.js @@ -36,13 +36,14 @@ export class CheckoutSteps extends Component { }) } - formContent = ({ handleSubmit, valid }) => { + formContent = ({ handleSubmit, valid, values }) => { const { currentStep } = this.state const { steps } = this.props return (
{ { title: 'Title-2', component: (
content-2
), showNextButton: false, showPrevButton: true } ] + const values = { shipping_method: 'usps_fcpi' } + beforeEach(() => { clearItemsFromShoppingCart = sinon.spy() sandbox.stub(Swal, 'fire').callsFake(() => Promise.resolve()) @@ -62,10 +64,11 @@ describe('(Component) CheckoutSteps', () => { const handleSubmit = sinon.spy() expect( - wrapper.instance().formContent({ handleSubmit }) + wrapper.instance().formContent({ handleSubmit, values }) ).to.eql( { formContent.props.children.props.onChange(1) expect( - wrapper.instance().formContent({ handleSubmit }) + wrapper.instance().formContent({ handleSubmit, values }) ).to.eql( { - const { shippingMethod, price } = this.shippingMethods[shipping_method] + handleShippingMethodChange = ({ target: { value } }) => { + const { shippingMethod, price } = this.shippingMethods[value] this.props.changeShippingMethod(shippingMethod, price) } @@ -37,8 +42,9 @@ export class Shipping extends Component { component='input' type='radio' value='usps_fcpi' - />{' '} - {strings.usps_fcpi.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['usps_fcpi'].price)} + onClick={this.handleShippingMethodChange} + /> + {strings.usps_fcpi.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['usps_fcpi'].price)}
{strings.usps_fcpi.delivery}
@@ -48,8 +54,9 @@ export class Shipping extends Component { component='input' type='radio' value='usps_pmi' - />{' '} - {strings.usps_pmi.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['usps_pmi'].price)} + onClick={this.handleShippingMethodChange} + /> + {strings.usps_pmi.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['usps_pmi'].price)}
{strings.usps_pmi.delivery}
@@ -59,8 +66,9 @@ export class Shipping extends Component { component='input' type='radio' value='usps_pmei' - />{' '} - {strings.usps_pmei.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['usps_pmei'].price)} + onClick={this.handleShippingMethodChange} + /> + {strings.usps_pmei.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['usps_pmei'].price)}
{strings.usps_pmei.delivery}
@@ -70,17 +78,14 @@ export class Shipping extends Component { component='input' type='radio' value='dhl_ew' - />{' '} - {strings.dhl_ew.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['dhl_ew'].price)} + onClick={this.handleShippingMethodChange} + /> + {strings.dhl_ew.label} - {currencyFormatter(lang, strings.currency, this.shippingMethods['dhl_ew'].price)}
{strings.dhl_ew.delivery}
- ) } diff --git a/src/components/Pages/Checkout/CheckoutSteps/Shipping/Shipping.spec.js b/src/components/Pages/Checkout/CheckoutSteps/Shipping/Shipping.spec.js index d14e387..cd1c788 100644 --- a/src/components/Pages/Checkout/CheckoutSteps/Shipping/Shipping.spec.js +++ b/src/components/Pages/Checkout/CheckoutSteps/Shipping/Shipping.spec.js @@ -1,4 +1,4 @@ -import { Field, FormSpy } from 'react-final-form' +import { Field } from 'react-final-form' import StepTitle from 'components/Pages/Checkout/CheckoutSteps/StepTitle' @@ -14,6 +14,7 @@ describe('(Component) Shipping', () => { ) }) @@ -33,8 +34,9 @@ describe('(Component) Shipping', () => { component='input' type='radio' value='usps_fcpi' - />{' '} - USPS First Class Package International - £13.35 + onClick={wrapper.instance().handleShippingMethodChange} + /> + USPS First Class Package International - £13.35
7 to 21 business days
@@ -44,8 +46,9 @@ describe('(Component) Shipping', () => { component='input' type='radio' value='usps_pmi' - />{' '} - USPS Priority Mail International - £44.85 + onClick={wrapper.instance().handleShippingMethodChange} + /> + USPS Priority Mail International - £44.85
6 to 10 business days
@@ -55,8 +58,9 @@ describe('(Component) Shipping', () => { component='input' type='radio' value='usps_pmei' - />{' '} - USPS Priority Mail Express International - £58.99 + onClick={wrapper.instance().handleShippingMethodChange} + /> + USPS Priority Mail Express International - £58.99
3 to 5 business days
@@ -66,27 +70,23 @@ describe('(Component) Shipping', () => { component='input' type='radio' value='dhl_ew' - />{' '} - DHL Express Worldwide - £83.73 + onClick={wrapper.instance().handleShippingMethodChange} + /> + DHL Express Worldwide - £83.73
2 to 3 business days
- )).to.equal(true) }) it('should handle shipping method change', () => { - const formSpy = wrapper.find(FormSpy) + const field = wrapper.find(Field).at(2) - formSpy.simulate('change', { values: { shipping_method: 'usps_pmi' } }) + field.simulate('click', { target: { value: 'usps_pmi' } }) - changeShippingMethod.should.have.been.calledOnce changeShippingMethod.should.have.been.calledWith('USPS Priority Mail International', 44.85) }) }) \ No newline at end of file diff --git a/src/components/Pages/Checkout/CheckoutSteps/Step/Step.js b/src/components/Pages/Checkout/CheckoutSteps/Step/Step.js index 01bbb33..10421c4 100644 --- a/src/components/Pages/Checkout/CheckoutSteps/Step/Step.js +++ b/src/components/Pages/Checkout/CheckoutSteps/Step/Step.js @@ -1,4 +1,4 @@ -import { Component } from 'react' +import React, { Component } from 'react' import PropTypes from 'prop-types' import translate from 'translate' @@ -16,13 +16,13 @@ export class Step extends Component { } render() { - const { strings, children, disabled, showPrevButton, showNextButton } = this.props + const { strings, children, values, disabled, showPrevButton, showNextButton } = this.props return (
{ - children + React.cloneElement(children, { values }) }
{