1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { FormsModule } from '@angular/forms';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { NgxDatatableModule } from '@swimlane/ngx-datatable';
7 import { configureTestBed } from '../../../../testing/unit-test-helper';
8 import { ComponentsModule } from '../../components/components.module';
9 import { TableComponent } from '../table/table.component';
10 import { TableKeyValueComponent } from './table-key-value.component';
12 describe('TableKeyValueComponent', () => {
13 let component: TableKeyValueComponent;
14 let fixture: ComponentFixture<TableKeyValueComponent>;
17 declarations: [TableComponent, TableKeyValueComponent],
18 imports: [FormsModule, NgxDatatableModule, ComponentsModule, RouterTestingModule]
22 fixture = TestBed.createComponent(TableKeyValueComponent);
23 component = fixture.componentInstance;
26 it('should create', () => {
27 expect(component).toBeTruthy();
30 it('should make key value object pairs out of arrays with length two', () => {
31 component.data = [['someKey', 0], [3, 'something']];
33 expect(component.tableData.length).toBe(2);
34 expect(component.tableData[0].key).toBe('someKey');
35 expect(component.tableData[1].value).toBe('something');
38 it('should transform arrays', () => {
39 component.data = [['someKey', [1, 2, 3]], [3, 'something']];
41 expect(component.tableData.length).toBe(2);
42 expect(component.tableData[0].key).toBe('someKey');
43 expect(component.tableData[0].value).toBe('1, 2, 3');
44 expect(component.tableData[1].value).toBe('something');
47 it('should remove pure object values', () => {
48 component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
50 expect(component.tableData.length).toBe(1);
51 expect(component.tableData[0].value).toBe('something');
54 it('makes key value object pairs out of an object', () => {
60 expect(component.tableData.length).toBe(2);
61 expect(component.tableData[0].value).toBe('something');
62 expect(component.tableData[1].key).toBe('someKey');
65 it('does nothing if data is correct', () => {
77 expect(component.tableData.length).toBe(2);
78 expect(component.tableData[0].value).toBe('something');
79 expect(component.tableData[1].key).toBe('someKey');
82 it('throws errors if miss match', () => {
84 expect(() => component.ngOnInit()).toThrowError('Wrong data format');
85 component.data = [['someKey', 0, 3]];
86 expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
87 component.data = [{ somekey: 939, somethingElse: 'test' }];
90 describe('Class objects equal plain objects', () => {
94 constructor(deep: boolean) {
96 this.deep = new Example(false);
101 const classExample = new Example(true);
102 const objectExample = {
109 const getTableData = (data) => {
110 component.data = data;
111 expect(() => component.ngOnInit()).not.toThrow();
112 return component.tableData;
115 const doesClassEqualsObject = (classData, objectData, dataLength) => {
116 const classTableData = getTableData(classData);
117 expect(classTableData).toEqual(getTableData(objectData));
118 expect(classTableData.length).toBe(dataLength);
121 it('should convert class objects the same way as plain objects', () => {
122 doesClassEqualsObject(classExample, objectExample, 1);
123 doesClassEqualsObject([classExample], [objectExample], 1);
124 component.renderObjects = true;
125 doesClassEqualsObject(classExample, objectExample, 2);
126 doesClassEqualsObject([classExample], [objectExample], 2);
130 it('tests _makePairs', () => {
131 expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
132 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
133 expect(component._makePairs(pair)).toEqual(pair);
134 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
135 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pair);
138 it('tests _makePairsFromArray', () => {
139 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
140 { key: 'dash', value: 'board' }
142 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
143 expect(component._makePairsFromArray(pair)).toEqual(pair);
146 it('tests _makePairsFromObject', () => {
147 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
148 { key: 'dash', value: 'board' }
150 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
151 { key: 'dash', value: 'board' },
152 { key: 'ceph', value: 'mimic' }
156 describe('tests _convertValue', () => {
157 const v = (value) => ({ key: 'sth', value: value });
158 const testConvertValue = (value, result) =>
159 expect(component._convertValue(v(value)).value).toBe(result);
161 it('should leave a string as it is', () => {
162 testConvertValue('something', 'something');
165 it('should leave an int as it is', () => {
166 testConvertValue(29, 29);
169 it('should convert arrays with any type to string', () => {
170 testConvertValue([1, 2, 3], '1, 2, 3');
171 testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
172 testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
175 it('should convert only allow objects if renderObjects is set to true', () => {
176 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
177 component.renderObjects = true;
178 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
182 it('tests _insertFlattenObjects', () => {
183 component.renderObjects = true;
200 expect(component._insertFlattenObjects(v)).toEqual([
201 { key: 'no', value: 'change' },
202 { key: 'first layer', value: 'something' },
203 { key: 'first second l3_1', value: 33 },
204 { key: 'first second l3_2', value: 44 }
208 describe('render objects', () => {
213 anotherSetting2: 'somethingElse',
221 additionalKeyContainingObject: { type: 'none' },
222 keyWithEmptyObject: {}
224 component.renderObjects = true;
227 it('with parent key', () => {
228 component.ngOnInit();
229 expect(component.tableData).toEqual([
230 { key: 'someKey', value: 0 },
231 { key: 'keyWithEmptyObject', value: '' },
232 { key: 'options someSetting1', value: 38 },
233 { key: 'options anotherSetting2', value: 'somethingElse' },
234 { key: 'options suboptions sub1', value: 12 },
235 { key: 'options suboptions sub2', value: 34 },
236 { key: 'options suboptions sub3', value: 56 },
237 { key: 'additionalKeyContainingObject type', value: 'none' }
241 it('without parent key', () => {
242 component.appendParentKey = false;
243 component.ngOnInit();
244 expect(component.tableData).toEqual([
245 { key: 'someKey', value: 0 },
246 { key: 'keyWithEmptyObject', value: '' },
247 { key: 'someSetting1', value: 38 },
248 { key: 'anotherSetting2', value: 'somethingElse' },
249 { key: 'sub1', value: 12 },
250 { key: 'sub2', value: 34 },
251 { key: 'sub3', value: 56 },
252 { key: 'type', value: 'none' }
257 describe('subscribe fetchData', () => {
258 it('should not subscribe fetchData of table', () => {
259 component.ngOnInit();
260 expect(component.table.fetchData.observers.length).toBe(0);
263 it('should call fetchData', () => {
265 component.fetchData.subscribe(() => {
268 component.ngOnInit();
269 expect(component.table.fetchData.observers.length).toBe(1);
270 component.table.fetchData.emit();
271 expect(called).toBeTruthy();