2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*
|
|
|
|
* @format
|
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
import { mount, shallow } from 'enzyme';
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2019-11-21 21:51:52 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from 'lib/currency-format';
|
|
|
|
import { numberFormat } from 'lib/number-format';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
import { Leaderboard } from '../';
|
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
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
const rows = mockData.map( row => {
|
|
|
|
const { name, items_sold, net_revenue, orders_count } = row;
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: '<a href="#">' + name + '</a>',
|
|
|
|
value: name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( items_sold ),
|
|
|
|
value: items_sold,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( orders_count ),
|
|
|
|
value: orders_count,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatCurrency( net_revenue ),
|
|
|
|
value: getCurrencyFormatDecimal( net_revenue ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
const headers = [
|
|
|
|
{
|
|
|
|
label: 'Name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Items Sold',
|
|
|
|
},
|
|
|
|
{
|
2019-08-26 22:37:16 +00:00
|
|
|
label: 'Orders',
|
2019-04-11 02:53:05 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:06:14 +00:00
|
|
|
label: 'Net Sales',
|
2019-04-11 02:53:05 +00:00
|
|
|
},
|
|
|
|
];
|
2018-12-31 06:42:46 +00:00
|
|
|
|
|
|
|
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(
|
2019-04-11 02:53:05 +00:00
|
|
|
<Leaderboard id="products" title={ '' } headers={ [] } rows={ [] } totalRows={ 5 } />
|
2018-12-31 06:42:46 +00:00
|
|
|
);
|
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', () => {
|
2019-04-11 02:53:05 +00:00
|
|
|
const leaderboard = mount(
|
|
|
|
<Leaderboard id="products" title={ '' } headers={ headers } rows={ rows } totalRows={ 5 } />
|
2018-12-31 06:42:46 +00:00
|
|
|
);
|
|
|
|
const table = leaderboard.find( 'TableCard' );
|
2018-08-29 15:55:56 +00:00
|
|
|
const firstRow = table.props().rows[ 0 ];
|
2019-04-11 02:53:05 +00:00
|
|
|
const tableItems = leaderboard.find( '.woocommerce-table__item' );
|
2018-08-29 15:55:56 +00:00
|
|
|
|
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 ].value ).toBe( mockData[ 0 ].items_sold );
|
|
|
|
expect( firstRow[ 2 ].value ).toBe( mockData[ 0 ].orders_count );
|
2018-12-19 00:56:27 +00:00
|
|
|
expect( firstRow[ 3 ].value ).toBe( getCurrencyFormatDecimal( mockData[ 0 ].net_revenue ) );
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2019-04-11 02:53:05 +00:00
|
|
|
expect( leaderboard.render().find( '.woocommerce-table__item a' ).length ).toBe( 5 );
|
|
|
|
expect( tableItems.at( 0 ).text() ).toBe( mockData[ 0 ].name );
|
|
|
|
expect( tableItems.at( 1 ).text() ).toBe( numberFormat( mockData[ 0 ].items_sold ) );
|
|
|
|
expect( tableItems.at( 2 ).text() ).toBe( numberFormat( mockData[ 0 ].orders_count ) );
|
|
|
|
expect( tableItems.at( 3 ).text() ).toBe( formatCurrency( mockData[ 0 ].net_revenue ) );
|
2018-08-29 15:55:56 +00:00
|
|
|
} );
|
|
|
|
} );
|