Skip to content

Commit 304e471

Browse files
dannyphan2000syadupathi-sf
authored andcommitted
@W-21000338: [BOPIS] Auto-populate billing address with default address (#3602)
Signed-off-by: d.phan <d.phan@salesforce.com>
1 parent 319af73 commit 304e471

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

packages/template-retail-react-app/app/pages/checkout-one-click/partials/one-click-shipping-address-selection.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,13 @@ const ShippingAddressSelection = ({
165165

166166
useEffect(() => {
167167
if (isBillingAddress) {
168-
form.reset({...selectedAddress})
169-
return
168+
// Choose selected address (if any)
169+
if (selectedAddress?.address1) {
170+
form.reset({...selectedAddress})
171+
return
172+
}
170173
}
171-
// Automatically select the customer's default/preferred shipping address
174+
// Automatically select the customer's default/preferred address as shipping/billing address
172175
if (customer.addresses) {
173176
const address = customer.addresses.find((addr) => addr.preferred === true)
174177
if (address) {
@@ -417,7 +420,6 @@ const ShippingAddressSelection = ({
417420
form={form}
418421
isBillingAddress={isBillingAddress}
419422
hidePhone={isBillingAddress}
420-
hidePreferred={true}
421423
submitButtonLabel={submitButtonLabel}
422424
formTitleAriaLabel={formTitleAriaLabel}
423425
/>

packages/template-retail-react-app/app/pages/checkout-one-click/partials/one-click-shipping-address-selection.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ jest.mock('react-intl', () => ({
4444
jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer')
4545
jest.mock('@salesforce/commerce-sdk-react')
4646

47+
// Mock AddressFields to avoid react-hook-form complexity in billing address tests
48+
jest.mock('@salesforce/retail-react-app/app/components/forms/address-fields', () => {
49+
return function MockAddressFields() {
50+
return <div data-testid="mock-address-fields">Address Fields</div>
51+
}
52+
})
53+
4754
const mockCustomer = {
4855
addresses: []
4956
}
@@ -101,6 +108,86 @@ describe('ShippingAddressSelection Component', () => {
101108

102109
expect(screen.queryByText('Submit')).not.toBeInTheDocument()
103110
})
111+
112+
test('auto-populates form with selectedAddress when isBillingAddress is true', () => {
113+
const mockForm = {
114+
handleSubmit: jest.fn(() => (e) => e?.preventDefault?.()),
115+
reset: jest.fn(),
116+
watch: jest.fn(),
117+
register: jest.fn(),
118+
control: {},
119+
trigger: jest.fn(),
120+
formState: {isSubmitting: false, errors: {}}
121+
}
122+
const selectedAddress = {
123+
address1: '456 Billing St',
124+
city: 'Billing City',
125+
stateCode: 'CA',
126+
postalCode: '90210',
127+
countryCode: 'US',
128+
firstName: 'Jane',
129+
lastName: 'Doe'
130+
}
131+
132+
useCurrentCustomer.mockReturnValue({
133+
data: {
134+
customerId: 'test-customer-id',
135+
isRegistered: true,
136+
addresses: []
137+
},
138+
isLoading: false,
139+
isFetching: false
140+
})
141+
142+
render(
143+
<ShippingAddressSelection
144+
form={mockForm}
145+
isBillingAddress={true}
146+
selectedAddress={selectedAddress}
147+
/>
148+
)
149+
150+
// Verify the form was reset with the selected billing address
151+
expect(mockForm.reset).toHaveBeenCalledWith(selectedAddress)
152+
})
153+
154+
test('auto-populates form with preferred address when no selectedAddress provided', () => {
155+
const mockForm = {
156+
handleSubmit: jest.fn(() => (e) => e?.preventDefault?.()),
157+
reset: jest.fn(),
158+
watch: jest.fn(),
159+
register: jest.fn(),
160+
control: {},
161+
trigger: jest.fn(),
162+
formState: {isSubmitting: false, errors: {}}
163+
}
164+
const preferredAddress = {
165+
addressId: 'addr-preferred',
166+
address1: '789 Preferred St',
167+
city: 'Preferred City',
168+
stateCode: 'NY',
169+
postalCode: '10001',
170+
countryCode: 'US',
171+
firstName: 'John',
172+
lastName: 'Smith',
173+
preferred: true
174+
}
175+
176+
useCurrentCustomer.mockReturnValue({
177+
data: {
178+
customerId: 'test-customer-id',
179+
isRegistered: true,
180+
addresses: [preferredAddress]
181+
},
182+
isLoading: false,
183+
isFetching: false
184+
})
185+
186+
render(<ShippingAddressSelection form={mockForm} isBillingAddress={true} />)
187+
188+
// Verify the form was reset with the preferred address
189+
expect(mockForm.reset).toHaveBeenCalledWith(preferredAddress)
190+
})
104191
})
105192

106193
describe('Edge Cases', () => {

0 commit comments

Comments
 (0)