|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint max-nested-callbacks: 0 */ |
| 7 | +define([ |
| 8 | + 'squire', |
| 9 | + 'ko' |
| 10 | +], function (Squire, ko) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + var injector = new Squire(), |
| 14 | + checkoutData = jasmine.createSpyObj( |
| 15 | + 'checkoutData', |
| 16 | + ['setSelectedBillingAddress', 'setNewCustomerBillingAddress'] |
| 17 | + ), |
| 18 | + createBillingAddress = jasmine.createSpy('createBillingAddress').and.callFake(function () { |
| 19 | + return { |
| 20 | + getKey: function () { |
| 21 | + return 'new-billing-address-key'; |
| 22 | + } |
| 23 | + }; |
| 24 | + }), |
| 25 | + selectBillingAddress = jasmine.createSpy('selectBillingAddress'), |
| 26 | + mocks = { |
| 27 | + 'Magento_Checkout/js/checkout-data': checkoutData, |
| 28 | + 'Magento_Checkout/js/action/create-billing-address': createBillingAddress, |
| 29 | + 'Magento_Checkout/js/action/select-billing-address': selectBillingAddress, |
| 30 | + 'Magento_Customer/js/model/customer': { |
| 31 | + isLoggedIn: function () { |
| 32 | + return true; |
| 33 | + } |
| 34 | + }, |
| 35 | + 'Magento_Checkout/js/model/quote': { |
| 36 | + billingAddress: ko.observable(null), |
| 37 | + shippingAddress: ko.observable(null), |
| 38 | + isVirtual: function () { |
| 39 | + return false; |
| 40 | + }, |
| 41 | + paymentMethod: ko.observable(null) |
| 42 | + }, |
| 43 | + 'Magento_Customer/js/customer-data': { |
| 44 | + get: function () { |
| 45 | + return function () { |
| 46 | + return {}; |
| 47 | + }; |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + lastSelectedBillingAddress = { |
| 52 | + city: 'Culver City', |
| 53 | + company: 'Magento', |
| 54 | + country_id: 'US', |
| 55 | + firstname: 'John', |
| 56 | + lastname: '', |
| 57 | + postcode: '90230', |
| 58 | + region: '', |
| 59 | + region_id: '12', |
| 60 | + street: { |
| 61 | + 0: '6161 West Centinela Avenue', |
| 62 | + 1: '' |
| 63 | + }, |
| 64 | + telephone: '+15555555555' |
| 65 | + }, |
| 66 | + billingAddress; |
| 67 | + |
| 68 | + beforeEach(function (done) { |
| 69 | + window.checkoutConfig = { |
| 70 | + quoteData: {}, |
| 71 | + storeCode: 'US', |
| 72 | + reloadOnBillingAddress: false, |
| 73 | + displayBillingOnPaymentMethod: true |
| 74 | + }; |
| 75 | + |
| 76 | + spyOn(mocks['Magento_Checkout/js/model/quote'], 'billingAddress').and.returnValue(lastSelectedBillingAddress); |
| 77 | + |
| 78 | + injector.mock(mocks); |
| 79 | + injector.require(['Magento_Checkout/js/view/billing-address'], function (Constr) { |
| 80 | + billingAddress = new Constr; |
| 81 | + |
| 82 | + billingAddress.source = { |
| 83 | + get: jasmine.createSpy('get').and.callFake(function (key) { |
| 84 | + if (key === billingAddress.dataScopePrefix + '.custom_attributes') { |
| 85 | + return true; |
| 86 | + } else if (key === 'params.invalid') { |
| 87 | + return true; // Simulate valid form data |
| 88 | + } else if (key === billingAddress.dataScopePrefix) { |
| 89 | + return lastSelectedBillingAddress; // Return mock address data |
| 90 | + } |
| 91 | + return null; |
| 92 | + }), |
| 93 | + set: jasmine.createSpy('set'), |
| 94 | + trigger: jasmine.createSpy('trigger').and.callFake(function (event) { |
| 95 | + if ( |
| 96 | + event === billingAddress.dataScopePrefix + '.data.validate' |
| 97 | + || event === billingAddress.dataScopePrefix + '.custom_attributes.data.validate' |
| 98 | + ) { |
| 99 | + billingAddress.source.set('params.invalid', false); // Simulate valid form data |
| 100 | + } |
| 101 | + }) |
| 102 | + }; |
| 103 | + |
| 104 | + done(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('Magento_Checkout/js/view/billing-address', function () { |
| 109 | + describe('"updateAddress" method', function () { |
| 110 | + it('should not call updateAddresses when form is invalid', function () { |
| 111 | + billingAddress.source.set.and.callFake(function (key, value) { |
| 112 | + if (key === 'params.invalid' && value === true) { |
| 113 | + billingAddress.source.get.and.callFake(function () { |
| 114 | + if (key === 'params.invalid') { |
| 115 | + return true; // Simulate invalid form data |
| 116 | + } |
| 117 | + return null; |
| 118 | + }); |
| 119 | + } |
| 120 | + }); |
| 121 | + spyOn(billingAddress, 'updateAddresses'); |
| 122 | + billingAddress.updateAddress(); |
| 123 | + expect(billingAddress.updateAddresses).not.toHaveBeenCalled(); |
| 124 | + expect(selectBillingAddress).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments