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 { CellTemplate } from '../../enum/cell-template.enum';
10 import { CdTableColumn } from '../../models/cd-table-column';
11 import { TableComponent } from '../table/table.component';
12 import { TableKeyValueComponent } from './table-key-value.component';
14 describe('TableKeyValueComponent', () => {
15 let component: TableKeyValueComponent;
16 let fixture: ComponentFixture<TableKeyValueComponent>;
19 declarations: [TableComponent, TableKeyValueComponent],
20 imports: [FormsModule, NgxDatatableModule, ComponentsModule, RouterTestingModule]
24 fixture = TestBed.createComponent(TableKeyValueComponent);
25 component = fixture.componentInstance;
28 it('should create', () => {
29 fixture.detectChanges();
30 expect(component).toBeTruthy();
33 it('should make key value object pairs out of arrays with length two', () => {
34 component.data = [['someKey', 0], [3, 'something']];
36 expect(component.tableData.length).toBe(2);
37 expect(component.tableData[0].key).toBe('someKey');
38 expect(component.tableData[1].value).toBe('something');
41 it('should transform arrays', () => {
42 component.data = [['someKey', [1, 2, 3]], [3, 'something']];
44 expect(component.tableData.length).toBe(2);
45 expect(component.tableData[0].key).toBe('someKey');
46 expect(component.tableData[0].value).toBe('1, 2, 3');
47 expect(component.tableData[1].value).toBe('something');
50 it('should remove pure object values', () => {
51 component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
53 expect(component.tableData.length).toBe(1);
54 expect(component.tableData[0].value).toBe('something');
57 it('makes key value object pairs out of an object', () => {
63 expect(component.tableData.length).toBe(2);
64 expect(component.tableData[0].value).toBe('something');
65 expect(component.tableData[1].key).toBe('someKey');
68 it('does nothing if data is correct', () => {
80 expect(component.tableData.length).toBe(2);
81 expect(component.tableData[0].value).toBe('something');
82 expect(component.tableData[1].key).toBe('someKey');
85 it('throws errors if miss match', () => {
87 expect(() => component.ngOnInit()).toThrowError('Wrong data format');
88 component.data = [['someKey', 0, 3]];
89 expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
90 component.data = [{ somekey: 939, somethingElse: 'test' }];
93 describe('Class objects equal plain objects', () => {
97 constructor(deep: boolean) {
99 this.deep = new Example(false);
104 const classExample = new Example(true);
105 const objectExample = {
112 const getTableData = (data) => {
113 component.data = data;
114 expect(() => component.ngOnInit()).not.toThrow();
115 return component.tableData;
118 const doesClassEqualsObject = (classData, objectData, dataLength) => {
119 const classTableData = getTableData(classData);
120 expect(classTableData).toEqual(getTableData(objectData));
121 expect(classTableData.length).toBe(dataLength);
124 it('should convert class objects the same way as plain objects', () => {
125 doesClassEqualsObject(classExample, objectExample, 1);
126 doesClassEqualsObject([classExample], [objectExample], 1);
127 component.renderObjects = true;
128 doesClassEqualsObject(classExample, objectExample, 2);
129 doesClassEqualsObject([classExample], [objectExample], 2);
133 it('tests _makePairs', () => {
134 expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
135 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
136 const pairInverse = [{ key: 'ceph', value: 'mimic' }, { key: 'dash', value: 'board' }];
137 expect(component._makePairs(pair)).toEqual(pairInverse);
138 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
139 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
142 it('tests _makePairsFromArray', () => {
143 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
144 { key: 'dash', value: 'board' }
146 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
147 expect(component._makePairsFromArray(pair)).toEqual(pair);
150 it('tests _makePairsFromObject', () => {
151 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
152 { key: 'dash', value: 'board' }
154 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
155 { key: 'dash', value: 'board' },
156 { key: 'ceph', value: 'mimic' }
160 describe('tests _convertValue', () => {
161 const v = (value) => ({ key: 'sth', value: value });
162 const testConvertValue = (value, result) =>
163 expect(component._convertValue(v(value)).value).toBe(result);
165 it('should leave a string as it is', () => {
166 testConvertValue('something', 'something');
169 it('should leave an int as it is', () => {
170 testConvertValue(29, 29);
173 it('should convert arrays with any type to string', () => {
174 testConvertValue([1, 2, 3], '1, 2, 3');
175 testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
176 testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
179 it('should convert only allow objects if renderObjects is set to true', () => {
180 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
181 component.renderObjects = true;
182 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
186 describe('render objects', () => {
191 anotherSetting2: 'somethingElse',
207 additionalKeyContainingObject: { type: 'none' },
208 keyWithEmptyObject: {}
210 component.renderObjects = true;
213 it('with parent key', () => {
214 component.ngOnInit();
215 expect(component.tableData).toEqual([
216 { key: 'additionalKeyContainingObject type', value: 'none' },
217 { key: 'keyWithEmptyObject', value: '' },
218 { key: 'o2 sub1 x', value: 42 },
219 { key: 'o2 sub2 y', value: 555 },
220 { key: 'options anotherSetting2', value: 'somethingElse' },
221 { key: 'options someSetting1', value: 38 },
222 { key: 'options suboptions sub1', value: 12 },
223 { key: 'options suboptions sub2', value: 34 },
224 { key: 'options suboptions sub3', value: 56 },
225 { key: 'someKey', value: 0 }
229 it('without parent key', () => {
230 component.appendParentKey = false;
231 component.ngOnInit();
232 expect(component.tableData).toEqual([
233 { key: 'anotherSetting2', value: 'somethingElse' },
234 { key: 'keyWithEmptyObject', value: '' },
235 { key: 'someKey', value: 0 },
236 { key: 'someSetting1', value: 38 },
237 { key: 'sub1', value: 12 },
238 { key: 'sub2', value: 34 },
239 { key: 'sub3', value: 56 },
240 { key: 'type', value: 'none' },
241 { key: 'x', value: 42 },
242 { key: 'y', value: 555 }
247 describe('subscribe fetchData', () => {
248 it('should not subscribe fetchData of table', () => {
249 component.ngOnInit();
250 expect(component.table.fetchData.observers.length).toBe(0);
253 it('should call fetchData', () => {
255 component.fetchData.subscribe(() => {
258 component.ngOnInit();
259 expect(component.table.fetchData.observers.length).toBe(1);
260 component.table.fetchData.emit();
261 expect(called).toBeTruthy();
265 describe('hide empty items', () => {
277 someDifferentNumber: 1,
285 component.renderObjects = true;
288 it('should show all items as default', () => {
289 expect(component.hideEmpty).toBe(false);
290 component.ngOnInit();
291 expect(component.tableData).toEqual([
292 { key: 'array', value: '' },
293 { key: 'emptyObject array', value: '' },
294 { key: 'emptyObject object', value: '' },
295 { key: 'emptyObject string', value: '' },
296 { key: 'object', value: '' },
297 { key: 'someArray', value: '0, 1' },
298 { key: 'someDifferentNumber', value: 1 },
299 { key: 'someNumber', value: 0 },
300 { key: 'someObject empty', value: '' },
301 { key: 'someObject something', value: 0.1 },
302 { key: 'someString', value: '0' },
303 { key: 'string', value: '' }
307 it('should hide all empty items', () => {
308 component.hideEmpty = true;
309 component.ngOnInit();
310 expect(component.tableData).toEqual([
311 { key: 'someArray', value: '0, 1' },
312 { key: 'someDifferentNumber', value: 1 },
313 { key: 'someNumber', value: 0 },
314 { key: 'someObject something', value: 0.1 },
315 { key: 'someString', value: '0' }
320 describe('columns set up', () => {
321 let columns: CdTableColumn[];
328 cellTransformation: CellTemplate.bold
337 it('should have the following default column set up', () => {
338 component.ngOnInit();
339 expect(component.columns).toEqual(columns);
342 it('should have the following column set up if customCss is defined', () => {
343 component.customCss = {
344 'answer-of-everything': 42
346 component.ngOnInit();
347 columns[1].cellTransformation = CellTemplate.classAdding;
348 expect(component.columns).toEqual(columns);