forked from elastic/kibana
-
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.
[Discover] Small fixes for types and memo deps. Add tests.
- Loading branch information
Showing
7 changed files
with
243 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
80 changes: 80 additions & 0 deletions
80
packages/kbn-unified-field-list/src/hooks/use_new_fields.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,80 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { stubLogstashDataView as dataView } from '@kbn/data-views-plugin/common/data_view.stub'; | ||
import { DataViewField } from '@kbn/data-views-plugin/common'; | ||
import { useNewFields, type UseNewFieldsParams } from './use_new_fields'; | ||
import { type ExistingFieldsReader } from './use_existing_fields'; | ||
import { ExistenceFetchStatus } from '../types'; | ||
|
||
const fieldsExistenceReader: ExistingFieldsReader = { | ||
hasFieldData: (dataViewId) => { | ||
return dataViewId === dataView.id; | ||
}, | ||
getFieldsExistenceStatus: (dataViewId) => | ||
dataViewId === dataView.id ? ExistenceFetchStatus.succeeded : ExistenceFetchStatus.unknown, | ||
isFieldsExistenceInfoUnavailable: (dataViewId) => dataViewId !== dataView.id, | ||
getNewFields: () => [], | ||
}; | ||
|
||
describe('UnifiedFieldList useNewFields()', () => { | ||
const allFields = dataView.fields; | ||
|
||
it('should work correctly in loading state', async () => { | ||
const props: UseNewFieldsParams<DataViewField> = { | ||
dataView, | ||
allFields: null, | ||
fieldsExistenceReader, | ||
}; | ||
const { result } = renderHook(useNewFields, { | ||
initialProps: props, | ||
}); | ||
|
||
expect(result.current.allFieldsModified).toBe(null); | ||
expect(result.current.hasNewFields).toBe(false); | ||
}); | ||
|
||
it('should work correctly with empty new fields', async () => { | ||
const props: UseNewFieldsParams<DataViewField> = { | ||
dataView, | ||
allFields, | ||
fieldsExistenceReader, | ||
}; | ||
const { result } = renderHook(useNewFields, { | ||
initialProps: props, | ||
}); | ||
|
||
expect(result.current.allFieldsModified).toBe(allFields); | ||
expect(result.current.hasNewFields).toBe(false); | ||
}); | ||
|
||
it('should work correctly with new fields', async () => { | ||
const newField = { name: 'test', type: 'keyword', searchable: true, aggregatable: true }; | ||
const newField2 = { ...newField, name: 'test2' }; | ||
const props: UseNewFieldsParams<DataViewField> = { | ||
dataView, | ||
allFields, | ||
fieldsExistenceReader: { | ||
...fieldsExistenceReader, | ||
getNewFields: () => [newField, newField2], | ||
}, | ||
getNewFieldsBySpec: (spec) => spec.map((field) => new DataViewField(field)), | ||
}; | ||
const { result } = renderHook(useNewFields, { | ||
initialProps: props, | ||
}); | ||
|
||
expect(result.current.allFieldsModified).toStrictEqual([ | ||
...allFields, | ||
new DataViewField(newField), | ||
new DataViewField(newField2), | ||
]); | ||
expect(result.current.hasNewFields).toBe(true); | ||
}); | ||
}); |
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