-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Runtime field editor] Component integration test for preview (#106410)
- Loading branch information
Showing
21 changed files
with
1,523 additions
and
233 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
185 changes: 185 additions & 0 deletions
185
...x_pattern_field_editor/__jest__/client_integration/field_editor_flyout_preview.helpers.ts
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,185 @@ | ||
/* | ||
* 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 { act } from 'react-dom/test-utils'; | ||
import { ReactWrapper } from 'enzyme'; | ||
import { registerTestBed, TestBed } from '@kbn/test/jest'; | ||
|
||
import { API_BASE_PATH } from '../../common/constants'; | ||
import { Context } from '../../public/components/field_editor_context'; | ||
import { | ||
FieldEditorFlyoutContent, | ||
Props, | ||
} from '../../public/components/field_editor_flyout_content'; | ||
import { | ||
WithFieldEditorDependencies, | ||
getCommonActions, | ||
spyIndexPatternGetAllFields, | ||
spySearchQuery, | ||
spySearchQueryResponse, | ||
} from './helpers'; | ||
|
||
const defaultProps: Props = { | ||
onSave: () => {}, | ||
onCancel: () => {}, | ||
runtimeFieldValidator: () => Promise.resolve(null), | ||
isSavingField: false, | ||
}; | ||
|
||
/** | ||
* This handler lets us mock the fields present on the index pattern during our test | ||
* @param fields The fields of the index pattern | ||
*/ | ||
export const setIndexPatternFields = (fields: Array<{ name: string; displayName: string }>) => { | ||
spyIndexPatternGetAllFields.mockReturnValue(fields); | ||
}; | ||
|
||
export interface TestDoc { | ||
title: string; | ||
subTitle: string; | ||
description: string; | ||
} | ||
|
||
export const getSearchCallMeta = () => { | ||
const totalCalls = spySearchQuery.mock.calls.length; | ||
const lastCall = spySearchQuery.mock.calls[totalCalls - 1] ?? null; | ||
let lastCallParams = null; | ||
|
||
if (lastCall) { | ||
lastCallParams = lastCall[0]; | ||
} | ||
|
||
return { | ||
totalCalls, | ||
lastCall, | ||
lastCallParams, | ||
}; | ||
}; | ||
|
||
export const setSearchResponse = ( | ||
documents: Array<{ _id: string; _index: string; _source: TestDoc }> | ||
) => { | ||
spySearchQueryResponse.mockResolvedValue({ | ||
rawResponse: { | ||
hits: { | ||
total: documents.length, | ||
hits: documents, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
const getActions = (testBed: TestBed) => { | ||
const getWrapperRenderedIndexPatternFields = (): ReactWrapper | null => { | ||
if (testBed.find('indexPatternFieldList').length === 0) { | ||
return null; | ||
} | ||
return testBed.find('indexPatternFieldList.listItem'); | ||
}; | ||
|
||
const getRenderedIndexPatternFields = (): Array<{ key: string; value: string }> => { | ||
const allFields = getWrapperRenderedIndexPatternFields(); | ||
|
||
if (allFields === null) { | ||
return []; | ||
} | ||
|
||
return allFields.map((field) => { | ||
const key = testBed.find('key', field).text(); | ||
const value = testBed.find('value', field).text(); | ||
return { key, value }; | ||
}); | ||
}; | ||
|
||
const getRenderedFieldsPreview = () => { | ||
if (testBed.find('fieldPreviewItem').length === 0) { | ||
return []; | ||
} | ||
|
||
const previewFields = testBed.find('fieldPreviewItem.listItem'); | ||
|
||
return previewFields.map((field) => { | ||
const key = testBed.find('key', field).text(); | ||
const value = testBed.find('value', field).text(); | ||
return { key, value }; | ||
}); | ||
}; | ||
|
||
const setFilterFieldsValue = async (value: string) => { | ||
await act(async () => { | ||
testBed.form.setInputValue('filterFieldsInput', value); | ||
}); | ||
|
||
testBed.component.update(); | ||
}; | ||
|
||
// Need to set "server: any" (instead of SinonFakeServer) to avoid a TS error :( | ||
// Error: Exported variable 'setup' has or is using name 'Document' from external module "/dev/shm/workspace/parallel/14/kibana/node_modules/@types/sinon/ts3.1/index" | ||
const getLatestPreviewHttpRequest = (server: any) => { | ||
let i = server.requests.length - 1; | ||
|
||
while (i >= 0) { | ||
const request = server.requests[i]; | ||
if (request.method === 'POST' && request.url === `${API_BASE_PATH}/field_preview`) { | ||
return { | ||
...request, | ||
requestBody: JSON.parse(JSON.parse(request.requestBody).body), | ||
}; | ||
} | ||
i--; | ||
} | ||
|
||
throw new Error(`Can't access the latest preview HTTP request as it hasn't been called.`); | ||
}; | ||
|
||
const goToNextDocument = async () => { | ||
await act(async () => { | ||
testBed.find('goToNextDocButton').simulate('click'); | ||
}); | ||
testBed.component.update(); | ||
}; | ||
|
||
const goToPreviousDocument = async () => { | ||
await act(async () => { | ||
testBed.find('goToPrevDocButton').simulate('click'); | ||
}); | ||
testBed.component.update(); | ||
}; | ||
|
||
const loadCustomDocument = (docId: string) => {}; | ||
|
||
return { | ||
...getCommonActions(testBed), | ||
getWrapperRenderedIndexPatternFields, | ||
getRenderedIndexPatternFields, | ||
getRenderedFieldsPreview, | ||
setFilterFieldsValue, | ||
getLatestPreviewHttpRequest, | ||
goToNextDocument, | ||
goToPreviousDocument, | ||
loadCustomDocument, | ||
}; | ||
}; | ||
|
||
export const setup = async (props?: Partial<Props>, deps?: Partial<Context>) => { | ||
let testBed: TestBed; | ||
|
||
// Setup testbed | ||
await act(async () => { | ||
testBed = await registerTestBed(WithFieldEditorDependencies(FieldEditorFlyoutContent, deps), { | ||
memoryRouter: { | ||
wrapComponent: false, | ||
}, | ||
})({ ...defaultProps, ...props }); | ||
}); | ||
|
||
testBed!.component.update(); | ||
|
||
return { ...testBed!, actions: getActions(testBed!) }; | ||
}; | ||
|
||
export type FieldEditorFlyoutContentTestBed = TestBed & { actions: ReturnType<typeof getActions> }; |
Oops, something went wrong.