From 03d65e83634365ae00f2dfa2ed3d5a3de91c4da3 Mon Sep 17 00:00:00 2001 From: Vincent Kempers Date: Fri, 14 Oct 2022 20:17:24 +0200 Subject: [PATCH] Fix and improve raw value editor (#15868) * this works in the form-field setting up for refactor * refactored the useRaw and made form-field-raw-editor * add defaults * add tests for render submitting and cancelling * add isNil * delete the comment * add a cancel button * change let to const * add the if statement when it's not a object * delete the .raw-value and place it in the raw-editor form field * rename submit to setRawValue * change submit to set-raw-value * add a possibility to add a placeholder to the system-raw-editor * implement the system-raw-editor to the form-field-raw-editor * update the snapshot and fix the emitted tests * found out we can disable the gutter and line-numbers * add a language prop to the system when it's not defined it should default to mustache * delete style; add language and add type * update the html in tests * add input-code for the extended validation * add default value * Update form-field-raw-editor.vue language to plaintext Co-authored-by: Rijk van Zanten * update test Co-authored-by: Vincent Kempers Co-authored-by: Rijk van Zanten --- .../form-field-raw-editor.test.ts.snap | 16 +++ .../v-form/form-field-raw-editor.test.ts | 76 ++++++++++++++ .../v-form/form-field-raw-editor.vue | 99 +++++++++++++++++++ app/src/components/v-form/form-field.vue | 73 ++++---------- .../system-raw-editor/system-raw-editor.vue | 8 +- 5 files changed, 214 insertions(+), 58 deletions(-) create mode 100644 app/src/components/v-form/__snapshots__/form-field-raw-editor.test.ts.snap create mode 100644 app/src/components/v-form/form-field-raw-editor.test.ts create mode 100644 app/src/components/v-form/form-field-raw-editor.vue diff --git a/app/src/components/v-form/__snapshots__/form-field-raw-editor.test.ts.snap b/app/src/components/v-form/__snapshots__/form-field-raw-editor.test.ts.snap new file mode 100644 index 0000000000..631c76dd30 --- /dev/null +++ b/app/src/components/v-form/__snapshots__/form-field-raw-editor.test.ts.snap @@ -0,0 +1,16 @@ +// Vitest Snapshot v1 + +exports[`should render 1`] = ` +" + + edit_raw_value + + + + + cancel + done + + +" +`; diff --git a/app/src/components/v-form/form-field-raw-editor.test.ts b/app/src/components/v-form/form-field-raw-editor.test.ts new file mode 100644 index 0000000000..56086cfba5 --- /dev/null +++ b/app/src/components/v-form/form-field-raw-editor.test.ts @@ -0,0 +1,76 @@ +import { it, test, expect } from 'vitest'; +import { mount } from '@vue/test-utils'; +import { createI18n } from 'vue-i18n'; + +import formFieldRawEditor from './form-field-raw-editor.vue'; +import { GlobalMountOptions } from '@vue/test-utils/dist/types'; + +const i18n = createI18n(); + +const global: GlobalMountOptions = { + plugins: [i18n], +}; + +test('should render', () => { + expect(formFieldRawEditor).toBeTruthy(); + + const wrapper = mount(formFieldRawEditor, { + props: { + showModal: true, + field: 'object', + disabled: false, + currentValue: '["id","new_content"]', + }, + global, + }); + expect(wrapper.html()).toMatchSnapshot(); +}); + +// test if there is a value +test('submitting', async () => { + expect(formFieldRawEditor).toBeTruthy(); + const wrapper = mount(formFieldRawEditor, { + props: { + showModal: true, + field: 'string', + disabled: false, + currentValue: 'things', + }, + global, + }); + const button = wrapper.findAll('v-button').at(1); + await button!.trigger('click'); + await wrapper.vm.$nextTick(); + expect(wrapper.emitted().setRawValue.length).toBe(1); +}); + +it('should cancel with keydown', async () => { + const wrapper = mount(formFieldRawEditor, { + props: { + showModal: true, + field: 'object', + disabled: false, + currentValue: '["id","new_content"]', + }, + global, + }); + await wrapper.trigger('esc'); + await wrapper.vm.$nextTick(); + expect(wrapper.emitted().cancel.length).toBe(1); +}); + +it('should cancel with the cancel button', async () => { + const wrapper = mount(formFieldRawEditor, { + props: { + showModal: true, + field: 'object', + disabled: false, + currentValue: '["id","new_content"]', + }, + global, + }); + const button = wrapper.findAll('v-button').at(0); + await button!.trigger('click'); + await wrapper.vm.$nextTick(); + expect(wrapper.emitted().cancel.length).toBe(1); +}); diff --git a/app/src/components/v-form/form-field-raw-editor.vue b/app/src/components/v-form/form-field-raw-editor.vue new file mode 100644 index 0000000000..a33e5d3e60 --- /dev/null +++ b/app/src/components/v-form/form-field-raw-editor.vue @@ -0,0 +1,99 @@ + + + diff --git a/app/src/components/v-form/form-field.vue b/app/src/components/v-form/form-field.vue index e8e66164c1..36689cd27d 100644 --- a/app/src/components/v-form/form-field.vue +++ b/app/src/components/v-form/form-field.vue @@ -49,17 +49,14 @@ @set-field-value="$emit('setFieldValue', $event)" /> - - - {{ isDisabled ? t('view_raw_value') : t('edit_raw_value') }} - - - - - {{ t('done') }} - - - + @@ -74,7 +71,6 @@