2020-05-04 22:58:39 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-06-10 01:55:06 +00:00
|
|
|
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { useUserPreferences } from '@woocommerce/data';
|
2020-05-14 03:23:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-05-04 22:58:39 +00:00
|
|
|
import { StatsOverview } from '../index';
|
2020-05-14 03:23:03 +00:00
|
|
|
import StatsList from '../stats-list';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { recordEvent } from '../../../lib/tracks';
|
2020-05-04 22:58:39 +00:00
|
|
|
|
|
|
|
jest.mock( 'lib/tracks' );
|
2020-05-14 03:23:03 +00:00
|
|
|
// Mock the stats list so that it can be tested separately.
|
|
|
|
jest.mock( '../stats-list', () =>
|
|
|
|
jest.fn().mockImplementation( () => <div>mocked stats list</div> )
|
|
|
|
);
|
2020-05-27 02:38:57 +00:00
|
|
|
// Mock the Install Jetpack CTA
|
|
|
|
jest.mock( '../install-jetpack-cta', () => {
|
|
|
|
return jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation( () => <div>mocked install jetpack cta</div> );
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
jest.mock( '@woocommerce/data' );
|
|
|
|
|
2020-05-04 22:58:39 +00:00
|
|
|
describe( 'StatsOverview tracking', () => {
|
|
|
|
it( 'should record an event when a stat is toggled', () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
useUserPreferences.mockReturnValue( {
|
|
|
|
updateUserPreferences: () => {},
|
|
|
|
hiddenStats: null,
|
|
|
|
} );
|
2020-07-28 02:32:58 +00:00
|
|
|
render( <StatsOverview /> );
|
2020-05-04 22:58:39 +00:00
|
|
|
|
2020-05-14 03:23:03 +00:00
|
|
|
const ellipsisBtn = screen.getByRole( 'button', {
|
|
|
|
name: 'Choose which values to display',
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
fireEvent.click( ellipsisBtn );
|
2020-05-14 03:23:03 +00:00
|
|
|
const totalSalesBtn = screen.getByRole( 'menuitemcheckbox', {
|
|
|
|
name: 'Total Sales',
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
fireEvent.click( totalSalesBtn );
|
|
|
|
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'statsoverview_indicators_toggle',
|
|
|
|
{
|
|
|
|
indicator_name: 'revenue/total_sales',
|
|
|
|
status: 'off',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
2020-05-29 00:40:51 +00:00
|
|
|
|
|
|
|
it( 'should record an event when a period is clicked', () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
useUserPreferences.mockReturnValue( {
|
|
|
|
updateUserPreferences: () => {},
|
|
|
|
hiddenStats: null,
|
|
|
|
} );
|
2020-07-28 02:32:58 +00:00
|
|
|
render( <StatsOverview /> );
|
2020-05-29 00:40:51 +00:00
|
|
|
|
|
|
|
const monthBtn = screen.getByRole( 'tab', {
|
|
|
|
name: 'Month to date',
|
|
|
|
} );
|
|
|
|
fireEvent.click( monthBtn );
|
|
|
|
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'statsoverview_date_picker_update',
|
|
|
|
{
|
|
|
|
period: 'month',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'StatsOverview toggle and persist stat preference', () => {
|
2020-06-10 01:55:06 +00:00
|
|
|
it( 'should update preferences', async () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
const updateUserPreferences = jest.fn();
|
|
|
|
useUserPreferences.mockReturnValue( {
|
|
|
|
updateUserPreferences,
|
|
|
|
hiddenStats: null,
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
render( <StatsOverview /> );
|
2020-05-04 22:58:39 +00:00
|
|
|
|
2020-05-14 03:23:03 +00:00
|
|
|
const ellipsisBtn = screen.getByRole( 'button', {
|
|
|
|
name: 'Choose which values to display',
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
fireEvent.click( ellipsisBtn );
|
2020-05-14 03:23:03 +00:00
|
|
|
const totalSalesBtn = screen.getByRole( 'menuitemcheckbox', {
|
|
|
|
name: 'Total Sales',
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
fireEvent.click( totalSalesBtn );
|
|
|
|
|
2020-06-10 01:55:06 +00:00
|
|
|
await waitFor( () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
expect( updateUserPreferences ).toHaveBeenCalledWith( {
|
2020-06-10 01:55:06 +00:00
|
|
|
homepage_stats: {
|
|
|
|
hiddenStats: [
|
|
|
|
'revenue/net_revenue',
|
|
|
|
'products/items_sold',
|
|
|
|
'revenue/total_sales',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
} );
|
2020-05-04 22:58:39 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2020-05-14 03:23:03 +00:00
|
|
|
|
|
|
|
describe( 'StatsOverview rendering correct elements', () => {
|
|
|
|
it( 'should include a link to all the overview page', () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
useUserPreferences.mockReturnValue( {
|
|
|
|
updateUserPreferences: () => {},
|
|
|
|
hiddenStats: null,
|
|
|
|
} );
|
2020-07-28 02:32:58 +00:00
|
|
|
render( <StatsOverview /> );
|
2020-05-14 03:23:03 +00:00
|
|
|
|
|
|
|
const viewDetailedStatsLink = screen.getByText( 'View detailed stats' );
|
|
|
|
expect( viewDetailedStatsLink ).toBeDefined();
|
|
|
|
expect( viewDetailedStatsLink.href ).toBe(
|
|
|
|
'http://localhost/admin.php?page=wc-admin&path=%2Fanalytics%2Foverview'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'StatsOverview period selection', () => {
|
|
|
|
it( 'should have Today selected by default', () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
useUserPreferences.mockReturnValue( {
|
|
|
|
updateUserPreferences: () => {},
|
|
|
|
hiddenStats: null,
|
|
|
|
} );
|
2020-07-28 02:32:58 +00:00
|
|
|
render( <StatsOverview /> );
|
2020-05-14 03:23:03 +00:00
|
|
|
|
|
|
|
const todayBtn = screen.getByRole( 'tab', { name: 'Today' } );
|
|
|
|
expect( todayBtn.classList ).toContain( 'is-active' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should select a new period', () => {
|
2020-06-10 16:46:46 +00:00
|
|
|
useUserPreferences.mockReturnValue( {
|
|
|
|
updateUserPreferences: () => {},
|
|
|
|
hiddenStats: null,
|
|
|
|
} );
|
2020-07-28 02:32:58 +00:00
|
|
|
render( <StatsOverview /> );
|
2020-05-14 03:23:03 +00:00
|
|
|
|
|
|
|
fireEvent.click( screen.getByRole( 'tab', { name: 'Month to date' } ) );
|
|
|
|
|
|
|
|
// Check props handed down to StatsList have the right period
|
|
|
|
expect( StatsList ).toHaveBeenLastCalledWith(
|
|
|
|
{
|
|
|
|
query: { compare: 'previous_period', period: 'month' },
|
|
|
|
stats: [
|
|
|
|
{
|
|
|
|
chart: 'total_sales',
|
|
|
|
label: 'Total Sales',
|
|
|
|
stat: 'revenue/total_sales',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chart: 'orders_count',
|
|
|
|
label: 'Orders',
|
|
|
|
stat: 'orders/orders_count',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|