From 12f2f1d2900f7f79dc9fb911fb1063a563511e4d Mon Sep 17 00:00:00 2001 From: Francesco Torchia Date: Thu, 11 May 2023 17:12:08 +0200 Subject: [PATCH 1/3] [create-yaml] add unit tests Signed-off-by: Francesco Torchia --- shell/utils/__tests__/create-yaml.test.ts | 63 +++++++++++++++++++++++ shell/utils/array.ts | 4 ++ shell/utils/create-yaml.js | 11 ++-- 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 shell/utils/__tests__/create-yaml.test.ts diff --git a/shell/utils/__tests__/create-yaml.test.ts b/shell/utils/__tests__/create-yaml.test.ts new file mode 100644 index 0000000000..8bf4742c94 --- /dev/null +++ b/shell/utils/__tests__/create-yaml.test.ts @@ -0,0 +1,63 @@ +import { + getBlockDescriptor, + dumpBlock, +} from '@shell/utils/create-yaml'; + +const key = 'example'; +const randomData = '\n foo\n bar\n'; + +const scalarStyles = ['>', '|']; +const chomping = ['+', '-', '']; +const indentations = ['4', '2', '']; + +describe('fx: getBlockDescriptor', () => { + describe('should parse blocks header for all block indicators combo', () => { + scalarStyles.forEach((scalar) => { + chomping.forEach((chomping) => { + indentations.forEach((indentation) => { + const combo = `${ scalar }${ indentation }${ chomping }`; + + it(`combo: ${ combo }`, () => { + const toParse = `${ key }: ${ combo }${ randomData }`; + + const desc = getBlockDescriptor(toParse, key); + + expect(desc?.header).toBe(`${ key }: ${ combo }`); + expect(desc?.indentation).toBe(indentation); + }); + }); + }); + }); + }); +}); + +describe('fx: dumpBlock', () => { + describe('should create a data block replacing indicators with blocks indicator from options', () => { + const key = 'example'; + + scalarStyles.forEach((scalarStyle) => { + chomping.forEach((chomping) => { + const options = { + [key]: { + chomping, + scalarStyle + } + }; + + it(`options: { scalarStyle: ${ scalarStyle }, chomping: ${ chomping } } with indentation`, () => { + const data = { [key]: ' foo \n bar \n \n foo\n bar\n ' }; + const block = dumpBlock(data, options); + + expect(block.includes(`example: ${ scalarStyle }${ chomping }2`)).toBeTruthy(); + }); + + it(`options: { scalarStyle: ${ scalarStyle }, chomping: ${ chomping } } without indentation`, () => { + const data = { [key]: 'foo \nbar \n\nfoo\nbar\n ' }; + const block = dumpBlock(data, options); + + expect(block.includes(`example: ${ scalarStyle }${ chomping }`)).toBeTruthy(); + }); + }); + }); + }); +}); diff --git a/shell/utils/array.ts b/shell/utils/array.ts index bee4d3a49c..5708abba12 100644 --- a/shell/utils/array.ts +++ b/shell/utils/array.ts @@ -188,6 +188,10 @@ export function uniq(ary: T[]): T[] { return out; } +export function concatStrings(a: string[], b: string[]) { + return [...a.map(aa => b.map(bb => aa.concat(bb)))].reduce((acc, arr) => [...arr, ...acc], []); +} + interface KubeResource { metadata: { labels: { [name: string]: string} } } // Migrate to central kube types resource when those are brought in export function getUniqueLabelKeys(aryResources: T[]): string[] { const uniqueObj = aryResources.reduce((res, r) => { diff --git a/shell/utils/create-yaml.js b/shell/utils/create-yaml.js index 6070114ef9..a549b0804c 100644 --- a/shell/utils/create-yaml.js +++ b/shell/utils/create-yaml.js @@ -370,7 +370,7 @@ function serializeSimpleValue(data) { return jsyaml.dump(data).trim(); } -function getBlockDescriptor(value, key) { +export function getBlockDescriptor(value, key) { const header = getBlockHeader(value, key); return { @@ -451,7 +451,7 @@ export function saferDump(obj) { * one of '|', '>' * default '|' * - chomping: - * one of: null, '-', '+' + * one of: null, '', '-', '+' * default: null * @returns the result of jsyaml.dump with the addition of multiline indicators */ @@ -464,15 +464,16 @@ export function dumpBlock(data, options = {}) { if (blockFields.length) { for (const key of blockFields) { + const desc = getBlockDescriptor(out, key); + const scalarStyle = options[key]?.scalarStyle ?? '|'; const chomping = options[key]?.chomping ?? ''; - - const desc = getBlockDescriptor(out, key); + const indentation = desc.indentation; /** * Replace the original block indicators with the ones provided in the options param */ - out = out.replace(desc.header, `${ key }: ${ scalarStyle }${ chomping }${ desc.indentation }`); + out = out.replace(desc.header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`); } } From 7653bc3ca6a7aa38e7be01aa39fbd0e599f8291a Mon Sep 17 00:00:00 2001 From: Francesco Torchia Date: Thu, 11 May 2023 17:13:13 +0200 Subject: [PATCH 2/3] [KeyValue] test multiline field Signed-off-by: Francesco Torchia --- shell/components/form/KeyValue.vue | 1 + .../components/form/__tests__/KeyValue.test.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/shell/components/form/KeyValue.vue b/shell/components/form/KeyValue.vue index 62fc8733d4..de43bd3f31 100644 --- a/shell/components/form/KeyValue.vue +++ b/shell/components/form/KeyValue.vue @@ -660,6 +660,7 @@ export default { { expect(inputValue.value).toBe(value); }); + + it('should display a markdown-multiline field with new lines visible', () => { + const wrapper = mount(KeyValue, { + propsData: { + value: 'test', + valueMarkdownMultiline: true, + }, + mocks: { $store: { getters: { 'i18n/t': jest.fn() } } }, + directives: { t } + }); + + const inputFieldTextArea = wrapper.find('textarea').element; + const inputFieldMultiline = wrapper.find('[data-testid="code-mirror-multiline-field"]').element; + + expect(inputFieldTextArea).toBeUndefined(); + expect(inputFieldMultiline).toBeDefined(); + }); }); From 4da0da96a0b1eddcef99a38416265bdea2cc4dd2 Mon Sep 17 00:00:00 2001 From: Francesco Torchia Date: Tue, 16 May 2023 10:09:31 +0200 Subject: [PATCH 3/3] Clean create-yaml.js code Signed-off-by: Francesco Torchia --- shell/utils/create-yaml.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/shell/utils/create-yaml.js b/shell/utils/create-yaml.js index a549b0804c..86289cf709 100644 --- a/shell/utils/create-yaml.js +++ b/shell/utils/create-yaml.js @@ -464,16 +464,15 @@ export function dumpBlock(data, options = {}) { if (blockFields.length) { for (const key of blockFields) { - const desc = getBlockDescriptor(out, key); + const { header, indentation } = getBlockDescriptor(out, key); const scalarStyle = options[key]?.scalarStyle ?? '|'; const chomping = options[key]?.chomping ?? ''; - const indentation = desc.indentation; /** * Replace the original block indicators with the ones provided in the options param */ - out = out.replace(desc.header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`); + out = out.replace(header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`); } }