forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add unit tests for ImportModeControl component
Signed-off-by: kishor82 <[email protected]>
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...ins/console/public/application/components/__snapshots__/import_mode_control.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
src/plugins/console/public/application/components/import_mode_control.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); |