forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.helpers.tsx
191 lines (170 loc) · 5.32 KB
/
processor.helpers.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* 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.
*/
import { act } from 'react-dom/test-utils';
import React from 'react';
import axios from 'axios';
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
/* eslint-disable @kbn/eslint/no-restricted-paths */
import { usageCollectionPluginMock } from 'src/plugins/usage_collection/public/mocks';
import { registerTestBed, TestBed } from '@kbn/test/jest';
import { stubWebWorker } from '@kbn/test/jest';
import { uiMetricService, apiService } from '../../../../services';
import { Props } from '../../';
import { initHttpRequests } from '../http_requests.helpers';
import { ProcessorsEditorWithDeps } from '../processors_editor';
stubWebWorker();
jest.mock('../../../../../../../../../src/plugins/kibana_react/public', () => {
const original = jest.requireActual('../../../../../../../../../src/plugins/kibana_react/public');
return {
...original,
// Mocking CodeEditor, which uses React Monaco under the hood
CodeEditor: (props: any) => (
<input
data-test-subj={props['data-test-subj'] || 'mockCodeEditor'}
data-currentvalue={props.value}
onChange={(e: any) => {
props.onChange(e.jsonContent);
}}
/>
),
};
});
jest.mock('@elastic/eui', () => {
const original = jest.requireActual('@elastic/eui');
return {
...original,
// Mocking EuiComboBox, as it utilizes "react-virtualized" for rendering search suggestions,
// which does not produce a valid component wrapper
EuiComboBox: (props: any) => (
<input
data-test-subj={props['data-test-subj']}
data-currentvalue={props.selectedOptions}
onChange={async (syntheticEvent: any) => {
props.onChange([syntheticEvent['0']]);
}}
/>
),
};
});
jest.mock('react-virtualized', () => {
const original = jest.requireActual('react-virtualized');
return {
...original,
AutoSizer: ({ children }: { children: any }) => (
<div>{children({ height: 500, width: 500 })}</div>
),
};
});
const testBedSetup = registerTestBed<TestSubject>(
(props: Props) => <ProcessorsEditorWithDeps {...props} />,
{
doMountAsync: false,
}
);
export interface SetupResult extends TestBed<TestSubject> {
actions: ReturnType<typeof createActions>;
}
const createActions = (testBed: TestBed<TestSubject>) => {
const { find, component } = testBed;
return {
async saveNewProcessor() {
await act(async () => {
find('addProcessorForm.submitButton').simulate('click');
});
component.update();
},
async addProcessorType(type: string) {
await act(async () => {
find('processorTypeSelector.input').simulate('change', [{ value: type }]);
});
component.update();
},
addProcessor() {
find('addProcessorButton').simulate('click');
},
};
};
export const setup = async (props: Props): Promise<SetupResult> => {
const testBed = await testBedSetup(props);
return {
...testBed,
actions: createActions(testBed),
};
};
const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });
export const setupEnvironment = () => {
// Initialize mock services
uiMetricService.setup(usageCollectionPluginMock.createSetupContract());
// @ts-ignore
apiService.setup(mockHttpClient, uiMetricService);
const { server, httpRequestsMockHelpers } = initHttpRequests();
return {
server,
httpRequestsMockHelpers,
};
};
export const getProcessorValue = (onUpdate: jest.Mock, type: string) => {
const [onUpdateResult] = onUpdate.mock.calls[onUpdate.mock.calls.length - 1];
const { processors } = onUpdateResult.getData();
return processors;
};
type TestSubject =
| 'addProcessorForm.submitButton'
| 'addProcessorButton'
| 'addProcessorForm.submitButton'
| 'appendValueField.input'
| 'formatsValueField.input'
| 'timezoneField.input'
| 'outputFormatField.input'
| 'localeField.input'
| 'processorTypeSelector.input'
| 'fieldNameField.input'
| 'messageField.input'
| 'mockCodeEditor'
| 'tagField.input'
| 'typeSelectorField'
| 'dateRoundingField'
| 'ignoreMissingSwitch.input'
| 'ignoreFailureSwitch.input'
| 'indexNamePrefixField.input'
| 'indexNameFormatField.input'
| 'dateFormatsField.input'
| 'timezoneField.input'
| 'localeField.input'
| 'ifField.textarea'
| 'targetField.input'
| 'targetFieldsField.input'
| 'keepOriginalField.input'
| 'removeIfSuccessfulField.input'
| 'shapeSelectorField'
| 'errorDistanceField.input'
| 'separatorValueField.input'
| 'quoteValueField.input'
| 'emptyValueField.input'
| 'extractDeviceTypeSwitch.input'
| 'propertiesValueField'
| 'regexFileField.input'
| 'valueFieldInput'
| 'mediaTypeSelectorField'
| 'networkDirectionField.input'
| 'toggleCustomField'
| 'ignoreEmptyField.input'
| 'overrideField.input'
| 'fieldsValueField.input'
| 'saltValueField.input'
| 'methodsValueField'
| 'sourceIpField.input'
| 'sourcePortField.input'
| 'destinationIpField.input'
| 'destinationPortField.input'
| 'icmpTypeField.input'
| 'icmpCodeField.input'
| 'ianaField.input'
| 'transportField.input'
| 'seedField.input'
| 'copyFromInput'
| 'trimSwitch.input';