2020-08-13 02:05:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-08-05 22:02:24 +00:00
|
|
|
import { render, fireEvent } from '@testing-library/react';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
2020-08-13 02:05:22 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-05 22:02:24 +00:00
|
|
|
import { Tabs } from '../';
|
|
|
|
|
2020-08-20 04:59:52 +00:00
|
|
|
jest.mock( '@woocommerce/tracks', () => ( { recordEvent: jest.fn() } ) );
|
2020-08-05 22:02:24 +00:00
|
|
|
const generateTabs = () => {
|
|
|
|
return [ '0', '1', '2', '3' ].map( ( name ) => ( {
|
|
|
|
name,
|
|
|
|
title: `Tab ${ name }`,
|
|
|
|
icon: <span>icon</span>,
|
|
|
|
unread: false,
|
|
|
|
} ) );
|
|
|
|
};
|
|
|
|
|
2020-11-16 13:48:50 +00:00
|
|
|
const CustomTab = () => {
|
|
|
|
return <div>Custom Tab</div>;
|
|
|
|
};
|
|
|
|
|
2020-08-05 22:02:24 +00:00
|
|
|
describe( 'Activity Panel Tabs', () => {
|
|
|
|
it( 'correctly tracks the selected tab', () => {
|
|
|
|
const { getAllByRole } = render(
|
|
|
|
<Tabs
|
|
|
|
selectedTab={ '3' }
|
|
|
|
tabs={ generateTabs() }
|
|
|
|
onTabClick={ () => {} }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const tabs = getAllByRole( 'tab' );
|
|
|
|
|
|
|
|
fireEvent.click( tabs[ 2 ] );
|
|
|
|
|
|
|
|
expect( tabs[ 2 ] ).toHaveClass( 'is-active' );
|
|
|
|
|
|
|
|
fireEvent.click( tabs[ 3 ] );
|
|
|
|
|
|
|
|
expect( tabs[ 2 ] ).not.toHaveClass( 'is-active' );
|
|
|
|
expect( tabs[ 3 ] ).toHaveClass( 'is-active' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'closes a tab if its the same one last opened', () => {
|
|
|
|
const { getAllByRole } = render(
|
|
|
|
<Tabs
|
|
|
|
selectedTab={ '3' }
|
|
|
|
tabs={ generateTabs() }
|
|
|
|
onTabClick={ () => {} }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const tabs = getAllByRole( 'tab' );
|
|
|
|
|
|
|
|
fireEvent.click( tabs[ 2 ] );
|
|
|
|
expect( tabs[ 2 ] ).toHaveClass( 'is-active' );
|
|
|
|
fireEvent.click( tabs[ 2 ] );
|
|
|
|
expect( tabs[ 2 ] ).not.toHaveClass( 'is-active' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'triggers onTabClick with the selected when a tab is clicked', () => {
|
|
|
|
const tabClickSpy = jest.fn();
|
|
|
|
const generatedTabs = generateTabs();
|
|
|
|
|
|
|
|
const { getAllByRole } = render(
|
|
|
|
<Tabs
|
|
|
|
selectedTab={ '3' }
|
|
|
|
tabs={ generatedTabs }
|
|
|
|
onTabClick={ tabClickSpy }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const tabs = getAllByRole( 'tab' );
|
|
|
|
|
|
|
|
fireEvent.click( tabs[ 3 ] );
|
|
|
|
|
|
|
|
expect( tabClickSpy ).toHaveBeenCalledWith( generatedTabs[ 3 ], true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'records an event when panels are being opened and when the open panel changes', () => {
|
|
|
|
const generatedTabs = generateTabs();
|
|
|
|
|
|
|
|
const { getAllByRole } = render(
|
|
|
|
<Tabs
|
|
|
|
selectedTab={ '3' }
|
|
|
|
tabs={ generatedTabs }
|
|
|
|
onTabClick={ () => {} }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const tabs = getAllByRole( 'tab' );
|
|
|
|
|
|
|
|
fireEvent.click( tabs[ 3 ] );
|
|
|
|
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith( 'activity_panel_open', {
|
|
|
|
tab: generatedTabs[ 3 ].name,
|
|
|
|
} );
|
|
|
|
} );
|
2020-11-16 13:48:50 +00:00
|
|
|
|
|
|
|
it( 'should render tabs with a custom component defined in tab config', () => {
|
|
|
|
const generatedTabs = generateTabs();
|
|
|
|
generatedTabs.push( {
|
|
|
|
component: CustomTab,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { getByText } = render(
|
|
|
|
<Tabs tabs={ generatedTabs } onTabClick={ () => {} } />
|
|
|
|
);
|
|
|
|
expect( getByText( 'Custom Tab' ) ).toBeDefined();
|
|
|
|
} );
|
2020-08-05 22:02:24 +00:00
|
|
|
} );
|