2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-10-15 12:41:39 +00:00
|
|
|
import { render } from '@testing-library/react';
|
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
|
|
|
|
2021-08-13 19:06:32 +00:00
|
|
|
const headers = [
|
|
|
|
{
|
|
|
|
label: 'Name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Items sold',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Orders',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Net sales',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const rows = mockData.map( ( row ) => {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
items_sold: itemsSold,
|
|
|
|
net_revenue: netRevenue,
|
|
|
|
orders_count: ordersCount,
|
|
|
|
} = row;
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
display: '<a href="#">' + name + '</a>',
|
|
|
|
value: name,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( CURRENCY, itemsSold ),
|
|
|
|
value: itemsSold,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: numberFormat( CURRENCY, ordersCount ),
|
|
|
|
value: ordersCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: formatAmount( netRevenue ),
|
|
|
|
value: formatDecimal( netRevenue ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
|
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', () => {
|
2021-08-13 19:06:32 +00:00
|
|
|
const { queryByText } = render(
|
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
|
|
|
|
2021-08-13 19:06:32 +00:00
|
|
|
expect(
|
|
|
|
queryByText( 'No data recorded for the selected time period.' )
|
|
|
|
).toBeInTheDocument();
|
2018-09-12 12:09:53 +00:00
|
|
|
} );
|
|
|
|
|
2021-08-13 19:06:32 +00:00
|
|
|
test( 'should render the headers', () => {
|
|
|
|
const { queryByText } = render(
|
|
|
|
<Leaderboard
|
|
|
|
id="products"
|
|
|
|
title={ '' }
|
|
|
|
headers={ headers }
|
|
|
|
rows={ rows }
|
|
|
|
totalRows={ 0 }
|
|
|
|
/>
|
|
|
|
);
|
2020-10-15 12:41:39 +00:00
|
|
|
|
2021-08-13 19:06:32 +00:00
|
|
|
expect( queryByText( 'Name' ) ).toBeInTheDocument();
|
|
|
|
expect( queryByText( 'Items sold' ) ).toBeInTheDocument();
|
|
|
|
expect( queryByText( 'Orders' ) ).toBeInTheDocument();
|
|
|
|
expect( queryByText( 'Net sales' ) ).toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should render formatted data in the table', () => {
|
|
|
|
const { container, queryByText } = render(
|
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
|
|
|
);
|
2018-08-29 15:55:56 +00:00
|
|
|
|
2021-08-13 19:06:32 +00:00
|
|
|
const tableRows = container.querySelectorAll( 'tr' );
|
|
|
|
|
|
|
|
expect( tableRows.length ).toBe( 6 );
|
|
|
|
expect( queryByText( 'awesome shirt' ) ).toBeInTheDocument();
|
|
|
|
expect( queryByText( '1,000.00' ) ).toBeInTheDocument();
|
|
|
|
expect( queryByText( '54.00' ) ).toBeInTheDocument();
|
|
|
|
expect( queryByText( '$999.99' ) ).toBeInTheDocument();
|
2018-08-29 15:55:56 +00:00
|
|
|
} );
|
|
|
|
} );
|