/** * External dependencies */ import { render, screen } from '@testing-library/react'; import { CurrencyFactory, CurrencyContext } from '@woocommerce/currency'; /** * Internal dependencies */ import { Leaderboard } from '../'; import mockData from '../data/top-selling-products-mock-data'; 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: '' + name + '', value: name, }, { display: itemsSold.toString(), value: itemsSold, format: 'number', }, { display: ordersCount.toString(), value: ordersCount.toString(), format: 'number', }, { display: `${ netRevenue }`, value: netRevenue, format: 'currency', }, ]; } ); describe( 'Leaderboard', () => { test( 'should render empty message when there are no rows', () => { render( ); expect( screen.getByText( 'No data recorded for the selected time period.' ) ).toBeInTheDocument(); } ); test( 'should render the headers', () => { render( ); expect( screen.getByText( 'Name' ) ).toBeInTheDocument(); expect( screen.getByText( 'Items sold' ) ).toBeInTheDocument(); expect( screen.getByText( 'Orders' ) ).toBeInTheDocument(); expect( screen.getByText( 'Net sales' ) ).toBeInTheDocument(); } ); test( 'should render formatted data in the table', () => { render( ); 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( ); 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( ( { label: i.toString(), } ) ) } rows={ [ columns ] } totalRows={ 5 } /> ); 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(); } ); } );