2018-09-03 15:25:38 +00:00
|
|
|
/*
|
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-11-22 23:12:12 +00:00
|
|
|
import {
|
|
|
|
isReportDataEmpty,
|
|
|
|
getReportChartData,
|
|
|
|
getSummaryNumbers,
|
|
|
|
getFilterQuery,
|
|
|
|
getReportTableData,
|
|
|
|
} from '../utils';
|
2018-10-18 09:34:37 +00:00
|
|
|
import * as ordersConfig from 'analytics/report/orders/config';
|
2018-09-03 15:25:38 +00:00
|
|
|
|
2018-11-22 23:12:12 +00:00
|
|
|
jest.mock( '../utils', () => ( {
|
|
|
|
...require.requireActual( '../utils' ),
|
|
|
|
getReportTableQuery: () => ( {
|
|
|
|
after: '2018-10-10',
|
|
|
|
before: '2018-10-10',
|
|
|
|
} ),
|
|
|
|
} ) );
|
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
describe( 'isReportDataEmpty()', () => {
|
|
|
|
it( 'returns false if report is valid', () => {
|
|
|
|
const report = {
|
|
|
|
data: {
|
|
|
|
totals: {
|
|
|
|
orders_count: 10,
|
|
|
|
num_items_sold: 9,
|
|
|
|
},
|
|
|
|
intervals: [ 0, 1, 2 ],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect( isReportDataEmpty( report ) ).toEqual( false );
|
|
|
|
} );
|
|
|
|
it( 'returns true if report object is undefined', () => {
|
|
|
|
expect( isReportDataEmpty( undefined ) ).toEqual( true );
|
|
|
|
} );
|
|
|
|
it( 'returns true if data response object is missing', () => {
|
|
|
|
expect( isReportDataEmpty( {} ) ).toEqual( true );
|
|
|
|
} );
|
|
|
|
it( 'returns true if totals response object is missing', () => {
|
|
|
|
expect( isReportDataEmpty( { data: {} } ) ).toEqual( true );
|
|
|
|
} );
|
|
|
|
it( 'returns true if intervals response object is empty', () => {
|
|
|
|
expect( isReportDataEmpty( { data: { intervals: [], totals: 2 } } ) ).toEqual( true );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2018-10-10 13:57:16 +00:00
|
|
|
describe( 'getReportChartData()', () => {
|
2018-09-03 15:25:38 +00:00
|
|
|
const select = jest.fn().mockReturnValue( {} );
|
|
|
|
const response = {
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
isEmpty: false,
|
2018-09-03 15:25:38 +00:00
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
data: {
|
|
|
|
totals: null,
|
|
|
|
intervals: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll( () => {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportStats = jest.fn().mockReturnValue( {} );
|
|
|
|
select( 'wc-api' ).isReportStatsRequesting = jest.fn().mockReturnValue( false );
|
|
|
|
select( 'wc-api' ).isReportStatsError = jest.fn().mockReturnValue( false );
|
2018-09-03 15:25:38 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( () => {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportStats.mockRestore();
|
|
|
|
select( 'wc-api' ).isReportStatsRequesting.mockRestore();
|
|
|
|
select( 'wc-api' ).isReportStatsError.mockRestore();
|
2018-09-03 15:25:38 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
function setGetReportStats( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) );
|
2018-09-03 15:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setIsReportStatsRequesting( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).isReportStatsRequesting.mockImplementation( ( ...args ) =>
|
2018-09-03 15:25:38 +00:00
|
|
|
func( ...args )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setIsReportStatsError( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) );
|
2018-09-03 15:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it( 'returns isRequesting if first request is in progress', () => {
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return true;
|
|
|
|
} );
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getReportChartData( 'revenue', 'primary', {}, select );
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( result ).toEqual( { ...response, isRequesting: true } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns isError if first request errors', () => {
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return true;
|
|
|
|
} );
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getReportChartData( 'revenue', 'primary', {}, select );
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( result ).toEqual( { ...response, isError: true } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns results after single page of data', () => {
|
|
|
|
const data = {
|
|
|
|
totals: {
|
|
|
|
orders_count: 115,
|
|
|
|
gross_revenue: 13966.92,
|
|
|
|
},
|
|
|
|
intervals: [
|
|
|
|
{
|
|
|
|
interval: 'day',
|
|
|
|
date_start: '2018-07-01 00:00:00',
|
|
|
|
subtotals: {
|
|
|
|
orders_count: 115,
|
|
|
|
gross_revenue: 13966.92,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setGetReportStats( () => {
|
|
|
|
return {
|
|
|
|
totalResults: 1,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getReportChartData( 'revenue', 'primary', {}, select );
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( result ).toEqual( { ...response, data: { ...data } } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns combined results for multiple pages of data', () => {
|
|
|
|
const totalResults = 110;
|
|
|
|
const orders_count = 115;
|
|
|
|
const gross_revenue = 13966.92;
|
|
|
|
const intervals = [];
|
|
|
|
for ( let i = 0; i < totalResults; i++ ) {
|
|
|
|
intervals.push( {
|
|
|
|
interval: 'day',
|
|
|
|
date_start: '2018-07-01 00:00:00',
|
|
|
|
subtotals: { orders_count, gross_revenue },
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
const totals = {
|
|
|
|
orders_count: orders_count * totalResults,
|
|
|
|
gross_revenue: gross_revenue * totalResults,
|
|
|
|
};
|
|
|
|
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setGetReportStats( ( endpoint, query ) => {
|
|
|
|
if ( 2 === query.page ) {
|
|
|
|
return {
|
|
|
|
totalResults,
|
|
|
|
data: {
|
|
|
|
totals,
|
|
|
|
intervals: intervals.slice( 100, 110 ),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
totalResults,
|
|
|
|
data: {
|
|
|
|
totals,
|
|
|
|
intervals: intervals.slice( 0, 100 ),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
const actualResponse = getReportChartData( 'revenue', 'primary', {}, select );
|
2018-09-03 15:25:38 +00:00
|
|
|
const expectedResponse = {
|
|
|
|
...response,
|
|
|
|
data: {
|
|
|
|
totals,
|
|
|
|
intervals,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect( actualResponse ).toEqual( expectedResponse );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns isRequesting if additional requests are in progress', () => {
|
|
|
|
setIsReportStatsRequesting( ( endpoint, query ) => {
|
|
|
|
if ( 2 === query.page ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getReportChartData( 'revenue', 'primary', {}, select );
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( result ).toEqual( { ...response, isRequesting: true } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns isError if additional requests return an error', () => {
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( ( endpoint, query ) => {
|
|
|
|
if ( 2 === query.page ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} );
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getReportChartData( 'revenue', 'primary', {}, select );
|
2018-09-03 15:25:38 +00:00
|
|
|
expect( result ).toEqual( { ...response, isError: true } );
|
|
|
|
} );
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
|
|
|
|
it( 'returns empty state if a query returns no data', () => {
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setGetReportStats( () => {
|
|
|
|
return {
|
|
|
|
totalResults: undefined,
|
|
|
|
data: {},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getReportChartData( 'revenue', 'primary', {}, select );
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
expect( result ).toEqual( { ...response, isEmpty: true } );
|
|
|
|
} );
|
2018-09-03 15:25:38 +00:00
|
|
|
} );
|
2018-10-11 18:45:01 +00:00
|
|
|
|
|
|
|
describe( 'getSummaryNumbers()', () => {
|
|
|
|
const select = jest.fn().mockReturnValue( {} );
|
|
|
|
const response = {
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
totals: {
|
|
|
|
primary: null,
|
|
|
|
secondary: null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
const query = {
|
|
|
|
after: '2018-10-10',
|
|
|
|
before: '2018-10-10',
|
|
|
|
period: 'custom',
|
|
|
|
compare: 'previous_period',
|
2018-10-11 18:45:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll( () => {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportStats = jest.fn().mockReturnValue( {} );
|
|
|
|
select( 'wc-api' ).isReportStatsRequesting = jest.fn().mockReturnValue( false );
|
|
|
|
select( 'wc-api' ).isReportStatsError = jest.fn().mockReturnValue( false );
|
2018-10-11 18:45:01 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( () => {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportStats.mockRestore();
|
|
|
|
select( 'wc-api' ).isReportStatsRequesting.mockRestore();
|
|
|
|
select( 'wc-api' ).isReportStatsError.mockRestore();
|
2018-10-11 18:45:01 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
function setGetReportStats( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportStats.mockImplementation( ( ...args ) => func( ...args ) );
|
2018-10-11 18:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setIsReportStatsRequesting( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).isReportStatsRequesting.mockImplementation( ( ...args ) =>
|
2018-10-11 18:45:01 +00:00
|
|
|
func( ...args )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setIsReportStatsError( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).isReportStatsError.mockImplementation( ( ...args ) => func( ...args ) );
|
2018-10-11 18:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it( 'returns isRequesting if a request is in progress', () => {
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return true;
|
|
|
|
} );
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getSummaryNumbers( 'revenue', query, select );
|
2018-10-11 18:45:01 +00:00
|
|
|
expect( result ).toEqual( { ...response, isRequesting: true } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns isError if request errors', () => {
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return true;
|
|
|
|
} );
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getSummaryNumbers( 'revenue', query, select );
|
2018-10-11 18:45:01 +00:00
|
|
|
expect( result ).toEqual( { ...response, isError: true } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns results after queries finish', () => {
|
|
|
|
const totals = {
|
|
|
|
primary: {
|
|
|
|
orders_count: 115,
|
|
|
|
gross_revenue: 13966.92,
|
|
|
|
},
|
|
|
|
secondary: {
|
|
|
|
orders_count: 85,
|
|
|
|
gross_revenue: 10406.1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
setIsReportStatsRequesting( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setIsReportStatsError( () => {
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
setGetReportStats( () => {
|
|
|
|
return {
|
|
|
|
totals,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
setGetReportStats( ( endpoint, _query ) => {
|
|
|
|
if ( '2018-10-10T00:00:00+00:00' === _query.after ) {
|
2018-10-11 18:45:01 +00:00
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
totals: totals.primary,
|
|
|
|
intervals: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
totals: totals.secondary,
|
|
|
|
intervals: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2018-10-17 23:24:36 +00:00
|
|
|
const result = getSummaryNumbers( 'revenue', query, select );
|
2018-10-11 18:45:01 +00:00
|
|
|
expect( result ).toEqual( { ...response, totals } );
|
|
|
|
} );
|
|
|
|
} );
|
2018-10-18 09:34:37 +00:00
|
|
|
|
|
|
|
describe( 'getFilterQuery', () => {
|
|
|
|
/**
|
|
|
|
* Mock the orders config
|
|
|
|
*/
|
|
|
|
const filters = [
|
|
|
|
{
|
2018-10-29 01:30:24 +00:00
|
|
|
param: 'filter',
|
|
|
|
filters: [
|
|
|
|
{ value: 'top_meal', query: { lunch: 'burritos' } },
|
|
|
|
{ value: 'top_dessert', query: { dinner: 'ice_cream' } },
|
|
|
|
{ value: 'compare-cuisines', settings: { param: 'region' } },
|
|
|
|
{
|
|
|
|
value: 'food_destination',
|
|
|
|
subFilters: [
|
|
|
|
{ value: 'choose_a_european_city', settings: { param: 'european_cities' } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2018-10-18 09:34:37 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const advancedFilters = {
|
|
|
|
filters: {
|
|
|
|
mexican: {
|
|
|
|
rules: [ { value: 'is' }, { value: 'is_not' } ],
|
|
|
|
},
|
|
|
|
french: {
|
|
|
|
rules: [ { value: 'includes' }, { value: 'excludes' } ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
ordersConfig.filters = filters;
|
|
|
|
ordersConfig.advancedFilters = advancedFilters;
|
|
|
|
|
|
|
|
it( 'should return an empty object if no filter param is given', () => {
|
|
|
|
const query = {};
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return an empty object if filter parameter is not in configs', () => {
|
|
|
|
const query = { filter: 'canned_meat' };
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( {} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return the query for an advanced filter', () => {
|
|
|
|
const query = { filter: 'advanced', mexican_is: 'delicious' };
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( { mexican_is: 'delicious', match: 'all' } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should ignore other queries not defined by filter configs', () => {
|
|
|
|
const query = {
|
|
|
|
filter: 'advanced',
|
|
|
|
mexican_is_not: 'healthy',
|
|
|
|
orderby: 'calories',
|
|
|
|
topping: 'salsa-verde',
|
|
|
|
};
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( { mexican_is_not: 'healthy', match: 'all' } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should apply the match parameter advanced filters', () => {
|
|
|
|
const query = {
|
|
|
|
filter: 'advanced',
|
|
|
|
french_includes: 'le-fromage',
|
|
|
|
match: 'any',
|
|
|
|
};
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( { french_includes: 'le-fromage', match: 'any' } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return the query for compare filters', () => {
|
|
|
|
const query = { filter: 'compare-cuisines', region: 'vietnam,malaysia,thailand' };
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( { region: 'vietnam,malaysia,thailand' } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return the query for subFilters', () => {
|
|
|
|
const query = { filter: 'choose_a_european_city', european_cities: 'paris,rome,barcelona' };
|
|
|
|
const filterQuery = getFilterQuery( 'orders', query );
|
|
|
|
|
|
|
|
expect( filterQuery ).toEqual( { european_cities: 'paris,rome,barcelona' } );
|
|
|
|
} );
|
|
|
|
} );
|
2018-11-22 23:12:12 +00:00
|
|
|
|
|
|
|
describe( 'getReportTableData()', () => {
|
|
|
|
const select = jest.fn().mockReturnValue( {} );
|
|
|
|
const response = {
|
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
items: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
after: '2018-10-10',
|
|
|
|
before: '2018-10-10',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll( () => {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportItems = jest.fn().mockReturnValue( {} );
|
2018-12-05 23:12:34 +00:00
|
|
|
select( 'wc-api' ).isReportItemsRequesting = jest.fn().mockReturnValue( false );
|
|
|
|
select( 'wc-api' ).isReportItemsError = jest.fn().mockReturnValue( false );
|
2018-11-22 23:12:12 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( () => {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportItems.mockRestore();
|
2018-12-05 23:12:34 +00:00
|
|
|
select( 'wc-api' ).isReportItemsRequesting.mockRestore();
|
|
|
|
select( 'wc-api' ).isReportItemsError.mockRestore();
|
2018-11-22 23:12:12 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
function setGetReportItems( func ) {
|
2018-12-05 23:05:40 +00:00
|
|
|
select( 'wc-api' ).getReportItems.mockImplementation( ( ...args ) => func( ...args ) );
|
2018-11-22 23:12:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 23:12:34 +00:00
|
|
|
function setisReportItemsRequesting( func ) {
|
|
|
|
select( 'wc-api' ).isReportItemsRequesting.mockImplementation( ( ...args ) =>
|
2018-11-22 23:12:12 +00:00
|
|
|
func( ...args )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-05 23:12:34 +00:00
|
|
|
function setisReportItemsError( func ) {
|
|
|
|
select( 'wc-api' ).isReportItemsError.mockImplementation( ( ...args ) => func( ...args ) );
|
2018-11-22 23:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it( 'returns isRequesting if a request is in progress', () => {
|
2018-12-05 23:12:34 +00:00
|
|
|
setisReportItemsRequesting( () => true );
|
2018-11-22 23:12:12 +00:00
|
|
|
|
|
|
|
const result = getReportTableData( 'coupons', query, select );
|
|
|
|
|
|
|
|
expect( result ).toEqual( { ...response, query, isRequesting: true } );
|
2018-12-05 23:05:40 +00:00
|
|
|
expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
|
2018-12-05 23:12:34 +00:00
|
|
|
expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith(
|
2018-11-22 23:12:12 +00:00
|
|
|
'coupons',
|
|
|
|
query
|
|
|
|
);
|
2018-12-05 23:12:34 +00:00
|
|
|
expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenCalledTimes( 0 );
|
2018-11-22 23:12:12 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns isError if request errors', () => {
|
2018-12-05 23:12:34 +00:00
|
|
|
setisReportItemsRequesting( () => false );
|
|
|
|
setisReportItemsError( () => true );
|
2018-11-22 23:12:12 +00:00
|
|
|
|
|
|
|
const result = getReportTableData( 'coupons', query, select );
|
|
|
|
|
|
|
|
expect( result ).toEqual( { ...response, query, isError: true } );
|
2018-12-05 23:05:40 +00:00
|
|
|
expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
|
2018-12-05 23:12:34 +00:00
|
|
|
expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith(
|
2018-11-22 23:12:12 +00:00
|
|
|
'coupons',
|
|
|
|
query
|
|
|
|
);
|
2018-12-05 23:12:34 +00:00
|
|
|
expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenLastCalledWith(
|
2018-11-22 23:12:12 +00:00
|
|
|
'coupons',
|
|
|
|
query
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'returns results after queries finish', () => {
|
|
|
|
const items = [ { id: 1 }, { id: 2 }, { id: 3 } ];
|
2018-12-05 23:12:34 +00:00
|
|
|
setisReportItemsRequesting( () => false );
|
|
|
|
setisReportItemsError( () => false );
|
2018-11-22 23:12:12 +00:00
|
|
|
setGetReportItems( () => items );
|
|
|
|
|
|
|
|
const result = getReportTableData( 'coupons', query, select );
|
|
|
|
|
|
|
|
expect( result ).toEqual( { ...response, query, items } );
|
2018-12-05 23:05:40 +00:00
|
|
|
expect( select( 'wc-api' ).getReportItems ).toHaveBeenLastCalledWith( 'coupons', query );
|
2018-12-05 23:12:34 +00:00
|
|
|
expect( select( 'wc-api' ).isReportItemsRequesting ).toHaveBeenLastCalledWith(
|
2018-11-22 23:12:12 +00:00
|
|
|
'coupons',
|
|
|
|
query
|
|
|
|
);
|
2018-12-05 23:12:34 +00:00
|
|
|
expect( select( 'wc-api' ).isReportItemsError ).toHaveBeenLastCalledWith(
|
2018-11-22 23:12:12 +00:00
|
|
|
'coupons',
|
|
|
|
query
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|