2020-08-23 22:46:18 +00:00
|
|
|
|
jest.mock( '@woocommerce/wc-admin-settings', () => ( {
|
|
|
|
|
...jest.requireActual( '@woocommerce/wc-admin-settings' ),
|
|
|
|
|
getSetting() {
|
|
|
|
|
return 'Fake Site Title';
|
|
|
|
|
},
|
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
|
|
jest.mock( '@woocommerce/tracks', () => ( {
|
|
|
|
|
...jest.requireActual( '@woocommerce/tracks' ),
|
|
|
|
|
recordEvent: jest.fn(),
|
|
|
|
|
} ) );
|
|
|
|
|
|
2020-08-27 01:46:53 +00:00
|
|
|
|
jest.mock( '@woocommerce/data', () => ( {
|
|
|
|
|
...jest.requireActual( '@woocommerce/data' ),
|
|
|
|
|
useUserPreferences: () => ( {
|
|
|
|
|
updateUserPreferences: () => {},
|
|
|
|
|
// mock to disable the mobile app banner while testing this component
|
|
|
|
|
android_app_banner_dismissed: 'yes',
|
|
|
|
|
} ),
|
|
|
|
|
} ) );
|
|
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
|
jest.mock( '@wordpress/data', () => {
|
|
|
|
|
// Require the original module to not be mocked...
|
|
|
|
|
const originalModule = jest.requireActual( '@wordpress/data' );
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
__esModule: true, // Use it when dealing with esModules
|
|
|
|
|
...originalModule,
|
2021-03-05 14:35:53 +00:00
|
|
|
|
useSelect: jest.fn().mockReturnValue( {
|
|
|
|
|
menuItems: [],
|
|
|
|
|
} ),
|
2021-02-12 16:32:26 +00:00
|
|
|
|
};
|
|
|
|
|
} );
|
|
|
|
|
|
2020-02-05 19:35:50 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2020-08-23 22:46:18 +00:00
|
|
|
|
import { render, fireEvent } from '@testing-library/react';
|
2020-02-05 19:35:50 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2020-08-23 22:46:18 +00:00
|
|
|
|
import { Header } from '../index.js';
|
2020-02-05 19:35:50 +00:00
|
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
|
global.window.wcNavigation = {};
|
|
|
|
|
|
2020-02-05 19:35:50 +00:00
|
|
|
|
const encodedBreadcrumb = [
|
|
|
|
|
[ 'admin.php?page=wc-settings', 'Settings' ],
|
|
|
|
|
'Accounts & Privacy',
|
|
|
|
|
];
|
|
|
|
|
|
2021-03-03 00:05:08 +00:00
|
|
|
|
const stubLocation = ( location ) => {
|
|
|
|
|
jest.spyOn( window, 'location', 'get' ).mockReturnValue( {
|
|
|
|
|
...window.location,
|
|
|
|
|
...location,
|
|
|
|
|
} );
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-05 19:35:50 +00:00
|
|
|
|
describe( 'Header', () => {
|
2020-08-23 22:46:18 +00:00
|
|
|
|
beforeEach( () => {
|
|
|
|
|
// Mock RAF to be synchronous for testing
|
|
|
|
|
jest.spyOn( window, 'requestAnimationFrame' ).mockImplementation(
|
|
|
|
|
( cb ) => {
|
|
|
|
|
cb();
|
2020-02-14 02:23:21 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
2020-08-23 22:46:18 +00:00
|
|
|
|
|
2020-08-27 01:46:53 +00:00
|
|
|
|
// Mock user preferences to avoid testing the MobileAppBanner here
|
|
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
|
// Disable the ActivityPanel so it isn't tested here
|
|
|
|
|
window.wcAdminFeatures[ 'activity-panels' ] = false;
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
afterEach( () => {
|
|
|
|
|
window.requestAnimationFrame.mockRestore();
|
|
|
|
|
} );
|
|
|
|
|
|
2021-03-03 00:05:08 +00:00
|
|
|
|
it( 'should render a back button and a custom title on task list pages', () => {
|
|
|
|
|
stubLocation( { href: 'http://localhost?task=payments' } );
|
|
|
|
|
|
|
|
|
|
const { queryByTestId, queryByText } = render(
|
|
|
|
|
<Header sections={ encodedBreadcrumb } isEmbedded={ false } />
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
queryByTestId( 'header-back-button' )
|
|
|
|
|
).not.toBeEmptyDOMElement();
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
queryByText( 'Choose payment methods' )
|
|
|
|
|
).not.toBeEmptyDOMElement();
|
|
|
|
|
} );
|
|
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
|
it( 'should render decoded breadcrumb name', () => {
|
|
|
|
|
const { queryByText } = render(
|
|
|
|
|
<Header sections={ encodedBreadcrumb } isEmbedded={ true } />
|
2020-02-14 02:23:21 +00:00
|
|
|
|
);
|
2020-08-23 22:46:18 +00:00
|
|
|
|
expect( queryByText( 'Accounts & Privacy' ) ).toBe( null );
|
|
|
|
|
expect( queryByText( 'Accounts & Privacy' ) ).not.toBe( null );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
it( 'should only have the is-scrolled class if the page is scrolled', () => {
|
|
|
|
|
const { container } = render(
|
|
|
|
|
<Header sections={ encodedBreadcrumb } isEmbedded={ false } />
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const topLevelElement = container.firstChild;
|
2020-11-03 00:57:49 +00:00
|
|
|
|
expect( Object.values( topLevelElement.classList ) ).not.toContain(
|
|
|
|
|
'is-scrolled'
|
|
|
|
|
);
|
|
|
|
|
Object.defineProperty( window, 'pageYOffset', {
|
|
|
|
|
value: 200,
|
|
|
|
|
writable: false,
|
|
|
|
|
} );
|
2020-08-23 22:46:18 +00:00
|
|
|
|
fireEvent.scroll( window, { target: { scrollY: 200 } } );
|
2020-11-03 00:57:49 +00:00
|
|
|
|
expect( Object.values( topLevelElement.classList ) ).toContain(
|
|
|
|
|
'is-scrolled'
|
|
|
|
|
);
|
2020-08-23 22:46:18 +00:00
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
it( 'correctly updates the document title to reflect the navigation state', () => {
|
|
|
|
|
render(
|
|
|
|
|
<Header sections={ encodedBreadcrumb } isEmbedded={ false } />
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect( document.title ).toBe(
|
|
|
|
|
'Accounts & Privacy ‹ Settings ‹ Fake Site Title — WooCommerce'
|
|
|
|
|
);
|
|
|
|
|
} );
|
2020-02-05 19:35:50 +00:00
|
|
|
|
} );
|