2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*
|
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
import TestRenderer from 'react-test-renderer';
|
2018-12-31 06:42:46 +00:00
|
|
|
import { map, noop } from 'lodash';
|
2018-08-29 15:55:56 +00:00
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
2019-01-29 16:48:46 +00:00
|
|
|
import { numberFormat } from '@woocommerce/number';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-12-31 06:42:46 +00:00
|
|
|
import LeaderboardWithSelect, { Leaderboard } from '../';
|
2019-01-30 07:22:10 +00:00
|
|
|
import { NAMESPACE } from 'wc-api/constants';
|
2018-10-15 17:06:37 +00:00
|
|
|
import mockData from '../__mocks__/top-selling-products-mock-data';
|
2018-08-29 15:55:56 +00:00
|
|
|
|
|
|
|
// Mock <Table> to avoid tests failing due to it using DOM properties that
|
|
|
|
// are not available on TestRenderer.
|
|
|
|
jest.mock( '@woocommerce/components', () => ( {
|
2019-03-26 11:31:57 +00:00
|
|
|
...jest.requireActual( '../../../../../packages/components' ),
|
2018-10-29 07:57:05 +00:00
|
|
|
TableCard: () => null,
|
2018-08-29 15:55:56 +00:00
|
|
|
} ) );
|
|
|
|
|
2018-12-31 06:42:46 +00:00
|
|
|
const getRowsContent = data => {
|
|
|
|
return map( data, row => {
|
|
|
|
const { name, items_sold, net_revenue, orders_count } = row;
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: name,
|
|
|
|
value: name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( items_sold ),
|
|
|
|
value: items_sold,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( orders_count ),
|
|
|
|
value: orders_count,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( net_revenue ),
|
|
|
|
value: getCurrencyFormatDecimal( net_revenue ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'Leaderboard', () => {
|
2018-09-12 12:09:53 +00:00
|
|
|
test( 'should render empty message when there are no rows', () => {
|
2018-12-31 06:42:46 +00:00
|
|
|
const leaderboard = shallow(
|
|
|
|
<Leaderboard title={ '' } getHeadersContent={ noop } getRowsContent={ getRowsContent } />
|
|
|
|
);
|
2018-09-12 12:09:53 +00:00
|
|
|
|
2018-12-31 06:42:46 +00:00
|
|
|
expect( leaderboard.find( 'EmptyTable' ).length ).toBe( 1 );
|
2018-09-12 12:09:53 +00:00
|
|
|
} );
|
|
|
|
|
2018-08-29 15:55:56 +00:00
|
|
|
test( 'should render correct data in the table', () => {
|
2018-12-31 06:42:46 +00:00
|
|
|
const leaderboard = shallow(
|
|
|
|
<Leaderboard
|
|
|
|
title={ '' }
|
|
|
|
getHeadersContent={ noop }
|
|
|
|
getRowsContent={ getRowsContent }
|
|
|
|
items={ { data: { ...mockData } } }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const table = leaderboard.find( 'TableCard' );
|
2018-08-29 15:55:56 +00:00
|
|
|
const firstRow = table.props().rows[ 0 ];
|
|
|
|
|
2018-09-14 15:21:21 +00:00
|
|
|
expect( firstRow[ 0 ].value ).toBe( mockData[ 0 ].name );
|
2018-08-29 15:55:56 +00:00
|
|
|
expect( firstRow[ 1 ].display ).toBe( numberFormat( mockData[ 0 ].items_sold ) );
|
|
|
|
expect( firstRow[ 1 ].value ).toBe( mockData[ 0 ].items_sold );
|
|
|
|
expect( firstRow[ 2 ].display ).toBe( numberFormat( mockData[ 0 ].orders_count ) );
|
|
|
|
expect( firstRow[ 2 ].value ).toBe( mockData[ 0 ].orders_count );
|
2018-12-19 00:56:27 +00:00
|
|
|
expect( firstRow[ 3 ].display ).toBe( formatCurrency( mockData[ 0 ].net_revenue ) );
|
|
|
|
expect( firstRow[ 3 ].value ).toBe( getCurrencyFormatDecimal( mockData[ 0 ].net_revenue ) );
|
2018-08-29 15:55:56 +00:00
|
|
|
} );
|
|
|
|
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo Since this now uses fresh-data / wc-api, the API testing needs to be revisted.
|
2018-12-31 06:42:46 +00:00
|
|
|
xtest( 'should load report stats from API', () => {
|
2018-09-04 14:10:07 +00:00
|
|
|
const getReportStatsMock = jest.fn().mockReturnValue( { data: mockData } );
|
2018-08-29 15:55:56 +00:00
|
|
|
const isReportStatsRequestingMock = jest.fn().mockReturnValue( false );
|
2018-12-18 19:32:25 +00:00
|
|
|
const isReportStatsErrorMock = jest.fn().mockReturnValue( false );
|
2018-08-29 15:55:56 +00:00
|
|
|
const registry = createRegistry();
|
2018-12-31 06:42:46 +00:00
|
|
|
registry.registerStore( 'wc-api', {
|
2018-08-29 15:55:56 +00:00
|
|
|
reducer: () => {},
|
|
|
|
selectors: {
|
|
|
|
getReportStats: getReportStatsMock,
|
|
|
|
isReportStatsRequesting: isReportStatsRequestingMock,
|
2018-12-18 19:32:25 +00:00
|
|
|
isReportStatsError: isReportStatsErrorMock,
|
2018-08-29 15:55:56 +00:00
|
|
|
},
|
|
|
|
} );
|
2018-12-31 06:42:46 +00:00
|
|
|
const leaderboardWrapper = TestRenderer.create(
|
2018-08-29 15:55:56 +00:00
|
|
|
<RegistryProvider value={ registry }>
|
2018-12-31 06:42:46 +00:00
|
|
|
<LeaderboardWithSelect />
|
2018-08-29 15:55:56 +00:00
|
|
|
</RegistryProvider>
|
|
|
|
);
|
2018-12-31 06:42:46 +00:00
|
|
|
const leaderboard = leaderboardWrapper.root.findByType( Leaderboard );
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2019-02-04 08:51:29 +00:00
|
|
|
const endpoint = NAMESPACE + '/reports/products';
|
2018-12-03 02:05:25 +00:00
|
|
|
const query = { orderby: 'items_sold', per_page: 5, extended_info: 1 };
|
2018-08-29 15:55:56 +00:00
|
|
|
|
|
|
|
expect( getReportStatsMock.mock.calls[ 0 ][ 1 ] ).toBe( endpoint );
|
|
|
|
expect( getReportStatsMock.mock.calls[ 0 ][ 2 ] ).toEqual( query );
|
|
|
|
expect( isReportStatsRequestingMock.mock.calls[ 0 ][ 1 ] ).toBe( endpoint );
|
|
|
|
expect( isReportStatsRequestingMock.mock.calls[ 0 ][ 2 ] ).toEqual( query );
|
2018-12-18 19:32:25 +00:00
|
|
|
expect( isReportStatsErrorMock.mock.calls[ 0 ][ 1 ] ).toBe( endpoint );
|
|
|
|
expect( isReportStatsErrorMock.mock.calls[ 0 ][ 2 ] ).toEqual( query );
|
2018-12-31 06:42:46 +00:00
|
|
|
expect( leaderboard.props.data ).toBe( mockData );
|
2018-08-29 15:55:56 +00:00
|
|
|
} );
|
|
|
|
} );
|