woocommerce/plugins/woocommerce-admin/client/homescreen/test/index.js

277 lines
7.2 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { render, screen, within } from '@testing-library/react';
import { useUserPreferences } from '@woocommerce/data';
/**
* Internal dependencies
*/
import { Layout } from '../layout';
Allow packages to be built in isolation. (https://github.com/woocommerce/woocommerce-admin/pull/7286) * Use yarn instead of npm. In prep for workspaces, since we're locked to npm < 7. See: https://github.com/woocommerce/woocommerce-admin/pull/7126#issue-661287749 * Initial workspace creation. * Add initial tsc build to @woocommerce/number. * Attempt to build experimental package. * Try currency package. * Define all packages as workspaces. * Use tsconfig common to packages. * Fix currency package build. * Build csv-export with tsc. * Try to build customer-effort-score with tsc. * Fix JSX pragma. * Build data package with tsc. * Build date package with tsc. * Build experimental package with tsc. * Try to build explat package with tsc. * Build navigation package with tsc. * Build notices package with tsc. * Build onboarding package with tsc. * Build components package with tsc. * Swap in package JS build into main script. * Fix experimental package build. * Try per-package css build with components. * Try to run components package tests in isolation. Broken on JSX in test files not being transformed. * Move @woocommerce/wc-admin-settings into a package. * Try to fix components package tests. Fails because we aren't setting up the jest/jest-dom globals. * Move JS test code to reusable (private) package. * Enable incremental TS builds. * Use workspaces to run JS tests. * Use new jest configs for update snapshot scripts. * Fix style builds. * Fix package version in components. * Fix client test debug and watch scripts. * Update yarn lock. * Update test-staged behavior. * Try to fix storybook. * Fix storybook. * Update more npm commands to yarn. * Add changelog. * Fix lint errors. * Update packages readme script references. * Clean up unused gitignore match. * Fix another npm command. * Fix JS builds on watch. * Fix start script. * Fix start scripts for packages. * Use tsc to build packages before tests * yarn -> npm. # Conflicts: # package-lock.json # package.json * Fix linter error. * Remove workspace definitions. * Fix missing Fragment import. * Fix package lock. * Fix missing reference. * Only build commonjs module for js-tests helper. * Remove errant dependency from components. * Remove noop scripts. * Fix package JS build before testing. * Revert noisy formatting changes. * Fix precommit and test scripts. * Fix minimum expected recommended extension count. Japan test case breaks this. * Revert babel config changes. * chore(release): publish - @woocommerce/components@7.2.0 - @woocommerce/csv-export@1.4.0 - @woocommerce/currency@3.2.0 - @woocommerce/customer-effort-score@1.1.0 - @woocommerce/data@1.4.0 - @woocommerce/date@3.1.0 - @woocommerce/dependency-extraction-webpack-plugin@1.7.0 - @woocommerce/eslint-plugin@1.3.0 - @woocommerce/experimental@1.5.0 - @woocommerce/explat@1.1.0 - @woocommerce/js-tests@1.1.0 - @woocommerce/navigation@6.1.0 - @woocommerce/notices@3.1.0 - @woocommerce/number@2.2.0 - @woocommerce/onboarding@1.1.0 - @woocommerce/tracks@1.1.0 - @woocommerce/wc-admin-settings@1.1.0 * Add script for running 'start' in a package. * Remove yarn from gitignore. * Update package changelogs, prep versions for release. * Try to fix E2E tests after main merge. * Some cleanup. * Add changelog. Co-authored-by: Paul Sealock <psealock@gmail.com>
2021-07-14 20:38:57 +00:00
jest.mock( '../stats-overview', () =>
jest.fn().mockReturnValue( <div>[StatsOverview]</div> )
);
jest.mock( '../../inbox-panel', () =>
jest.fn().mockReturnValue( <div>[InboxPanel]</div> )
);
jest.mock( '../../store-management-links', () => ( {
StoreManagementLinks: jest
.fn()
.mockReturnValue( <div>[StoreManagementLinks]</div> ),
} ) );
jest.mock( '../activity-panel', () => ( {
ActivityPanel: jest.fn().mockReturnValue( <div>[ActivityPanel]</div> ),
} ) );
jest.mock( '@woocommerce/data', () => ( {
...jest.requireActual( '@woocommerce/data' ),
useUserPreferences: jest.fn().mockReturnValue( {} ),
} ) );
jest.mock( '@wordpress/element', () => {
return {
...jest.requireActual( '@wordpress/element' ),
Suspense: ( { children } ) => <div>{ children }</div>,
// It's not easy to mock a React.lazy component, since we only use one in this component, this mocks lazy to return a mocked <TaskList>
lazy: () => () => <div>[TaskList]</div>,
};
} );
describe( 'Homescreen Layout', () => {
it( 'should show TaskList inline', () => {
const { container } = render(
<Layout
requestingTaskList={ false }
taskListComplete
hasTaskList={ true }
query={ { page: 'wc-admin' } }
updateOptions={ () => {} }
/>
);
// Expect that we're rendering the "full" home screen (with columns).
const columns = container.querySelector(
'.woocommerce-homescreen-column'
);
expect( columns ).toBeInTheDocument();
// Expect that the <TaskList /> is there too.
const taskList = screen.getByText( '[TaskList]' );
expect( taskList ).toBeInTheDocument();
} );
it( 'should render TaskList alone when on task', () => {
const { container } = render(
<Layout
requestingTaskList={ false }
hasTaskList={ true }
query={ {
task: 'products',
} }
updateOptions={ () => {} }
/>
);
// Expect that we're NOT rendering the "full" home screen (with columns).
const columns = container.querySelector(
'.woocommerce-homescreen-column'
);
expect( columns ).not.toBeInTheDocument();
// Expect that the <TaskList /> is there though.
const taskList = screen.queryByText( '[TaskList]' );
expect( taskList ).toBeInTheDocument();
} );
it( 'should not show TaskList when user has hidden', () => {
render(
<Layout
requestingTaskList={ false }
hasTaskList={ false }
query={ { page: 'wc-admin' } }
updateOptions={ () => {} }
/>
);
const taskList = screen.queryByText( '[TaskList]' );
expect( taskList ).not.toBeInTheDocument();
} );
it( 'should show StoreManagementLinks when TaskList is complete, even if the task list is not hidden', () => {
render(
<Layout
requestingTaskList={ false }
hasTaskList={ true }
taskListComplete
query={ { page: 'wc-admin' } }
updateOptions={ () => {} }
/>
);
const storeManagementLinks = screen.queryByText(
'[StoreManagementLinks]'
);
expect( storeManagementLinks ).toBeInTheDocument();
} );
it( 'should default to layout option value', () => {
useUserPreferences.mockReturnValue( {
homepage_layout: '',
} );
const { container } = render(
<Layout
defaultHomescreenLayout="two_columns"
requestingTaskList={ false }
hasTaskList={ true }
query={ {} }
updateOptions={ () => {} }
/>
);
// Expect that we're rendering the two-column home screen.
// The user doesn't have a preference set, but the component received a prop
// defaulting to the two column view.
expect(
container.getElementsByClassName(
'woocommerce-homescreen two-columns'
)
).toHaveLength( 1 );
} );
it( 'should falback to single column layout', () => {
useUserPreferences.mockReturnValue( {
homepage_layout: '',
} );
const { container } = render(
<Layout
requestingTaskList={ false }
hasTaskList={ true }
query={ {} }
updateOptions={ () => {} }
/>
);
// Expect that we're rendering the single column home screen.
// If a default layout prop isn't specified, the default falls back to single column view.
const homescreen = container.getElementsByClassName(
'woocommerce-homescreen'
);
expect( homescreen ).toHaveLength( 1 );
expect( homescreen[ 0 ] ).not.toHaveClass( 'two-columns' );
} );
it( 'switches to two column layout based on user preference', () => {
useUserPreferences.mockReturnValue( {
homepage_layout: 'two_columns',
} );
const { container } = render(
<Layout
requestingTaskList={ false }
hasTaskList={ true }
query={ {} }
updateOptions={ () => {} }
/>
);
// Expect that we're rendering the two-column home screen.
expect(
container.getElementsByClassName(
'woocommerce-homescreen two-columns'
)
).toHaveLength( 1 );
} );
it( 'should display the correct blocks in each column', () => {
useUserPreferences.mockReturnValue( {
homepage_layout: 'two_columns',
} );
const { container } = render(
<Layout
requestingTaskList={ false }
taskListComplete
hasTaskList={ true }
query={ { page: 'wc-admin' } }
updateOptions={ () => {} }
/>
);
const columns = container.getElementsByClassName(
'woocommerce-homescreen-column'
);
expect( columns ).toHaveLength( 2 );
const firstColumn = columns[ 0 ];
const secondColumn = columns[ 1 ];
expect(
within( firstColumn ).getByText( '[TaskList]' )
).toBeInTheDocument();
expect(
within( firstColumn ).getByText( '[InboxPanel]' )
).toBeInTheDocument();
expect(
within( secondColumn ).queryByText( '[TaskList]' )
).not.toBeInTheDocument();
expect(
within( secondColumn ).queryByText( '[InboxPanel]' )
).not.toBeInTheDocument();
expect(
within( secondColumn ).getByText( '[StatsOverview]' )
).toBeInTheDocument();
expect(
within( firstColumn ).queryByText( '[StatsOverview]' )
).not.toBeInTheDocument();
} );
it( 'should display the correct blocks in each column when task list is hidden', () => {
useUserPreferences.mockReturnValue( {
homepage_layout: 'two_columns',
} );
const { container } = render(
<Layout
requestingTaskList={ false }
taskListComplete
hasTaskList={ true }
isTaskListHidden={ true }
query={ { page: 'wc-admin' } }
updateOptions={ () => {} }
/>
);
const columns = container.getElementsByClassName(
'woocommerce-homescreen-column'
);
expect( columns ).toHaveLength( 2 );
const firstColumn = columns[ 0 ];
const secondColumn = columns[ 1 ];
expect(
within( firstColumn ).getByText( '[TaskList]' )
).toBeInTheDocument();
expect(
within( firstColumn ).getByText( '[InboxPanel]' )
).toBeInTheDocument();
expect(
within( secondColumn ).queryByText( '[TaskList]' )
).not.toBeInTheDocument();
expect(
within( secondColumn ).queryByText( '[InboxPanel]' )
).not.toBeInTheDocument();
expect(
within( secondColumn ).getByText( '[StatsOverview]' )
).toBeInTheDocument();
expect(
within( secondColumn ).getByText( '[StoreManagementLinks]' )
).toBeInTheDocument();
expect(
within( firstColumn ).queryByText( '[StatsOverview]' )
).not.toBeInTheDocument();
} );
} );