2018-12-07 21:13:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-10-15 12:41:39 +00:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2018-12-07 21:13:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ReportSummary } from '../';
|
|
|
|
|
|
|
|
describe( 'ReportSummary', () => {
|
|
|
|
function renderChart(
|
|
|
|
type,
|
|
|
|
primaryValue,
|
|
|
|
secondaryValue,
|
|
|
|
isError = false,
|
2019-02-07 09:57:47 +00:00
|
|
|
isRequesting = false,
|
|
|
|
props
|
2018-12-07 21:13:02 +00:00
|
|
|
) {
|
|
|
|
const selectedChart = {
|
2019-11-22 15:06:14 +00:00
|
|
|
key: 'total_sales',
|
|
|
|
label: 'Total Sales',
|
2018-12-07 21:13:02 +00:00
|
|
|
type,
|
|
|
|
};
|
|
|
|
const charts = [ selectedChart ];
|
|
|
|
const endpoint = 'revenue';
|
|
|
|
const query = {};
|
|
|
|
const summaryData = {
|
|
|
|
totals: {
|
|
|
|
primary: {
|
2019-11-22 15:06:14 +00:00
|
|
|
total_sales: primaryValue,
|
2018-12-07 21:13:02 +00:00
|
|
|
},
|
|
|
|
secondary: {
|
2019-11-22 15:06:14 +00:00
|
|
|
total_sales: secondaryValue,
|
2018-12-07 21:13:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
};
|
2020-10-15 12:41:39 +00:00
|
|
|
return render(
|
2018-12-07 21:13:02 +00:00
|
|
|
<ReportSummary
|
|
|
|
charts={ charts }
|
|
|
|
endpoint={ endpoint }
|
|
|
|
query={ query }
|
|
|
|
selectedChart={ selectedChart }
|
|
|
|
summaryData={ summaryData }
|
2019-02-07 09:57:47 +00:00
|
|
|
{ ...props }
|
2018-12-07 21:13:02 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'should set the correct prop values for the SummaryNumber components', async () => {
|
|
|
|
renderChart( 'number', 1000.5, 500.25 );
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
expect( screen.getByText( '1,000.5' ) ).toBeInTheDocument();
|
|
|
|
|
|
|
|
const delta = screen.getByText( '100%' );
|
|
|
|
expect( delta ).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.hover( delta );
|
|
|
|
const tooltip = await screen.findByText( 'Previous Year: 500.25' );
|
|
|
|
expect( tooltip ).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.unhover( delta );
|
|
|
|
expect( screen.queryByText( 'Previous Year: 500.25' ) ).toBeNull();
|
2018-12-07 21:13:02 +00:00
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'should format currency numbers properly', async () => {
|
|
|
|
renderChart( 'currency', 1000.5, 500.25 );
|
|
|
|
|
|
|
|
expect( screen.getByText( '$1,000.50' ) ).toBeInTheDocument();
|
|
|
|
|
|
|
|
const delta = screen.getByText( '100%' );
|
|
|
|
expect( delta ).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.hover( delta );
|
|
|
|
const tooltip = await screen.findByText( 'Previous Year: $500.25' );
|
|
|
|
expect( tooltip ).toBeInTheDocument();
|
|
|
|
|
|
|
|
userEvent.unhover( delta );
|
|
|
|
expect( screen.queryByText( 'Previous Year: $500.25' ) ).toBeNull();
|
2018-12-07 21:13:02 +00:00
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'should format average numbers properly', async () => {
|
|
|
|
renderChart( 'average', 1000.5, 500.25 );
|
|
|
|
|
|
|
|
expect( screen.getByText( '1001' ) ).toBeInTheDocument();
|
|
|
|
|
|
|
|
const delta = screen.getByText( '100%' );
|
|
|
|
expect( delta ).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.hover( delta );
|
|
|
|
const tooltip = await screen.findByText( 'Previous Year: 500' );
|
|
|
|
expect( tooltip ).toBeInTheDocument();
|
|
|
|
|
|
|
|
userEvent.unhover( delta );
|
|
|
|
expect( screen.queryByText( 'Previous Year: 500' ) ).toBeNull();
|
2018-12-07 21:13:02 +00:00
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'should not break if secondary value is 0', async () => {
|
|
|
|
renderChart( 'number', 1000.5, 0 );
|
|
|
|
|
|
|
|
expect( screen.getByText( '1,000.5' ) ).toBeInTheDocument();
|
|
|
|
|
|
|
|
const delta = screen.getByText( '0%' );
|
|
|
|
expect( delta ).toBeInTheDocument();
|
|
|
|
|
|
|
|
userEvent.hover( delta );
|
|
|
|
const tooltip = await screen.findByText( 'Previous Year: 0' );
|
|
|
|
expect( tooltip ).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
userEvent.unhover( delta );
|
|
|
|
expect( screen.queryByText( 'Previous Year: 0' ) ).toBeNull();
|
2018-12-07 21:13:02 +00:00
|
|
|
} );
|
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
test( 'should show 0s when displaying an empty search', async () => {
|
|
|
|
renderChart( 'number', null, undefined );
|
|
|
|
|
|
|
|
expect( screen.getAllByText( 'N/A' ) ).not.toBeNull();
|
2019-02-07 09:57:47 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
const delta = screen.getByLabelText( 'No change from Previous Year:' );
|
|
|
|
expect( delta ).toBeInTheDocument();
|
2019-02-07 09:57:47 +00:00
|
|
|
} );
|
|
|
|
|
2018-12-07 21:13:02 +00:00
|
|
|
test( 'should display ReportError when isError is true', () => {
|
2020-10-15 12:41:39 +00:00
|
|
|
renderChart( 'number', null, null, true );
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
expect(
|
|
|
|
screen.getByText(
|
|
|
|
'There was an error getting your stats. Please try again.'
|
|
|
|
)
|
|
|
|
).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should display SummaryListPlaceholder when isRequesting is true', () => {
|
2020-10-15 12:41:39 +00:00
|
|
|
const { container } = renderChart( 'number', null, null, false, true );
|
2018-12-07 21:13:02 +00:00
|
|
|
|
2020-10-15 12:41:39 +00:00
|
|
|
expect(
|
|
|
|
container.querySelector( '.woocommerce-summary.is-placeholder' )
|
|
|
|
).toBeInTheDocument();
|
2018-12-07 21:13:02 +00:00
|
|
|
} );
|
|
|
|
} );
|