Skip to content

Commit

Permalink
add test for sorting
Browse files Browse the repository at this point in the history
Signed-off-by: Eric <menwe@amazon.com>
  • Loading branch information
mengweieric committed Jan 23, 2024
1 parent 023b463 commit 0b54a17
Showing 1 changed file with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import React from 'react';
import { render, act } from '@testing-library/react';
import { render, act, screen, fireEvent } from '@testing-library/react';
import { DataSourceSelectable } from './datasource_selectable';
import { DataSourceType, GenericDataSource } from '../datasource_services';
import { DataSourceGroup, DataSourceOption } from './types';
Expand Down Expand Up @@ -81,4 +81,114 @@ describe('DataSourceSelectable', () => {

expect(onFetchDataSetErrorMock).toHaveBeenCalledWith(new Error('Fetch error'));
});

it('should sort index patterns list alphabetically', async () => {
const mockDataSourceOptionList = [
{
label: 'Index patterns',
options: [
{ label: 'logstash-*' },
{ label: '000-*' },
{ label: 'p000-1' },
{ label: 'pattern_archive' },
{ label: 'index_main' },
{ label: 'index-2024' },
],
},
] as any;

render(
<DataSourceSelectable
dataSources={[
({
getDataSet: jest.fn().mockResolvedValue([]),
getType: jest.fn().mockReturnValue('DEFAULT_INDEX_PATTERNS'),
getName: jest.fn().mockReturnValue('Index patterns'),
} as unknown) as DataSourceType,
({
getDataSet: jest.fn().mockResolvedValue([]),
getType: jest.fn().mockReturnValue('s3glue'),
getName: jest.fn().mockReturnValue('Amazon S3'),
} as unknown) as DataSourceType,
]}
dataSourceOptionList={mockDataSourceOptionList}
selectedSources={selectedSourcesMock}
onDataSourceSelect={setSelectedSourcesMock}
setDataSourceOptionList={setDataSourceOptionListMock}
onGetDataSetError={onFetchDataSetErrorMock}
/>
);

const button = screen.getByLabelText('Open list of options');
fireEvent.click(button);
expect(
screen.getByTestId('comboBoxOptionsList dataExplorerDSSelect-optionsList')
).toBeInTheDocument();
const defaultDSOptions = document.querySelectorAll('.euiComboBoxOption__content');
const optionTexts = Array.from(defaultDSOptions).map((option) => option.innerHTML);
const expectedIndexPatternSortedOrder = [
'000-*',
'index_main',
'index-2024',
'logstash-*',
'p000-1',
'pattern_archive',
];
expect(optionTexts).toEqual(expectedIndexPatternSortedOrder);
});

it('should sort non index patterns list alphabetically', async () => {
const mockDataSourceOptionList = [
{
label: 'Amazon S3',
options: [
{ label: 'mys3' },
{ label: '*starred' },
{ label: 'alpha-test-s3' },
{ label: '@special' },
{ label: 's3-2024' },
{ label: 'S3_Archive' },
],
},
] as any;

render(
<DataSourceSelectable
dataSources={[
({
getDataSet: jest.fn().mockResolvedValue([]),
getType: jest.fn().mockReturnValue('DEFAULT_INDEX_PATTERNS'),
getName: jest.fn().mockReturnValue('Index patterns'),
} as unknown) as DataSourceType,
({
getDataSet: jest.fn().mockResolvedValue([]),
getType: jest.fn().mockReturnValue('s3glue'),
getName: jest.fn().mockReturnValue('Amazon S3'),
} as unknown) as DataSourceType,
]}
dataSourceOptionList={mockDataSourceOptionList}
selectedSources={selectedSourcesMock}
onDataSourceSelect={setSelectedSourcesMock}
setDataSourceOptionList={setDataSourceOptionListMock}
onGetDataSetError={onFetchDataSetErrorMock}
/>
);

const button = screen.getByLabelText('Open list of options');
fireEvent.click(button);
expect(
screen.getByTestId('comboBoxOptionsList dataExplorerDSSelect-optionsList')
).toBeInTheDocument();
const defaultDSOptions = document.querySelectorAll('.euiComboBoxOption__content');
const optionTexts = Array.from(defaultDSOptions).map((option) => option.innerHTML);
const expectedIndexPatternSortedOrder = [
'@special',
'*starred',
'alpha-test-s3',
'mys3',
'S3_Archive',
's3-2024',
];
expect(optionTexts).toEqual(expectedIndexPatternSortedOrder);
});
});

0 comments on commit 0b54a17

Please sign in to comment.