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(); + }); }); 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..86289cf709 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,15 @@ export function dumpBlock(data, options = {}) { if (blockFields.length) { for (const key of blockFields) { + const { header, indentation } = getBlockDescriptor(out, key); + const scalarStyle = options[key]?.scalarStyle ?? '|'; const chomping = options[key]?.chomping ?? ''; - const desc = getBlockDescriptor(out, key); - /** * 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(header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`); } }