woocommerce/plugins/woocommerce-admin/client/analytics/components/report-summary/test/index.js

135 lines
3.8 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { mount } from 'enzyme';
/**
* Internal dependencies
*/
import { ReportSummary } from '../';
describe( 'ReportSummary', () => {
function renderChart(
type,
primaryValue,
secondaryValue,
isError = false,
isRequesting = false,
props
) {
const selectedChart = {
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
key: 'total_sales',
label: 'Total Sales',
type,
};
const charts = [ selectedChart ];
const endpoint = 'revenue';
const query = {};
const summaryData = {
totals: {
primary: {
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
total_sales: primaryValue,
},
secondary: {
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
total_sales: secondaryValue,
},
},
isError,
isRequesting,
};
return mount(
<ReportSummary
charts={ charts }
endpoint={ endpoint }
query={ query }
selectedChart={ selectedChart }
summaryData={ summaryData }
{ ...props }
/>
);
}
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 );
} );
test( 'should show 0s when displaying an empty search', () => {
const reportChart = renderChart(
'number',
null,
undefined,
false,
false,
{
emptySearchResults: true,
}
);
const summaryNumber = reportChart.find( 'SummaryNumber' );
expect( summaryNumber.props().value ).toBe( '0' );
expect( summaryNumber.props().prevValue ).toBe( '0' );
expect( summaryNumber.props().delta ).toBe( 0 );
} );
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 );
const summaryListPlaceholder = reportChart.find(
'SummaryListPlaceholder'
);
const summaryNumber = reportChart.find( 'SummaryNumber' );
expect( summaryListPlaceholder ).toHaveLength( 1 );
expect( summaryNumber ).toHaveLength( 0 );
} );
} );