Skip to content

Commit

Permalink
test: Add unit tests for ImportModeControl component
Browse files Browse the repository at this point in the history
Signed-off-by: kishor82 <[email protected]>
  • Loading branch information
kishor82 committed Aug 1, 2023
1 parent c32b238 commit 76b4c86
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { act } from '@testing-library/react-hooks';
import { ShallowWrapper, shallow } from 'enzyme';
import { shallowWithIntl } from 'test_utils/enzyme_helpers';
import { ImportModeControl } from './import_mode_control';
import { EuiFormLegendProps, EuiRadioGroupProps } from '@elastic/eui';
import { ReactElement } from 'react';

const radioGroupIdentifier = 'EuiRadioGroup';

describe('ImportModeControl Component', () => {
let component: ShallowWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;
const mockUpdateSelection = jest.fn();

beforeEach(async () => {
jest.clearAllMocks();
await act(async () => {
component = await shallowWithIntl(
<ImportModeControl
initialValues={{ overwrite: false }}
updateSelection={mockUpdateSelection}
/>
);
});
component.update();
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should render correclty', () => {
expect(component).toMatchSnapshot();
});

it('should render the correct title in the fieldset legend', () => {
const legendText = 'Import options';
const legend: EuiFormLegendProps = component.find('EuiFormFieldset').prop('legend');
const legendTitle = shallow(legend?.children as ReactElement);

expect(legendTitle.text()).toBe(legendText);
});

it('should display the correct labels for radio options', () => {
const componentProps = (component
.find(radioGroupIdentifier)
.props() as unknown) as EuiRadioGroupProps;

// Check if the labels for radio options are displayed correctly
const radioOptions = componentProps.options;
expect(radioOptions[0].label).toBe('Merge with existing queries');
expect(radioOptions[1].label).toBe('Overwrite existing queries');

// Check the initial selection (overwrite is false, so Merge with existing queries should be selected)
const selectedOptionId = component.find(radioGroupIdentifier).prop('idSelected');
expect(selectedOptionId).toBe('overwriteDisabled');
});

it('should call updateSelection when the selection is changed', async () => {
await act(async () => {
// @ts-ignore
await component.find(radioGroupIdentifier).first().props().onChange('overwriteEnabled');
});
component.update();

// Expect that the updateSelection function has been called with the correct parameters
expect(mockUpdateSelection).toHaveBeenCalledWith({ overwrite: true });
});
});

0 comments on commit 76b4c86

Please sign in to comment.