From 3108d7108fb30a77ca66257ebb9867d2599640ce Mon Sep 17 00:00:00 2001 From: Joe Portner <5295965+jportner@users.noreply.github.com> Date: Mon, 2 Aug 2021 15:21:15 -0400 Subject: [PATCH] Change Role Mappings editor to use CodeEditor --- .../json_rule_editor.test.tsx | 41 +++++++++++-------- .../rule_editor_panel/json_rule_editor.tsx | 34 +++++++-------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.test.tsx b/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.test.tsx index 1cfc57323da1..78174484f701 100644 --- a/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.test.tsx +++ b/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.test.tsx @@ -12,25 +12,27 @@ import 'brace/mode/json'; // warnings in the console which adds unnecessary noise to the test output. import '@kbn/test/target_node/jest/utils/stub_web_worker'; -import { EuiCodeEditor } from '@elastic/eui'; import React from 'react'; import { act } from 'react-dom/test-utils'; -import { mountWithIntl } from '@kbn/test/jest'; -import { coreMock } from 'src/core/public/mocks'; -import { KibanaContextProvider } from 'src/plugins/kibana_react/public'; +import type { monaco } from '@kbn/monaco'; +import { shallowWithIntl } from '@kbn/test/jest'; +import { CodeEditor } from 'src/plugins/kibana_react/public'; import { AllRule, AnyRule, ExceptAllRule, ExceptAnyRule, FieldRule } from '../../model'; import { JSONRuleEditor } from './json_rule_editor'; +jest.mock('../../../../../../../../src/plugins/kibana_react/public', () => ({ + ...jest.requireActual('../../../../../../../../src/plugins/kibana_react/public'), + useKibana: jest.fn().mockReturnValue({ + services: { docLinks: { links: { apis: { createRoleMapping: 'createRoleMappingLink' } } } }, + }), +})); + describe('JSONRuleEditor', () => { + const mockChangeEvent = {} as monaco.editor.IModelContentChangedEvent; const renderView = (props: React.ComponentProps) => { - const coreStart = coreMock.createStart(); - return mountWithIntl( - - - - ); + return shallowWithIntl(); }; it('renders an empty rule set', () => { @@ -40,7 +42,8 @@ describe('JSONRuleEditor', () => { expect(props.onChange).not.toHaveBeenCalled(); expect(props.onValidityChange).not.toHaveBeenCalled(); - expect(wrapper.find(EuiCodeEditor).props().value).toMatchInlineSnapshot(`"{}"`); + wrapper.update(); + expect(wrapper.find(CodeEditor).props().value).toMatchInlineSnapshot(`"{}"`); }); it('renders a rule set', () => { @@ -58,7 +61,7 @@ describe('JSONRuleEditor', () => { }; const wrapper = renderView(props); - const { value } = wrapper.find(EuiCodeEditor).props(); + const { value } = wrapper.find(CodeEditor).props(); expect(JSON.parse(value as string)).toEqual({ all: [ { @@ -89,7 +92,10 @@ describe('JSONRuleEditor', () => { const allRule = JSON.stringify(new AllRule().toRaw()); act(() => { - wrapper.find(EuiCodeEditor).props().onChange!(allRule + ', this makes invalid JSON'); + wrapper.find(CodeEditor).props().onChange!( + allRule + ', this makes invalid JSON', + mockChangeEvent + ); }); expect(props.onValidityChange).toHaveBeenCalledTimes(1); @@ -112,7 +118,7 @@ describe('JSONRuleEditor', () => { }); act(() => { - wrapper.find(EuiCodeEditor).props().onChange!(invalidRule); + wrapper.find(CodeEditor).props().onChange!(invalidRule, mockChangeEvent); }); expect(props.onValidityChange).toHaveBeenCalledTimes(1); @@ -126,7 +132,10 @@ describe('JSONRuleEditor', () => { const allRule = JSON.stringify(new AllRule().toRaw()); act(() => { - wrapper.find(EuiCodeEditor).props().onChange!(allRule + ', this makes invalid JSON'); + wrapper.find(CodeEditor).props().onChange!( + allRule + ', this makes invalid JSON', + mockChangeEvent + ); }); expect(props.onValidityChange).toHaveBeenCalledTimes(1); @@ -136,7 +145,7 @@ describe('JSONRuleEditor', () => { props.onValidityChange.mockReset(); act(() => { - wrapper.find(EuiCodeEditor).props().onChange!(allRule); + wrapper.find(CodeEditor).props().onChange!(allRule, mockChangeEvent); }); expect(props.onValidityChange).toHaveBeenCalledTimes(1); diff --git a/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.tsx b/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.tsx index ecbc71f295a9..5308b969b3b0 100644 --- a/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.tsx +++ b/x-pack/plugins/security/public/management/role_mappings/edit_role_mapping/rule_editor_panel/json_rule_editor.tsx @@ -8,13 +8,14 @@ import 'brace/mode/json'; import 'brace/theme/github'; -import { EuiButton, EuiCodeEditor, EuiFormRow, EuiLink, EuiSpacer, EuiText } from '@elastic/eui'; +import { EuiButton, EuiFormRow, EuiLink, EuiSpacer, EuiText } from '@elastic/eui'; import React, { Fragment, useState } from 'react'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; +import { XJsonLang } from '@kbn/monaco'; -import { useKibana } from '../../../../../../../../src/plugins/kibana_react/public'; +import { CodeEditor, useKibana } from '../../../../../../../../src/plugins/kibana_react/public'; import type { Rule } from '../../model'; import { generateRulesFromRaw, RuleBuilderError } from '../../model'; @@ -76,25 +77,26 @@ export const JSONRuleEditor = (props: Props) => { data-test-subj="roleMappingsJSONEditor" > -