From 95db06766d6ef2ddb79860a9ea6bd456eac0184f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stephan=20M=C3=BCller?= Date: Tue, 5 Jun 2018 16:11:12 +0200 Subject: [PATCH] mgr/dashboard: Stringify object[] in KV-table MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The problem was that object[] weren't handled in the key value table before. Now they will be stringified to prevent an output like '[Object object]'. Signed-off-by: Stephan Müller --- .../table-key-value.component.spec.ts | 29 +++++++++++++++---- .../table-key-value.component.ts | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.spec.ts index 53e89e53f9296..df54ca431084a 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.spec.ts @@ -121,13 +121,30 @@ describe('TableKeyValueComponent', () => { ]); }); - it('tests _convertValue', () => { + describe('tests _convertValue', () => { const v = (value) => ({ key: 'sth', value: value }); - expect(component._convertValue(v('something'))).toEqual(v('something')); - expect(component._convertValue(v([1, 2, 3]))).toEqual(v('1, 2, 3')); - expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined); - component.renderObjects = true; - expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' })); + const testConvertValue = (value, result) => + expect(component._convertValue(v(value)).value).toBe(result); + + it('should leave a string as it is', () => { + testConvertValue('something', 'something') + }); + + it('should leave an int as it is', () => { + testConvertValue(29, 29) + }); + + it('should convert arrays with any type to string', () => { + testConvertValue([1, 2, 3], '1, 2, 3') + testConvertValue([{ sth: 'something' }], '{"sth":"something"}') + testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}') + }); + + it('should convert only allow objects if renderObjects is set to true', () => { + expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined); + component.renderObjects = true; + expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' })); + }); }); it('tests _insertFlattenObjects', () => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.ts index a6ad738073e8b..0c836648fb321 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.ts @@ -131,7 +131,7 @@ export class TableKeyValueComponent implements OnInit, OnChanges { _convertValue(v: any) { if (_.isArray(v.value)) { - v.value = v.value.join(', '); + v.value = v.value.map((v) => (_.isPlainObject(v) ? JSON.stringify(v) : v)).join(', '); } else if (_.isPlainObject(v.value) && !this.renderObjects) { return; } -- 2.39.5