Skip to content

Commit

Permalink
Fix broken mocks in Jest tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed Aug 13, 2021
1 parent 4a26336 commit e8ef235
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ jest.mock('@elastic/eui', () => {
};
});

// This mocks JsonEditor's consumption of EuiCodeEditor.
jest.mock(
'../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor',
() => {
const original = jest.requireActual(
'../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor'
);

return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj="mockCodeEditor"
onChange={(syntheticEvent: any) => {
props.onChange(syntheticEvent.jsonString);
}}
/>
),
};
}
);

describe('<ComponentTemplateCreate />', () => {
let testBed: ComponentTemplateCreateTestBed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ jest.mock('@elastic/eui', () => {
}}
/>
),
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj={props['data-test-subj'] || 'mockCodeEditor'}
data-currentvalue={props.value}
onChange={(e: any) => {
props.onChange(e.jsonContent);
}}
/>
),
// Mocking EuiSuperSelect to be able to easily change its value
// with a `myWrapper.simulate('change', { target: { value: 'someValue' } })`
EuiSuperSelect: (props: any) => (
Expand All @@ -54,6 +44,29 @@ jest.mock('@elastic/eui', () => {
};
});

jest.mock(
'../../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor',
() => {
const original = jest.requireActual(
'../../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor'
);

return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj={props['data-test-subj'] || 'mockCodeEditor'}
data-currentvalue={props.value}
onChange={(e: any) => {
props.onChange(e.jsonContent);
}}
/>
),
};
}
);

jest.mock('../../../../../../../../../../src/plugins/kibana_react/public', () => {
const original = jest.requireActual(
'../../../../../../../../../../src/plugins/kibana_react/public'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
import React from 'react';
import { act } from 'react-dom/test-utils';

jest.mock('@elastic/eui', () => {
const original = jest.requireActual('@elastic/eui');

return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj="mockCodeEditor"
onChange={(syntheticEvent: any) => {
props.onChange(syntheticEvent.jsonString);
}}
/>
),
};
});

jest.mock('lodash', () => {
const original = jest.requireActual('lodash');

Expand All @@ -34,6 +17,28 @@ jest.mock('lodash', () => {
};
});

jest.mock(
'../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor',
() => {
const original = jest.requireActual(
'../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor'
);

return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj="mockCodeEditor"
onChange={(syntheticEvent: any) => {
props.onChange(syntheticEvent.jsonString);
}}
/>
),
};
}
);

import { registerTestBed, TestBed } from '@kbn/test/jest';
import { LoadMappingsProvider } from './load_mappings_provider';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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
Expand All @@ -39,8 +40,11 @@ jest.mock('../../../../../../../../src/plugins/kibana_react/public', () => {
};
});

jest.mock('@elastic/eui', () => {
const original = jest.requireActual('@elastic/eui');
jest.mock('../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor', () => {
const original = jest.requireActual(
'../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor'
);

return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
import React from 'react';
import { ModalProvider, OnDoneLoadJsonHandler } from './modal_provider';

jest.mock('@elastic/eui', () => {
const original = jest.requireActual('@elastic/eui');
jest.mock(
'../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor',
() => {
const original = jest.requireActual(
'../../../../../../../../../src/plugins/es_ui_shared/public/components/code_editor'
);

return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj="mockCodeEditor"
onChange={(syntheticEvent: any) => {
props.onChange(syntheticEvent.jsonString);
}}
/>
),
};
});
return {
...original,
// Mocking EuiCodeEditor, which uses React Ace under the hood
EuiCodeEditor: (props: any) => (
<input
data-test-subj="mockCodeEditor"
onChange={(syntheticEvent: any) => {
props.onChange(syntheticEvent.jsonString);
}}
/>
),
};
}
);

jest.mock('lodash', () => {
const original = jest.requireActual('lodash');
Expand Down

0 comments on commit e8ef235

Please sign in to comment.