2018-12-07 21:13:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
};
|
|
|
|
return mount(
|
|
|
|
<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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
test( 'should set the correct prop values for the SummaryNumber components', () => {
|
|
|
|
const reportChart = renderChart( 'number', 1000.5, 500.25 );
|
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryNumber.props().value ).toBe( '1,000.5' );
|
|
|
|
expect( summaryNumber.props().prevValue ).toBe( '500.25' );
|
|
|
|
expect( summaryNumber.props().delta ).toBe( 100 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should format currency numbers properly', () => {
|
|
|
|
const reportChart = renderChart( 'currency', 1000.5, 500.25 );
|
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryNumber.props().value ).toBe( '$1,000.50' );
|
|
|
|
expect( summaryNumber.props().prevValue ).toBe( '$500.25' );
|
|
|
|
expect( summaryNumber.props().delta ).toBe( 100 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should format average numbers properly', () => {
|
|
|
|
const reportChart = renderChart( 'average', 1000.5, 500.25 );
|
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryNumber.props().value ).toBe( 1001 );
|
|
|
|
expect( summaryNumber.props().prevValue ).toBe( 500 );
|
|
|
|
expect( summaryNumber.props().delta ).toBe( 100 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should not break if secondary value is 0', () => {
|
|
|
|
const reportChart = renderChart( 'number', 1000.5, 0 );
|
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryNumber.props().value ).toBe( '1,000.5' );
|
|
|
|
expect( summaryNumber.props().prevValue ).toBe( '0' );
|
|
|
|
expect( summaryNumber.props().delta ).toBe( 0 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should not break with null or undefined values', () => {
|
|
|
|
const reportChart = renderChart( 'number', null, undefined );
|
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryNumber.props().value ).toBe( null );
|
|
|
|
expect( summaryNumber.props().prevValue ).toBe( null );
|
|
|
|
expect( summaryNumber.props().delta ).toBe( null );
|
|
|
|
} );
|
|
|
|
|
2019-02-07 09:57:47 +00:00
|
|
|
test( 'should show 0s when displaying an empty search', () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const reportChart = renderChart(
|
|
|
|
'number',
|
|
|
|
null,
|
|
|
|
undefined,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
{
|
|
|
|
emptySearchResults: true,
|
|
|
|
}
|
|
|
|
);
|
2019-02-07 09:57:47 +00:00
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryNumber.props().value ).toBe( '0' );
|
|
|
|
expect( summaryNumber.props().prevValue ).toBe( '0' );
|
|
|
|
expect( summaryNumber.props().delta ).toBe( 0 );
|
|
|
|
} );
|
|
|
|
|
2018-12-07 21:13:02 +00:00
|
|
|
test( 'should display ReportError when isError is true', () => {
|
|
|
|
const reportChart = renderChart( 'number', null, null, true );
|
|
|
|
const reportError = reportChart.find( 'ReportError' );
|
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( reportError ).toHaveLength( 1 );
|
|
|
|
expect( summaryNumber ).toHaveLength( 0 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should display SummaryListPlaceholder when isRequesting is true', () => {
|
|
|
|
const reportChart = renderChart( 'number', null, null, false, true );
|
2020-02-14 02:23:21 +00:00
|
|
|
const summaryListPlaceholder = reportChart.find(
|
|
|
|
'SummaryListPlaceholder'
|
|
|
|
);
|
2018-12-07 21:13:02 +00:00
|
|
|
const summaryNumber = reportChart.find( 'SummaryNumber' );
|
|
|
|
|
|
|
|
expect( summaryListPlaceholder ).toHaveLength( 1 );
|
|
|
|
expect( summaryNumber ).toHaveLength( 0 );
|
|
|
|
} );
|
|
|
|
} );
|