2018-08-29 15:55:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-07-08 03:15:12 +00:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import { CurrencyFactory, CurrencyContext } from '@woocommerce/currency';
|
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';
|
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,
|
|
|
|
},
|
|
|
|
{
|
2024-07-08 03:15:12 +00:00
|
|
|
display: itemsSold.toString(),
|
2021-08-13 19:06:32 +00:00
|
|
|
value: itemsSold,
|
2024-07-08 03:15:12 +00:00
|
|
|
format: 'number',
|
2021-08-13 19:06:32 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-08 03:15:12 +00:00
|
|
|
display: ordersCount.toString(),
|
|
|
|
value: ordersCount.toString(),
|
|
|
|
format: 'number',
|
2021-08-13 19:06:32 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-08 03:15:12 +00:00
|
|
|
display: `<span class="woocommerce-Price-currencySymbol">${ netRevenue }</span>`,
|
|
|
|
value: netRevenue,
|
|
|
|
format: 'currency',
|
2021-08-13 19:06:32 +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', () => {
|
2024-07-08 03:15:12 +00:00
|
|
|
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(
|
2024-07-08 03:15:12 +00:00
|
|
|
screen.getByText( 'No data recorded for the selected time period.' )
|
2021-08-13 19:06:32 +00:00
|
|
|
).toBeInTheDocument();
|
2018-09-12 12:09:53 +00:00
|
|
|
} );
|
|
|
|
|
2021-08-13 19:06:32 +00:00
|
|
|
test( 'should render the headers', () => {
|
2024-07-08 03:15:12 +00:00
|
|
|
render(
|
2021-08-13 19:06:32 +00:00
|
|
|
<Leaderboard
|
|
|
|
id="products"
|
|
|
|
title={ '' }
|
|
|
|
headers={ headers }
|
|
|
|
rows={ rows }
|
2024-07-08 03:15:12 +00:00
|
|
|
totalRows={ 5 }
|
2021-08-13 19:06:32 +00:00
|
|
|
/>
|
|
|
|
);
|
2020-10-15 12:41:39 +00:00
|
|
|
|
2024-07-08 03:15:12 +00:00
|
|
|
expect( screen.getByText( 'Name' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'Items sold' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'Orders' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'Net sales' ) ).toBeInTheDocument();
|
2021-08-13 19:06:32 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should render formatted data in the table', () => {
|
2024-07-08 03:15:12 +00:00
|
|
|
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
|
|
|
|
2024-07-08 03:15:12 +00:00
|
|
|
expect( screen.getAllByRole( 'row' ) ).toHaveLength( 6 );
|
|
|
|
expect( screen.getByText( 'awesome shirt' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( '123,456,789' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( '54' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( '$9,876,543.22' ) ).toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should format data according to the currency context', () => {
|
|
|
|
const currencySetting = {
|
|
|
|
code: 'PLN',
|
|
|
|
decimalSeparator: ',',
|
|
|
|
precision: 3,
|
|
|
|
priceFormat: '%1$s %2$s',
|
|
|
|
symbol: 'zł',
|
|
|
|
thousandSeparator: '.',
|
|
|
|
};
|
|
|
|
|
|
|
|
render(
|
|
|
|
<CurrencyContext.Provider
|
|
|
|
value={ new CurrencyFactory( currencySetting ) }
|
|
|
|
>
|
|
|
|
<Leaderboard
|
|
|
|
id="products"
|
|
|
|
title={ '' }
|
|
|
|
headers={ headers }
|
|
|
|
rows={ rows }
|
|
|
|
totalRows={ 5 }
|
|
|
|
/>
|
|
|
|
</CurrencyContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( screen.getByText( 'awesome shirt' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( '123.456.789' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( '54' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'zł 9.876.543,215' ) ).toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( `should not format data that is not specified in a format or doesn't conform to a number value`, () => {
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
display: 'awesome shirt',
|
|
|
|
value: '$123.456', // Not a pure numeric string
|
|
|
|
format: 'currency',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: 'awesome pants',
|
|
|
|
value: '123,456', // Not a pure numeric string
|
|
|
|
format: 'number',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: 'awesome hat',
|
|
|
|
value: '', // Not a number
|
|
|
|
format: 'number',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
display: 'awesome sticker',
|
|
|
|
value: 123,
|
|
|
|
format: 'product', // Invalid format
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Not specified format
|
|
|
|
display: 'awesome button',
|
|
|
|
value: 123,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
render(
|
|
|
|
<Leaderboard
|
|
|
|
id="products"
|
|
|
|
title={ '' }
|
|
|
|
headers={ columns.map( ( _, i ) => ( {
|
|
|
|
label: i.toString(),
|
|
|
|
} ) ) }
|
|
|
|
rows={ [ columns ] }
|
|
|
|
totalRows={ 5 }
|
|
|
|
/>
|
|
|
|
);
|
2021-08-13 19:06:32 +00:00
|
|
|
|
2024-07-08 03:15:12 +00:00
|
|
|
expect( screen.getByText( 'awesome shirt' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'awesome pants' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'awesome hat' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'awesome sticker' ) ).toBeInTheDocument();
|
|
|
|
expect( screen.getByText( 'awesome button' ) ).toBeInTheDocument();
|
2018-08-29 15:55:56 +00:00
|
|
|
} );
|
|
|
|
} );
|