2022-04-08 13:47:19 +00:00
|
|
|
// We need to disable the following eslint check as it's only applicable
|
|
|
|
// to testing-library/react not `react-test-renderer` used here
|
|
|
|
/* eslint-disable testing-library/await-async-query */
|
2019-09-25 14:22:36 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import TestRenderer from 'react-test-renderer';
|
2020-09-02 08:21:46 +00:00
|
|
|
import * as mockUtils from '@woocommerce/editor-components/utils';
|
2019-09-25 14:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import withProductVariations from '../with-product-variations';
|
|
|
|
import * as mockBaseUtils from '../../base/utils/errors';
|
|
|
|
|
2020-09-02 08:21:46 +00:00
|
|
|
jest.mock( '@woocommerce/editor-components/utils', () => ( {
|
2019-09-25 14:22:36 +00:00
|
|
|
getProductVariations: jest.fn(),
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
jest.mock( '../../base/utils/errors', () => ( {
|
|
|
|
formatError: jest.fn(),
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
const mockProducts = [
|
2020-06-19 14:51:09 +00:00
|
|
|
{ id: 1, name: 'Hoodie', variations: [ { id: 3 }, { id: 4 } ] },
|
2019-09-25 14:22:36 +00:00
|
|
|
{ id: 2, name: 'Backpack' },
|
|
|
|
];
|
2020-06-19 14:51:09 +00:00
|
|
|
const mockVariations = [
|
|
|
|
{ id: 3, name: 'Blue' },
|
|
|
|
{ id: 4, name: 'Red' },
|
|
|
|
];
|
2019-09-25 14:22:36 +00:00
|
|
|
const TestComponent = withProductVariations( ( props ) => {
|
|
|
|
return (
|
|
|
|
<div
|
2023-09-07 10:01:15 +00:00
|
|
|
data-error={ props.error }
|
|
|
|
data-expandedProduct={ props.expandedProduct }
|
|
|
|
data-isLoading={ props.isLoading }
|
|
|
|
data-variations={ props.variations }
|
|
|
|
data-variationsLoading={ props.variationsLoading }
|
2019-09-25 14:22:36 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
const render = () => {
|
|
|
|
return TestRenderer.create(
|
|
|
|
<TestComponent
|
|
|
|
error={ null }
|
|
|
|
isLoading={ false }
|
|
|
|
products={ mockProducts }
|
|
|
|
selected={ [ 1 ] }
|
|
|
|
showVariations={ true }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'withProductVariations Component', () => {
|
|
|
|
let renderer;
|
|
|
|
afterEach( () => {
|
|
|
|
mockUtils.getProductVariations.mockReset();
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'lifecycle events', () => {
|
|
|
|
beforeEach( () => {
|
|
|
|
mockUtils.getProductVariations.mockImplementation( () =>
|
|
|
|
Promise.resolve( mockVariations )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'getProductVariations is called on mount', () => {
|
|
|
|
renderer = render();
|
|
|
|
const { getProductVariations } = mockUtils;
|
|
|
|
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledWith( 1 );
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'getProductVariations is called on component update', () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
<TestComponent
|
|
|
|
error={ null }
|
|
|
|
isLoading={ false }
|
|
|
|
products={ mockProducts }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const { getProductVariations } = mockUtils;
|
|
|
|
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledTimes( 0 );
|
|
|
|
|
|
|
|
renderer.update(
|
|
|
|
<TestComponent
|
|
|
|
error={ null }
|
|
|
|
isLoading={ false }
|
|
|
|
products={ mockProducts }
|
|
|
|
selected={ [ 1 ] }
|
|
|
|
showVariations={ true }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledWith( 1 );
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'getProductVariations is not called if selected product has no variations', () => {
|
|
|
|
TestRenderer.create(
|
|
|
|
<TestComponent
|
|
|
|
error={ null }
|
|
|
|
isLoading={ false }
|
|
|
|
products={ mockProducts }
|
|
|
|
selected={ [ 2 ] }
|
|
|
|
showVariations={ true }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const { getProductVariations } = mockUtils;
|
|
|
|
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledTimes( 0 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'getProductVariations is called if selected product is a variation', () => {
|
|
|
|
TestRenderer.create(
|
|
|
|
<TestComponent
|
|
|
|
error={ null }
|
|
|
|
isLoading={ false }
|
|
|
|
products={ mockProducts }
|
|
|
|
selected={ [ 3 ] }
|
|
|
|
showVariations={ true }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const { getProductVariations } = mockUtils;
|
|
|
|
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledWith( 1 );
|
|
|
|
expect( getProductVariations ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'when the API returns variations data', () => {
|
|
|
|
beforeEach( () => {
|
|
|
|
mockUtils.getProductVariations.mockImplementation( () =>
|
|
|
|
Promise.resolve( mockVariations )
|
|
|
|
);
|
|
|
|
renderer = render();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'sets the variations props', () => {
|
|
|
|
const props = renderer.root.findByType( 'div' ).props;
|
|
|
|
const expectedVariations = {
|
|
|
|
1: [
|
|
|
|
{ id: 3, name: 'Blue', parent: 1 },
|
|
|
|
{ id: 4, name: 'Red', parent: 1 },
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-09-07 10:01:15 +00:00
|
|
|
expect( props[ 'data-error' ] ).toBeNull();
|
|
|
|
expect( props[ 'data-isLoading' ] ).toBe( false );
|
|
|
|
expect( props[ 'data-variations' ] ).toEqual( expectedVariations );
|
2019-09-25 14:22:36 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'when the API returns an error', () => {
|
|
|
|
const error = { message: 'There was an error.' };
|
|
|
|
const getProductVariationsPromise = Promise.reject( error );
|
|
|
|
const formattedError = { message: 'There was an error.', type: 'api' };
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
|
|
mockUtils.getProductVariations.mockImplementation(
|
|
|
|
() => getProductVariationsPromise
|
|
|
|
);
|
|
|
|
mockBaseUtils.formatError.mockImplementation(
|
|
|
|
() => formattedError
|
|
|
|
);
|
|
|
|
renderer = render();
|
|
|
|
} );
|
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
test( 'sets the error prop', async () => {
|
|
|
|
await expect( () => getProductVariationsPromise() ).toThrow();
|
2019-09-25 14:22:36 +00:00
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
const { formatError } = mockBaseUtils;
|
|
|
|
const props = renderer.root.findByType( 'div' ).props;
|
2019-09-25 14:22:36 +00:00
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
expect( formatError ).toHaveBeenCalledWith( error );
|
|
|
|
expect( formatError ).toHaveBeenCalledTimes( 1 );
|
2023-09-07 10:01:15 +00:00
|
|
|
expect( props[ 'data-error' ] ).toEqual( formattedError );
|
|
|
|
expect( props[ 'data-isLoading' ] ).toBe( false );
|
|
|
|
expect( props[ 'data-variations' ] ).toEqual( { 1: null } );
|
2019-09-25 14:22:36 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|