woocommerce/plugins/woocommerce-blocks/assets/js/data/cart/test/push-changes.ts

195 lines
5.5 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import * as wpDataFunctions from '@wordpress/data';
import { CART_STORE_KEY, VALIDATION_STORE_KEY } from '@woocommerce/block-data';
/**
* Internal dependencies
*/
import { pushChanges } from '../push-changes';
// When first updating the customer data, we want to simulate a rejected update.
const updateCustomerDataMock = jest.fn().mockRejectedValue( 'error' );
const getCustomerDataMock = jest.fn().mockReturnValue( {
billingAddress: {
first_name: 'John',
last_name: 'Doe',
address_1: '123 Main St',
address_2: '',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
email: 'john.doe@mail.com',
phone: '555-555-5555',
},
shippingAddress: {
first_name: 'John',
last_name: 'Doe',
address_1: '123 Main St',
address_2: '',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
phone: '555-555-5555',
},
} );
// Mocking select and dispatch here so we can control the actions/selectors used in pushChanges.
jest.mock( '@wordpress/data', () => ( {
...jest.requireActual( '@wordpress/data' ),
__esModule: true,
select: jest.fn(),
dispatch: jest.fn(),
} ) );
2023-04-28 10:29:45 +00:00
// Mocking the debounce method so we can use the callback directly without waiting for debounce.
jest.mock( '@woocommerce/base-utils', () => ( {
...jest.requireActual( '@woocommerce/base-utils' ),
__esModule: true,
debounce: jest.fn( ( callback ) => callback ),
} ) );
// Mocking processErrorResponse because we don't actually care about processing the error response, we just don't want
// pushChanges to throw an error.
jest.mock( '../utils', () => ( {
...jest.requireActual( '../utils' ),
__esModule: true,
processErrorResponse: jest.fn(),
} ) );
// Mocking updatePaymentMethods because this uses the mocked debounce earlier, and causes an error. Moreover, we don't
// need to update payment methods, they are not relevant to the tests in this file.
jest.mock( '../update-payment-methods', () => ( {
debouncedUpdatePaymentMethods: jest.fn(),
Remove WC Core shipping settings if Cart/Checkout blocks are in use (https://github.com/woocommerce/woocommerce-blocks/pull/8679) * Add CartCheckoutUtils class This class will store reusable methods relating to Cart/Checkout Blocks, i.e. whether they are used on the Cart/Checkout page. * Update ShippingController to use the new CartCheckoutUtils function This will reduce code duplication when checking if the Cart/Checkout blocks are in use on the Cart/Checkout page. * Add filter to remove shipping settings when Cart/Checkout are default * Ensure setting displays correctly if cart is default but not checkout * Add tests to ensure core shipping settings update correctly * Add setCartCheckoutPages function to update set the cart/checkout page * Force shipping to be enabled if the Checkout block is in use. * Add filter to override cost requires address option * Add shippingCostRequiresAddress option * Check if the address is required before showing rates * Show shipping rates in editor * Add shippingCostRequiresAddress attribute to shipping methods block * Update frontend type to show shippingCostRequiresAddress is a prop * Add control to toggle shippingCostRequiresAddress option * Show address notice in the correct scenario * Send shippingCostRequiresAddress to Block in front end context * Add e2e test for editor control * Add e2e tests for shipping options on the front end * Add updateAttributeInSiblingBlock function * Add shippingCostRequiresAddress to shipping method block * Ensure attribute is updated in both blocks when editing * In Shipping Methods Block, show correct component based on block setting * Show correct block in editor * Remove broken test from PR * Clean up updateAttributeInSiblingBlock * Add setCartCheckoutPages function to update set the cart/checkout page * Add tests to ensure core shipping settings update correctly * Add isAddressComplete function Borrowed from woocommerce/woocommerce-blocks#8141 * Check if the address is required before showing rates * Show shipping rates in editor * Show address notice in the correct scenario * Add e2e tests for shipping options on the front end * Ensure errorId is passed to StateInput * Add fullShippingAddressPushed action to wc/store/cart * Add fullShippingAddressPushed case to reducer * Ensure fullShippingAddressPushed is set when initialising cart store * Add fullShippingAddressPushed selector and default state entry * Add shippingAddressHasValidationErrors util function * Do not overwrite addresses when selecting a rate * Set whether full address has been pushed when saving address changes * In Shipping Methods Block, show correct component based on block setting * Don't show from price if rates should be hidden until address entered * Check city validation errors to assert if shipping address is valid * Rename merchant.js to merchant.ts * Move local pickup functions to common merchant util * Update local pickup tests to use common merchant utils * Add test to ensure setting toggles in both blocks * Add navigating to settings and saving in merchant util * Create addPickupLocation merchant util * Add test for local pickup and require full address * Make sure correct conditions are met to show shipping options * Ensure checkbox is checked during local pickup tests * Unset the checkbox when tests are finished running * Update checkout block fixture * Prevent error in unit tests * Import validation store key from constants Required because importing from the index causes the validation data store to register twice * Update checkout terms test to wait for button not to be disabled * Revert "Add isAddressComplete function" This reverts commit 9967dc0d4f10cf638859ae085e6f4cc2901dd299.
2023-03-13 11:49:28 +00:00
updatePaymentMethods: jest.fn(),
} ) );
describe( 'pushChanges', () => {
beforeEach( () => {
wpDataFunctions.select.mockImplementation( ( storeName: string ) => {
if ( storeName === CART_STORE_KEY ) {
return {
...jest
.requireActual( '@wordpress/data' )
.select( storeName ),
hasFinishedResolution: () => true,
getCustomerData: getCustomerDataMock,
};
}
if ( storeName === VALIDATION_STORE_KEY ) {
return {
...jest
.requireActual( '@wordpress/data' )
.select( storeName ),
getValidationError: () => undefined,
};
}
return jest.requireActual( '@wordpress/data' ).select( storeName );
} );
wpDataFunctions.dispatch.mockImplementation( ( storeName: string ) => {
if ( storeName === CART_STORE_KEY ) {
return {
...jest
.requireActual( '@wordpress/data' )
.dispatch( storeName ),
updateCustomerData: updateCustomerDataMock,
};
}
return jest
.requireActual( '@wordpress/data' )
.dispatch( storeName );
} );
} );
it( 'Keeps props dirty if data did not persist due to an error', async () => {
// Run this without changing anything because the first run does not push data (the first run is populating what was received on page load).
pushChanges();
// Mock the returned value of `getCustomerData` to simulate a change in the shipping address.
getCustomerDataMock.mockReturnValue( {
billingAddress: {
first_name: 'John',
last_name: 'Doe',
address_1: '123 Main St',
address_2: '',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
email: 'john.doe@mail.com',
phone: '555-555-5555',
},
shippingAddress: {
first_name: 'John',
last_name: 'Doe',
address_1: '123 Main St',
address_2: '',
city: 'Houston',
state: 'TX',
postcode: 'ABCDEF',
country: 'US',
phone: '555-555-5555',
},
} );
// Push these changes to the server, the `updateCustomerData` mock is set to reject (in the original mock at the top of the file), to simulate a server error.
pushChanges();
// Check that the mock was called with only the updated data.
await expect( updateCustomerDataMock ).toHaveBeenCalledWith( {
shipping_address: {
city: 'Houston',
state: 'TX',
postcode: 'ABCDEF',
},
} );
// This assertion is required to ensure the async `catch` block in `pushChanges` is done executing and all side effects finish.
await expect( updateCustomerDataMock ).toHaveReturned();
// Reset the mock so that it no longer rejects.
updateCustomerDataMock.mockReset();
updateCustomerDataMock.mockResolvedValue( jest.fn() );
// Simulate the user updating the postcode only.
getCustomerDataMock.mockReturnValue( {
billingAddress: {
first_name: 'John',
last_name: 'Doe',
address_1: '123 Main St',
address_2: '',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
email: 'john.doe@mail.com',
phone: '555-555-5555',
},
shippingAddress: {
first_name: 'John',
last_name: 'Doe',
address_1: '123 Main St',
address_2: '',
city: 'Houston',
state: 'TX',
postcode: '77058',
country: 'US',
phone: '555-555-5555',
},
} );
// Although only one property was updated between calls, we should expect City, State, and Postcode to be pushed
// to the server because the previous push failed when they were originally changed.
pushChanges();
await expect( updateCustomerDataMock ).toHaveBeenLastCalledWith( {
shipping_address: {
city: 'Houston',
state: 'TX',
postcode: '77058',
},
} );
} );
} );