Skip to content

Commit

Permalink
Fix broken Jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Nov 28, 2023
1 parent f7a5291 commit b47fcce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React, { ReactElement } from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { DiscoverTopNav, DiscoverTopNavProps, ServerlessTopNav } from './discover_topnav';
import { DiscoverTopNav, DiscoverTopNavProps } from './discover_topnav';
import { TopNavMenu, TopNavMenuData } from '@kbn/navigation-plugin/public';
import { setHeaderActionMenuMounter } from '../../../../kibana_services';
import { discoverServiceMock as mockDiscoverService } from '../../../../__mocks__/services';
Expand All @@ -19,7 +19,6 @@ import type { SearchBarCustomization, TopNavCustomization } from '../../../../cu
import type { DiscoverCustomizationId } from '../../../../customizations/customization_service';
import { useDiscoverCustomization } from '../../../../customizations';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { LogExplorerTabs } from '../../../../components/log_explorer_tabs';

setHeaderActionMenuMounter(jest.fn());

Expand Down Expand Up @@ -78,9 +77,7 @@ function getProps(
stateContainer,
savedQuery: '',
updateQuery: jest.fn(),
onOpenInspector: jest.fn(),
onFieldEdited: jest.fn(),
isPlainRecord: false,
};
}

Expand Down Expand Up @@ -285,22 +282,21 @@ describe('Discover topnav component', () => {
});
});

describe('ServerlessTopNav', () => {
it('should not render when serverless plugin is not defined', () => {
describe('serverless', () => {
it('should render top nav when serverless plugin is not defined', () => {
const props = getProps();
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(0);
const searchBar = component.find(mockDiscoverService.navigation.ui.AggregateQueryTopNavMenu);
expect(searchBar.prop('badges')).toBeDefined();
expect(searchBar.prop('config')).toBeDefined();
expect(searchBar.prop('setMenuMountPoint')).toBeDefined();
});

it('should render when serverless plugin is defined and displayMode is "standalone"', () => {
it('should not render top nav when serverless plugin is defined', () => {
mockUseKibana.mockReturnValue({
services: {
...mockDiscoverService,
Expand All @@ -313,69 +309,10 @@ describe('Discover topnav component', () => {
<DiscoverTopNav {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(1);
const searchBar = component.find(mockDiscoverService.navigation.ui.AggregateQueryTopNavMenu);
expect(searchBar.prop('badges')).toBeUndefined();
expect(searchBar.prop('config')).toBeUndefined();
expect(searchBar.prop('setMenuMountPoint')).toBeUndefined();
});

it('should not render when serverless plugin is defined and displayMode is not "standalone"', () => {
mockUseKibana.mockReturnValue({
services: {
...mockDiscoverService,
serverless: true,
},
});
const props = getProps();
props.stateContainer.customizationContext.displayMode = 'embedded';
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(0);
const searchBar = component.find(mockDiscoverService.navigation.ui.AggregateQueryTopNavMenu);
expect(searchBar.prop('badges')).toBeUndefined();
expect(searchBar.prop('config')).toBeUndefined();
expect(searchBar.prop('setMenuMountPoint')).toBeUndefined();
});

describe('LogExplorerTabs', () => {
it('should render when showLogExplorerTabs is true', () => {
mockUseKibana.mockReturnValue({
services: {
...mockDiscoverService,
serverless: true,
},
});
const props = getProps();
props.stateContainer.customizationContext.showLogExplorerTabs = true;
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(1);
expect(component.find(LogExplorerTabs)).toHaveLength(1);
});

it('should not render when showLogExplorerTabs is false', () => {
mockUseKibana.mockReturnValue({
services: {
...mockDiscoverService,
serverless: true,
},
});
const props = getProps();
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(1);
expect(component.find(LogExplorerTabs)).toHaveLength(0);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,48 @@
* Side Public License, v 1.
*/

import React from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { DiscoverTopNavServerless } from './discover_topnav_serverless';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { discoverServiceMock as mockDiscoverService } from '../../../../__mocks__/services';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { LogExplorerTabs } from '../../../../components/log_explorer_tabs';

jest.mock('@kbn/kibana-react-plugin/public', () => ({
...jest.requireActual('@kbn/kibana-react-plugin/public'),
useKibana: jest.fn(),
}));

function getProps() {
const stateContainer = getDiscoverStateMock({ isTimeBased: true });
stateContainer.internalState.transitions.setDataView(dataViewMock);

return {
stateContainer,
};
}

const mockUseKibana = useKibana as jest.Mock;

describe('DiscoverTopNavServerless', () => {
beforeEach(() => {
jest.clearAllMocks();
mockUseKibana.mockReturnValue({
services: mockDiscoverService,
});
});

describe('ServerlessTopNav', () => {
it('should not render when serverless plugin is not defined', () => {
const props = getProps();
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
<DiscoverTopNavServerless {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(0);
const searchBar = component.find(mockDiscoverService.navigation.ui.AggregateQueryTopNavMenu);
expect(searchBar.prop('badges')).toBeDefined();
expect(searchBar.prop('config')).toBeDefined();
expect(searchBar.prop('setMenuMountPoint')).toBeDefined();
expect(component.find(DiscoverTopNavServerless).isEmptyRender()).toBe(true);
});

it('should render when serverless plugin is defined and displayMode is "standalone"', () => {
Expand All @@ -33,14 +60,10 @@ describe('ServerlessTopNav', () => {
const props = getProps();
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
<DiscoverTopNavServerless {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(1);
const searchBar = component.find(mockDiscoverService.navigation.ui.AggregateQueryTopNavMenu);
expect(searchBar.prop('badges')).toBeUndefined();
expect(searchBar.prop('config')).toBeUndefined();
expect(searchBar.prop('setMenuMountPoint')).toBeUndefined();
expect(component.find(DiscoverTopNavServerless).isEmptyRender()).toBe(false);
});

it('should not render when serverless plugin is defined and displayMode is not "standalone"', () => {
Expand All @@ -54,14 +77,10 @@ describe('ServerlessTopNav', () => {
props.stateContainer.customizationContext.displayMode = 'embedded';
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
<DiscoverTopNavServerless {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(0);
const searchBar = component.find(mockDiscoverService.navigation.ui.AggregateQueryTopNavMenu);
expect(searchBar.prop('badges')).toBeUndefined();
expect(searchBar.prop('config')).toBeUndefined();
expect(searchBar.prop('setMenuMountPoint')).toBeUndefined();
expect(component.find(DiscoverTopNavServerless).isEmptyRender()).toBe(true);
});

describe('LogExplorerTabs', () => {
Expand All @@ -76,10 +95,10 @@ describe('ServerlessTopNav', () => {
props.stateContainer.customizationContext.showLogExplorerTabs = true;
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
<DiscoverTopNavServerless {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(1);
expect(component.find(DiscoverTopNavServerless)).toHaveLength(1);
expect(component.find(LogExplorerTabs)).toHaveLength(1);
});

Expand All @@ -93,10 +112,10 @@ describe('ServerlessTopNav', () => {
const props = getProps();
const component = mountWithIntl(
<DiscoverMainProvider value={props.stateContainer}>
<DiscoverTopNav {...props} />
<DiscoverTopNavServerless {...props} />
</DiscoverMainProvider>
);
expect(component.find(ServerlessTopNav)).toHaveLength(1);
expect(component.find(DiscoverTopNavServerless)).toHaveLength(1);
expect(component.find(LogExplorerTabs)).toHaveLength(0);
});
});
Expand Down

0 comments on commit b47fcce

Please sign in to comment.