Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed Apr 19, 2024
1 parent 424b058 commit a88156e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/core/public/chrome/nav_controls/nav_controls_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@

import { NavControlsService } from './nav_controls_service';
import { take } from 'rxjs/operators';
import { applicationServiceMock, httpServiceMock } from '../../../../core/public/mocks';

const mockMountReactNode = jest.fn().mockReturnValue('mock mount point');
jest.mock('../../utils', () => ({
mountReactNode: () => mockMountReactNode(),
}));

describe('RecentlyAccessed#start()', () => {
const getStart = () => {
Expand Down Expand Up @@ -76,6 +82,45 @@ describe('RecentlyAccessed#start()', () => {
});
});

describe('top right navigation', () => {
const mockProps = {
application: applicationServiceMock.createStartContract(),
http: httpServiceMock.createStartContract(),
appId: 'app_id',
iconType: 'icon',
title: 'title',
order: 1,
};
it('allows registration', async () => {
const navControls = getStart();
navControls.registerRightNavigation(mockProps);
expect(await navControls.getRight$().pipe(take(1)).toPromise()).toEqual([
{
mount: 'mock mount point',
order: 1,
},
]);
});

it('sorts controls by order property', async () => {
const navControls = getStart();
const props1 = { ...mockProps, order: 10 };
const props2 = { ...mockProps, order: 0 };
navControls.registerRightNavigation(props1);
navControls.registerRightNavigation(props2);
expect(await navControls.getRight$().pipe(take(1)).toPromise()).toEqual([
{
mount: 'mock mount point',
order: 0,
},
{
mount: 'mock mount point',
order: 10,
},
]);
});
});

describe('center controls', () => {
it('allows registration', async () => {
const navControls = getStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface ChromeNavControls {
registerRight(navControl: ChromeNavControl): void;
/** Register a nav control to be presented on the top-center side of the chrome header. */
registerCenter(navControl: ChromeNavControl): void;
/** Register a nav control to be presented on the top-right side of the chrome header. The component and style will be maintained in chrome */
/** Register a nav control to be presented on the top-right side of the chrome header. The component and style will be uniformly maintained in chrome */
registerRightNavigation(props: RightNavigationProps): void;
/** @internal */
getLeft$(): Observable<ChromeNavControl[]>;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions src/core/public/chrome/ui/header/right_navigation_button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import { RightNavigationButton } from './right_navigation_button';
import { applicationServiceMock, httpServiceMock } from '../../../../../core/public/mocks';

const mockProps = {
application: applicationServiceMock.createStartContract(),
http: httpServiceMock.createStartContract(),
appId: 'app_id',
iconType: 'mock_icon',
title: 'title',
};

describe('Right navigation button', () => {
it('should render base element normally', () => {
const { baseElement } = render(<RightNavigationButton {...mockProps} />);
expect(baseElement).toMatchSnapshot();
});

it('should call application getUrlForApp and navigateToUrl after clicked', () => {
const navigateToUrl = jest.fn();
const getUrlForApp = jest.fn();
const props = {
...mockProps,
application: {
...applicationServiceMock.createStartContract(),
getUrlForApp,
navigateToUrl,
},
};
const { getByTestId } = render(<RightNavigationButton {...props} />);
const icon = getByTestId('rightNavigationButton');
fireEvent.click(icon);
expect(getUrlForApp).toHaveBeenCalledWith('app_id', {
path: '/',
absolute: false,
});
expect(navigateToUrl).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const RightNavigationButton = ({
return (
<EuiButtonIcon
iconType={iconType}
data-test-subj="rightNavigationButton"
aria-label={title}
title={title}
onClick={navigateToApp}
Expand Down

0 comments on commit a88156e

Please sign in to comment.