Skip to content

Commit

Permalink
Moves the rest of the files and tests into the kbn-package and adds K…
Browse files Browse the repository at this point in the history
…ibana tickets to all the TODO blocks
  • Loading branch information
FrankHassanabad committed Jul 20, 2021
1 parent 1c6c6aa commit cd203f0
Show file tree
Hide file tree
Showing 43 changed files with 1,808 additions and 1,799 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-securitysolution-autocomplete/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SOURCE_FILES = glob(
exclude = [
"**/*.test.*",
"**/*.mock.*",
"**/*.mocks.*",
],
)

Expand All @@ -34,6 +35,7 @@ SRC_DEPS = [
"//packages/kbn-dev-utils",
"//packages/kbn-i18n",
"//packages/kbn-securitysolution-io-ts-list-types",
"//packages/kbn-securitysolution-list-hooks",
"@npm//@babel/core",
"@npm//babel-loader",
"@npm//@elastic/eui",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

// Copied from "src/plugins/data/public/mocks.ts" but without any type information
// TODO: Remove this in favor of the data/public/mocks if/when they become available, https://github.com/elastic/kibana/issues/100715
export const autocompleteStartMock = {
getQuerySuggestions: jest.fn(),
getValueSuggestions: jest.fn(),
hasQuerySuggestions: jest.fn(),
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,44 @@
* Side Public License, v 1.
*/

describe('use_field_value_autocomplete', () => {
test('Tests should be ported', () => {
// TODO: Port all the tests from: x-pack/plugins/lists/public/exceptions/components/autocomplete/helpers.ts here once mocks are figured out and kbn package mocks are figured out
expect(true).toBe(true);
import { checkEmptyValue } from '.';
import { getField } from '../fields/index.mock';
import * as i18n from '../translations';

describe('check_empty_value', () => {
test('returns no errors if no field has been selected', () => {
const isValid = checkEmptyValue('', undefined, true, false);

expect(isValid).toBeUndefined();
});

test('returns error string if user has touched a required input and left empty', () => {
const isValid = checkEmptyValue(undefined, getField('@timestamp'), true, true);

expect(isValid).toEqual(i18n.FIELD_REQUIRED_ERR);
});

test('returns no errors if required input is empty but user has not yet touched it', () => {
const isValid = checkEmptyValue(undefined, getField('@timestamp'), true, false);

expect(isValid).toBeUndefined();
});

test('returns no errors if user has touched an input that is not required and left empty', () => {
const isValid = checkEmptyValue(undefined, getField('@timestamp'), false, true);

expect(isValid).toBeUndefined();
});

test('returns no errors if user has touched an input that is not required and left empty string', () => {
const isValid = checkEmptyValue('', getField('@timestamp'), false, true);

expect(isValid).toBeUndefined();
});

test('returns null if input value is not empty string or undefined', () => {
const isValid = checkEmptyValue('hellooo', getField('@timestamp'), false, true);

expect(isValid).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
* Side Public License, v 1.
*/

import * as i18n from './translations';
import * as i18n from '../translations';

// TODO: I have to use any here for now, but once this is available below, we should use the correct types
// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType } from '../../../../../../../src/plugins/data/common';
type IFieldType = any;

/**
* Determines if empty value is ok
* There is a copy within:
* x-pack/plugins/lists/public/exceptions/components/autocomplete/helpers.ts
*
* TODO: This should be in its own packaged and not copied, https://github.com/elastic/kibana/issues/105378
*/
export const checkEmptyValue = (
param: string | undefined,
Expand Down

This file was deleted.

135 changes: 132 additions & 3 deletions packages/kbn-securitysolution-autocomplete/src/field/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,138 @@
* Side Public License, v 1.
*/

import React from 'react';
import { mount } from 'enzyme';
import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import { FieldComponent } from '.';
import { fields, getField } from '../fields/index.mock';

describe('field', () => {
test('Tests should be ported', () => {
// TODO: Port all the tests from: x-pack/plugins/lists/public/exceptions/components/autocomplete/field.test.tsx here once mocks are figured out and kbn package mocks are figured out
expect(true).toBe(true);
test('it renders disabled if "isDisabled" is true', () => {
const wrapper = mount(
<FieldComponent
isClearable={false}
isDisabled={true}
isLoading={false}
indexPattern={{
fields,
id: '1234',
title: 'logstash-*',
}}
onChange={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('machine.os.raw')}
/>
);

expect(
wrapper.find(`[data-test-subj="fieldAutocompleteComboBox"] input`).prop('disabled')
).toBeTruthy();
});

test('it renders loading if "isLoading" is true', () => {
const wrapper = mount(
<FieldComponent
indexPattern={{
fields,
id: '1234',
title: 'logstash-*',
}}
isClearable={false}
isDisabled={false}
isLoading={true}
onChange={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('machine.os.raw')}
/>
);
wrapper.find(`[data-test-subj="fieldAutocompleteComboBox"] button`).at(0).simulate('click');
expect(
wrapper
.find(`EuiComboBoxOptionsList[data-test-subj="fieldAutocompleteComboBox-optionsList"]`)
.prop('isLoading')
).toBeTruthy();
});

test('it allows user to clear values if "isClearable" is true', () => {
const wrapper = mount(
<FieldComponent
indexPattern={{
fields,
id: '1234',
title: 'logstash-*',
}}
isClearable={true}
isDisabled={false}
isLoading={false}
onChange={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('machine.os.raw')}
/>
);

expect(
wrapper
.find(`[data-test-subj="comboBoxInput"]`)
.hasClass('euiComboBox__inputWrap-isClearable')
).toBeTruthy();
});

test('it correctly displays selected field', () => {
const wrapper = mount(
<FieldComponent
indexPattern={{
fields,
id: '1234',
title: 'logstash-*',
}}
isClearable={false}
isDisabled={false}
isLoading={false}
onChange={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('machine.os.raw')}
/>
);

expect(
wrapper.find(`[data-test-subj="fieldAutocompleteComboBox"] EuiComboBoxPill`).at(0).text()
).toEqual('machine.os.raw');
});

test('it invokes "onChange" when option selected', () => {
const mockOnChange = jest.fn();
const wrapper = mount(
<FieldComponent
indexPattern={{
fields,
id: '1234',
title: 'logstash-*',
}}
isClearable={false}
isDisabled={false}
isLoading={false}
onChange={mockOnChange}
placeholder="Placeholder text"
selectedField={getField('machine.os.raw')}
/>
);

((wrapper.find(EuiComboBox).props() as unknown) as {
onChange: (a: EuiComboBoxOptionOption[]) => void;
}).onChange([{ label: 'machine.os' }]);

expect(mockOnChange).toHaveBeenCalledWith([
{
aggregatable: true,
count: 0,
esTypes: ['text'],
name: 'machine.os',
readFromDocValues: false,
scripted: false,
searchable: true,
type: 'string',
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React, { useCallback, useMemo, useState } from 'react';
import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';

// TODO: I have to use any here for now, but once this is available below, we should use the correct types
// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType, IIndexPattern } from '../../../../../../../../src/plugins/data/common';
type IFieldType = any;
type IIndexPattern = any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { mount } from 'enzyme';

import { AutocompleteFieldExistsComponent } from './field_value_exists';
import { AutocompleteFieldExistsComponent } from '.';

describe('AutocompleteFieldExistsComponent', () => {
test('it renders field disabled', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
Expand All @@ -11,15 +12,20 @@ import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import { waitFor } from '@testing-library/react';
import type { ListSchema } from '@kbn/securitysolution-io-ts-list-types';

import { coreMock } from '../../../../../../../src/core/public/mocks';
import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { getFoundListSchemaMock } from '../../../../../lists/common/schemas/response/found_list_schema.mock';
import { getListResponseMock } from '../../../../../lists/common/schemas/response/list_schema.mock';
import { DATE_NOW, IMMUTABLE, VERSION } from '../../../../../lists/common/constants.mock';

import { AutocompleteFieldListsComponent } from './field_value_lists';

const mockKibanaHttpService = coreMock.createStart().http;
import { getField } from '../fields/index.mock';
import { AutocompleteFieldListsComponent } from '.';
import {
getListResponseMock,
getFoundListSchemaMock,
DATE_NOW,
IMMUTABLE,
VERSION,
} from '../list_schema/index.mock';

// TODO: Once these mocks are available, use them instead of hand mocking, https://github.com/elastic/kibana/issues/100715
// const mockKibanaHttpService = coreMock.createStart().http;
// import { coreMock } from '../../../../../../../src/core/public/mocks';
const mockKibanaHttpService = jest.fn();

const mockStart = jest.fn();
const mockKeywordList: ListSchema = {
Expand All @@ -35,7 +41,6 @@ jest.mock('@kbn/securitysolution-list-hooks', () => {

return {
...originalModule,
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
useFindLists: () => ({
error: undefined,
loading: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow } from '@elastic/eui';
import { HttpStart } from 'kibana/public';
import type { ListSchema } from '@kbn/securitysolution-io-ts-list-types';
import { useFindLists } from '@kbn/securitysolution-list-hooks';
import { getGenericComboBoxProps } from '@kbn/securitysolution-autocomplete';

import { IFieldType } from '../../../../../../../src/plugins/data/common';
import { filterFieldToList } from '../filter_field_to_list';
import { getGenericComboBoxProps } from '../get_generic_combo_box_props';

import { filterFieldToList } from './helpers';
import * as i18n from './translations';
// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType } from '../../../../../../../src/plugins/data/common';
type IFieldType = any;

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/100715
// import { HttpStart } from 'kibana/public';
type HttpStart = any;

import * as i18n from '../translations';

const SINGLE_SELECTION = { asPlainText: true };

Expand Down
Loading

0 comments on commit cd203f0

Please sign in to comment.