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-08-22 11:36:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import TestRenderer from 'react-test-renderer';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import withReviews from '../with-reviews';
|
|
|
|
import * as mockUtils from '../../../blocks/reviews/utils';
|
2019-09-04 16:07:00 +00:00
|
|
|
import * as mockBaseUtils from '../../utils/errors';
|
2019-08-22 11:36:20 +00:00
|
|
|
|
|
|
|
jest.mock( '../../../blocks/reviews/utils', () => ( {
|
2019-10-28 13:53:09 +00:00
|
|
|
getSortArgs: () => ( {
|
2019-08-22 11:36:20 +00:00
|
|
|
order: 'desc',
|
|
|
|
orderby: 'date_gmt',
|
|
|
|
} ),
|
|
|
|
getReviews: jest.fn(),
|
|
|
|
} ) );
|
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
jest.mock( '../../utils/errors', () => ( {
|
|
|
|
formatError: jest.fn(),
|
|
|
|
} ) );
|
|
|
|
|
2019-08-22 11:36:20 +00:00
|
|
|
const mockReviews = [
|
|
|
|
{ reviewer: 'Alice', review: 'Lorem ipsum', rating: 2 },
|
|
|
|
{ reviewer: 'Bob', review: 'Dolor sit amet', rating: 3 },
|
|
|
|
{ reviewer: 'Carol', review: 'Consectetur adipiscing elit', rating: 5 },
|
|
|
|
];
|
|
|
|
const defaultArgs = {
|
|
|
|
offset: 0,
|
|
|
|
order: 'desc',
|
|
|
|
orderby: 'date_gmt',
|
|
|
|
per_page: 2,
|
|
|
|
product_id: 1,
|
|
|
|
};
|
|
|
|
const TestComponent = withReviews( ( props ) => {
|
2019-09-05 15:09:31 +00:00
|
|
|
return (
|
|
|
|
<div
|
2023-09-07 10:01:15 +00:00
|
|
|
data-error={ props.error }
|
|
|
|
data-getReviews={ props.getReviews }
|
|
|
|
data-appendReviews={ props.appendReviews }
|
|
|
|
data-onChangeArgs={ props.onChangeArgs }
|
|
|
|
data-isLoading={ props.isLoading }
|
|
|
|
data-reviews={ props.reviews }
|
|
|
|
data-totalReviews={ props.totalReviews }
|
2019-09-05 15:09:31 +00:00
|
|
|
/>
|
|
|
|
);
|
2019-08-22 11:36:20 +00:00
|
|
|
} );
|
|
|
|
const render = () => {
|
|
|
|
return TestRenderer.create(
|
|
|
|
<TestComponent
|
2019-10-22 14:13:14 +00:00
|
|
|
attributes={ {} }
|
2019-08-22 11:36:20 +00:00
|
|
|
order="desc"
|
|
|
|
orderby="date_gmt"
|
|
|
|
productId={ 1 }
|
|
|
|
reviewsToDisplay={ 2 }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'withReviews Component', () => {
|
|
|
|
let renderer;
|
|
|
|
afterEach( () => {
|
|
|
|
mockUtils.getReviews.mockReset();
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'lifecycle events', () => {
|
|
|
|
beforeEach( () => {
|
2019-09-05 15:09:31 +00:00
|
|
|
mockUtils.getReviews
|
|
|
|
.mockImplementationOnce( () =>
|
|
|
|
Promise.resolve( {
|
|
|
|
reviews: mockReviews.slice( 0, 2 ),
|
|
|
|
totalReviews: mockReviews.length,
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
.mockImplementationOnce( () =>
|
|
|
|
Promise.resolve( {
|
|
|
|
reviews: mockReviews.slice( 2, 3 ),
|
|
|
|
totalReviews: mockReviews.length,
|
|
|
|
} )
|
|
|
|
);
|
2019-08-22 11:36:20 +00:00
|
|
|
renderer = render();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'getReviews is called on mount with default args', () => {
|
|
|
|
const { getReviews } = mockUtils;
|
|
|
|
|
|
|
|
expect( getReviews ).toHaveBeenCalledWith( defaultArgs );
|
|
|
|
expect( getReviews ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'getReviews is called on component update', () => {
|
|
|
|
const { getReviews } = mockUtils;
|
|
|
|
renderer.update(
|
|
|
|
<TestComponent
|
|
|
|
order="desc"
|
|
|
|
orderby="date_gmt"
|
|
|
|
productId={ 1 }
|
|
|
|
reviewsToDisplay={ 3 }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
expect( getReviews ).toHaveBeenNthCalledWith( 2, {
|
|
|
|
...defaultArgs,
|
|
|
|
offset: 2,
|
|
|
|
per_page: 1,
|
|
|
|
} );
|
2019-08-22 11:36:20 +00:00
|
|
|
expect( getReviews ).toHaveBeenCalledTimes( 2 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'when the API returns product data', () => {
|
|
|
|
beforeEach( () => {
|
2019-09-05 15:09:31 +00:00
|
|
|
mockUtils.getReviews.mockImplementation( () =>
|
|
|
|
Promise.resolve( {
|
|
|
|
reviews: mockReviews.slice( 0, 2 ),
|
|
|
|
totalReviews: mockReviews.length,
|
|
|
|
} )
|
2019-08-22 11:36:20 +00:00
|
|
|
);
|
|
|
|
renderer = render();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'sets reviews based on API response', () => {
|
|
|
|
const props = renderer.root.findByType( 'div' ).props;
|
|
|
|
|
2023-09-07 10:01:15 +00:00
|
|
|
expect( props[ 'data-error' ] ).toBeNull();
|
|
|
|
expect( props[ 'data-isLoading' ] ).toBe( false );
|
|
|
|
expect( props[ 'data-reviews' ] ).toEqual(
|
|
|
|
mockReviews.slice( 0, 2 )
|
|
|
|
);
|
|
|
|
expect( props[ 'data-totalReviews' ] ).toEqual(
|
|
|
|
mockReviews.length
|
|
|
|
);
|
2019-08-22 11:36:20 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'when the API returns an error', () => {
|
2019-09-04 16:07:00 +00:00
|
|
|
const error = { message: 'There was an error.' };
|
|
|
|
const getReviewsPromise = Promise.reject( error );
|
|
|
|
const formattedError = { message: 'There was an error.', type: 'api' };
|
|
|
|
|
2019-08-22 11:36:20 +00:00
|
|
|
beforeEach( () => {
|
2019-09-05 15:09:31 +00:00
|
|
|
mockUtils.getReviews.mockImplementation( () => getReviewsPromise );
|
2019-09-09 10:52:48 +00:00
|
|
|
mockBaseUtils.formatError.mockImplementation(
|
|
|
|
() => formattedError
|
|
|
|
);
|
2019-08-22 11:36:20 +00:00
|
|
|
renderer = render();
|
|
|
|
} );
|
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
test( 'sets the error prop', async () => {
|
|
|
|
await expect( () => getReviewsPromise() ).toThrow();
|
2019-08-22 11:36:20 +00:00
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
const { formatError } = mockBaseUtils;
|
|
|
|
const props = renderer.root.findByType( 'div' ).props;
|
2019-09-04 16:07:00 +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-reviews' ] ).toEqual( [] );
|
2019-08-22 11:36:20 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|