@@ -44,6 +44,13 @@ jest.mock('react-intl', () => ({
4444jest . mock ( '@salesforce/retail-react-app/app/hooks/use-current-customer' )
4545jest . 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+
4754const 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