Skip to content

Commit

Permalink
Set up test_helpers for inspecting pageHeaders
Browse files Browse the repository at this point in the history
- primarily useful for rightSideItems, which often contain conditional logic
  • Loading branch information
cee-chen committed Jun 15, 2021
1 parent d1861c0 commit 1675342
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { Fragment } from 'react';

import { shallow, ShallowWrapper } from 'enzyme';

import { EuiPageHeaderProps } from '@elastic/eui';

/*
* Given an AppSearchPageTemplate or WorkplaceSearchPageTemplate, these
* helpers dive into various parts of the EuiPageHeader to make assertions
* slightly less of a pain in shallow renders
*/

export const getPageHeader = (wrapper: ShallowWrapper) => {
const pageHeader = wrapper.prop('pageHeader') as EuiPageHeaderProps;
return pageHeader || {};
};

export const getPageTitle = (wrapper: ShallowWrapper) => {
return getPageHeader(wrapper).pageTitle;
};

export const getPageDescription = (wrapper: ShallowWrapper) => {
return getPageHeader(wrapper).description;
};

export const getPageHeaderActions = (wrapper: ShallowWrapper) => {
const actions = getPageHeader(wrapper).rightSideItems || [];

return shallow(
<div>
{actions.map((action: React.ReactNode, i) => (
<Fragment key={i}>{action}</Fragment>
))}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export { mountAsync } from './mount_async';
export { mountWithIntl } from './mount_with_i18n';
export { shallowWithIntl } from './shallow_with_i18n';
export { rerender } from './enzyme_rerender';
export {
getPageHeader,
getPageTitle,
getPageDescription,
getPageHeaderActions,
} from './get_page_header';

// Misc
export { expectedAsyncError } from './expected_async_error';

0 comments on commit 1675342

Please sign in to comment.