2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-04-11 02:53:05 +00:00
|
|
|
import { mount, shallow } from 'enzyme';
|
2020-04-02 21:54:38 +00:00
|
|
|
import { numberFormat } from '@woocommerce/number';
|
2020-06-17 23:33:40 +00:00
|
|
|
import CurrencyFactory from '@woocommerce/currency';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { CURRENCY } from '@woocommerce/wc-admin-settings';
|
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 '../';
|
2020-02-14 02:23:21 +00:00
|
|
|
import mockData from '../data/top-selling-products-mock-data';
|
2020-04-02 21:54:38 +00:00
|
|
|
|
2020-06-17 23:33:40 +00:00
|
|
|
const { formatAmount, formatDecimal } = CurrencyFactory( CURRENCY );
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const rows = mockData.map( ( row ) => {
|
2020-04-02 21:54:38 +00:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
items_sold: itemsSold,
|
|
|
|
net_revenue: netRevenue,
|
|
|
|
orders_count: ordersCount,
|
|
|
|
} = row;
|
2019-04-11 02:53:05 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: '<a href="#">' + name + '</a>',
|
|
|
|
value: name,
|
|
|
|
},
|
|
|
|
{
|
2020-04-02 21:54:38 +00:00
|
|
|
display: numberFormat( CURRENCY, itemsSold ),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: itemsSold,
|
2019-04-11 02:53:05 +00:00
|
|
|
},
|
|
|
|
{
|
2020-04-02 21:54:38 +00:00
|
|
|
display: numberFormat( CURRENCY, ordersCount ),
|
2020-02-14 02:23:21 +00:00
|
|
|
value: ordersCount,
|
2019-04-11 02:53:05 +00:00
|
|
|
},
|
|
|
|
{
|
2020-06-17 23:33:40 +00:00
|
|
|
display: formatAmount( netRevenue ),
|
2020-04-02 21:54:38 +00:00
|
|
|
value: formatDecimal( netRevenue ),
|
2019-04-11 02:53:05 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
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(
|
2020-02-14 02:23:21 +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(
|
2020-02-14 02:23:21 +00:00
|
|
|
<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 );
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( firstRow[ 3 ].value ).toBe(
|
2020-04-02 21:54:38 +00:00
|
|
|
formatDecimal( mockData[ 0 ].net_revenue )
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect(
|
|
|
|
leaderboard.render().find( '.woocommerce-table__item a' ).length
|
|
|
|
).toBe( 5 );
|
2019-04-11 02:53:05 +00:00
|
|
|
expect( tableItems.at( 0 ).text() ).toBe( mockData[ 0 ].name );
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( tableItems.at( 1 ).text() ).toBe(
|
2020-04-02 21:54:38 +00:00
|
|
|
numberFormat( CURRENCY, mockData[ 0 ].items_sold )
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
|
|
|
expect( tableItems.at( 2 ).text() ).toBe(
|
2020-04-02 21:54:38 +00:00
|
|
|
numberFormat( CURRENCY, mockData[ 0 ].orders_count )
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
|
|
|
expect( tableItems.at( 3 ).text() ).toBe(
|
2020-06-17 23:33:40 +00:00
|
|
|
formatAmount( mockData[ 0 ].net_revenue )
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2018-08-29 15:55:56 +00:00
|
|
|
} );
|
|
|
|
} );
|