2020-02-14 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import TestRenderer, { act } from 'react-test-renderer';
|
|
|
|
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
2020-03-05 19:54:05 +00:00
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-11 10:50:12 +00:00
|
|
|
import { useShippingRates } from '../shipping';
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
jest.mock( '@woocommerce/block-data', () => ( {
|
|
|
|
__esModule: true,
|
2020-03-05 19:54:05 +00:00
|
|
|
CART_STORE_KEY: 'test/store',
|
2020-02-14 03:43:13 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
describe( 'useShippingRates', () => {
|
|
|
|
let registry, mocks, renderer;
|
|
|
|
const getProps = ( testRenderer ) => {
|
|
|
|
const {
|
|
|
|
shippingRates,
|
2020-03-10 10:55:19 +00:00
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
2020-02-14 03:43:13 +00:00
|
|
|
shippingRatesLoading,
|
|
|
|
} = testRenderer.root.findByType( 'div' ).props;
|
|
|
|
return {
|
|
|
|
shippingRates,
|
2020-03-10 10:55:19 +00:00
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
2020-02-14 03:43:13 +00:00
|
|
|
shippingRatesLoading,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
const getWrappedComponents = ( Component ) => (
|
2020-02-14 03:43:13 +00:00
|
|
|
<RegistryProvider value={ registry }>
|
2020-03-05 19:54:05 +00:00
|
|
|
<Component />
|
2020-02-14 03:43:13 +00:00
|
|
|
</RegistryProvider>
|
|
|
|
);
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
const getTestComponent = () => () => {
|
2020-03-13 19:04:03 +00:00
|
|
|
const items = useShippingRates();
|
2020-02-14 03:43:13 +00:00
|
|
|
return <div { ...items } />;
|
|
|
|
};
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
const mockCartData = {
|
|
|
|
coupons: [],
|
|
|
|
items: [ { foo: 'bar' } ],
|
|
|
|
itemsCount: 123,
|
|
|
|
itemsWeight: 123,
|
|
|
|
needsShipping: false,
|
2020-03-13 19:04:03 +00:00
|
|
|
shippingAddress: {
|
|
|
|
country: '',
|
|
|
|
state: '',
|
|
|
|
city: '',
|
|
|
|
postcode: '',
|
|
|
|
},
|
2020-03-10 10:55:19 +00:00
|
|
|
shippingRates: [
|
|
|
|
{
|
|
|
|
shippingRates: [ { foo: 'bar' } ],
|
|
|
|
destination: {
|
|
|
|
country: '',
|
|
|
|
state: '',
|
|
|
|
city: '',
|
|
|
|
postcode: '',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2020-03-05 19:54:05 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 03:43:13 +00:00
|
|
|
const setUpMocks = () => {
|
|
|
|
mocks = {
|
|
|
|
selectors: {
|
2020-03-05 19:54:05 +00:00
|
|
|
getCartData: jest.fn().mockReturnValue( mockCartData ),
|
|
|
|
getCartErrors: jest.fn().mockReturnValue( false ),
|
|
|
|
getCartTotals: jest.fn().mockReturnValue( 123 ),
|
|
|
|
areShippingRatesLoading: jest.fn().mockReturnValue( false ),
|
2020-02-14 03:43:13 +00:00
|
|
|
hasFinishedResolution: jest.fn().mockReturnValue( true ),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
registry.registerStore( storeKey, {
|
|
|
|
reducer: () => ( {} ),
|
|
|
|
selectors: mocks.selectors,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
|
|
registry = createRegistry();
|
|
|
|
mocks = {};
|
|
|
|
renderer = null;
|
|
|
|
setUpMocks();
|
|
|
|
} );
|
2020-03-10 10:55:19 +00:00
|
|
|
it( 'should return expected address provided by the store', () => {
|
|
|
|
const TestComponent = getTestComponent();
|
|
|
|
act( () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
getWrappedComponents( TestComponent )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { shippingAddress } = getProps( renderer );
|
2020-03-13 19:04:03 +00:00
|
|
|
expect( shippingAddress ).toStrictEqual( mockCartData.shippingAddress );
|
2020-03-10 10:55:19 +00:00
|
|
|
// rerender
|
|
|
|
act( () => {
|
|
|
|
renderer.update( getWrappedComponents( TestComponent ) );
|
|
|
|
} );
|
|
|
|
// re-render should result in same shippingAddress object.
|
|
|
|
const { shippingAddress: newShippingAddress } = getProps( renderer );
|
|
|
|
expect( newShippingAddress ).toStrictEqual( shippingAddress );
|
|
|
|
renderer.unmount();
|
|
|
|
} );
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
it( 'should return expected shipping rates provided by the store', () => {
|
|
|
|
const TestComponent = getTestComponent();
|
|
|
|
act( () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
getWrappedComponents( TestComponent )
|
2020-02-14 03:43:13 +00:00
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
} );
|
2020-03-10 10:55:19 +00:00
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
const { shippingRates } = getProps( renderer );
|
2020-03-10 10:55:19 +00:00
|
|
|
expect( shippingRates ).toStrictEqual( mockCartData.shippingRates );
|
2020-03-05 19:54:05 +00:00
|
|
|
// rerender
|
|
|
|
act( () => {
|
|
|
|
renderer.update( getWrappedComponents( TestComponent ) );
|
|
|
|
} );
|
2020-03-10 10:55:19 +00:00
|
|
|
// re-render should result in same shippingAddress object.
|
2020-03-05 19:54:05 +00:00
|
|
|
const { shippingRates: newShippingRates } = getProps( renderer );
|
2020-03-10 10:55:19 +00:00
|
|
|
expect( newShippingRates ).toStrictEqual( shippingRates );
|
2020-03-05 19:54:05 +00:00
|
|
|
renderer.unmount();
|
|
|
|
} );
|
2020-02-14 03:43:13 +00:00
|
|
|
} );
|