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';
6 import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
8 import { configureTestBed } from '../../../../testing/unit-test-helper';
9 import { ComponentsModule } from '../../components/components.module';
10 import { CellTemplate } from '../../enum/cell-template.enum';
11 import { CdTableColumn } from '../../models/cd-table-column';
12 import { CdDatePipe } from '../../pipes/cd-date.pipe';
13 import { TableComponent } from '../table/table.component';
14 import { TableKeyValueComponent } from './table-key-value.component';
16 describe('TableKeyValueComponent', () => {
17 let component: TableKeyValueComponent;
18 let fixture: ComponentFixture<TableKeyValueComponent>;
21 declarations: [TableComponent, TableKeyValueComponent],
27 BsDropdownModule.forRoot()
32 fixture = TestBed.createComponent(TableKeyValueComponent);
33 component = fixture.componentInstance;
36 it('should create', () => {
37 fixture.detectChanges();
38 expect(component).toBeTruthy();
41 it('should make key value object pairs out of arrays with length two', () => {
42 component.data = [['someKey', 0], [3, 'something']];
44 expect(component.tableData.length).toBe(2);
45 expect(component.tableData[0].key).toBe('someKey');
46 expect(component.tableData[1].value).toBe('something');
49 it('should transform arrays', () => {
50 component.data = [['someKey', [1, 2, 3]], [3, 'something']];
52 expect(component.tableData.length).toBe(2);
53 expect(component.tableData[0].key).toBe('someKey');
54 expect(component.tableData[0].value).toBe('1, 2, 3');
55 expect(component.tableData[1].value).toBe('something');
58 it('should remove pure object values', () => {
59 component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
61 expect(component.tableData.length).toBe(1);
62 expect(component.tableData[0].value).toBe('something');
65 it('makes key value object pairs out of an object', () => {
71 expect(component.tableData.length).toBe(2);
72 expect(component.tableData[0].value).toBe('something');
73 expect(component.tableData[1].key).toBe('someKey');
76 it('does nothing if data is correct', () => {
88 expect(component.tableData.length).toBe(2);
89 expect(component.tableData[0].value).toBe('something');
90 expect(component.tableData[1].key).toBe('someKey');
93 it('throws errors if miss match', () => {
95 expect(() => component.ngOnInit()).toThrowError('Wrong data format');
96 component.data = [['someKey', 0, 3]];
97 expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
98 component.data = [{ somekey: 939, somethingElse: 'test' }];
101 describe('Class objects equal plain objects', () => {
105 constructor(deep: boolean) {
107 this.deep = new Example(false);
112 const classExample = new Example(true);
113 const objectExample = {
120 const getTableData = (data) => {
121 component.data = data;
122 expect(() => component.ngOnInit()).not.toThrow();
123 return component.tableData;
126 const doesClassEqualsObject = (classData, objectData, dataLength) => {
127 const classTableData = getTableData(classData);
128 expect(classTableData).toEqual(getTableData(objectData));
129 expect(classTableData.length).toBe(dataLength);
132 it('should convert class objects the same way as plain objects', () => {
133 doesClassEqualsObject(classExample, objectExample, 1);
134 doesClassEqualsObject([classExample], [objectExample], 1);
135 component.renderObjects = true;
136 doesClassEqualsObject(classExample, objectExample, 2);
137 doesClassEqualsObject([classExample], [objectExample], 2);
141 it('tests _makePairs', () => {
142 expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
143 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
144 const pairInverse = [{ key: 'ceph', value: 'mimic' }, { key: 'dash', value: 'board' }];
145 expect(component._makePairs(pair)).toEqual(pairInverse);
146 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
147 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
150 it('tests _makePairsFromArray', () => {
151 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
152 { key: 'dash', value: 'board' }
154 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
155 expect(component._makePairsFromArray(pair)).toEqual(pair);
158 it('tests _makePairsFromObject', () => {
159 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
160 { key: 'dash', value: 'board' }
162 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
163 { key: 'dash', value: 'board' },
164 { key: 'ceph', value: 'mimic' }
168 describe('tests _convertValue', () => {
169 const v = (value) => ({ key: 'sth', value: value });
170 const testConvertValue = (value, result) =>
171 expect(component._convertValue(v(value)).value).toBe(result);
173 it('should leave a string as it is', () => {
174 testConvertValue('something', 'something');
177 it('should leave an int as it is', () => {
178 testConvertValue(29, 29);
181 it('should convert arrays with any type to string', () => {
182 testConvertValue([1, 2, 3], '1, 2, 3');
183 testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
184 testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
187 it('should convert only allow objects if renderObjects is set to true', () => {
188 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
189 component.renderObjects = true;
190 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
193 describe('automatically pipe utc dates through cdDate', () => {
194 let datePipe: CdDatePipe;
197 datePipe = TestBed.get(CdDatePipe);
198 spyOn(datePipe, 'transform').and.callThrough();
201 const expectTimeConversion = (date: string) => {
206 component.ngOnInit();
207 expect(component.tableData.length).toBe(2);
208 expect(datePipe.transform).toHaveBeenCalledWith(date);
209 expect(component.tableData[1].key).not.toBe(date);
212 it('converts some date', () => {
213 expectTimeConversion('2019-04-15 12:26:52.305285');
216 it('converts utc date', () => {
217 expectTimeConversion('2019-04-16T12:35:46.646300974Z');
222 describe('render objects', () => {
227 anotherSetting2: 'somethingElse',
243 additionalKeyContainingObject: { type: 'none' },
244 keyWithEmptyObject: {}
246 component.renderObjects = true;
249 it('with parent key', () => {
250 component.ngOnInit();
251 expect(component.tableData).toEqual([
252 { key: 'additionalKeyContainingObject type', value: 'none' },
253 { key: 'keyWithEmptyObject', value: '' },
254 { key: 'o2 sub1 x', value: 42 },
255 { key: 'o2 sub2 y', value: 555 },
256 { key: 'options anotherSetting2', value: 'somethingElse' },
257 { key: 'options someSetting1', value: 38 },
258 { key: 'options suboptions sub1', value: 12 },
259 { key: 'options suboptions sub2', value: 34 },
260 { key: 'options suboptions sub3', value: 56 },
261 { key: 'someKey', value: 0 }
265 it('without parent key', () => {
266 component.appendParentKey = false;
267 component.ngOnInit();
268 expect(component.tableData).toEqual([
269 { key: 'anotherSetting2', value: 'somethingElse' },
270 { key: 'keyWithEmptyObject', value: '' },
271 { key: 'someKey', value: 0 },
272 { key: 'someSetting1', value: 38 },
273 { key: 'sub1', value: 12 },
274 { key: 'sub2', value: 34 },
275 { key: 'sub3', value: 56 },
276 { key: 'type', value: 'none' },
277 { key: 'x', value: 42 },
278 { key: 'y', value: 555 }
283 describe('subscribe fetchData', () => {
284 it('should not subscribe fetchData of table', () => {
285 component.ngOnInit();
286 expect(component.table.fetchData.observers.length).toBe(0);
289 it('should call fetchData', () => {
291 component.fetchData.subscribe(() => {
294 component.ngOnInit();
295 expect(component.table.fetchData.observers.length).toBe(1);
296 component.table.fetchData.emit();
297 expect(called).toBeTruthy();
301 describe('hide empty items', () => {
315 someDifferentNumber: 1,
323 component.renderObjects = true;
326 it('should show all items as default', () => {
327 expect(component.hideEmpty).toBe(false);
328 component.ngOnInit();
329 expect(component.tableData).toEqual([
330 { key: 'array', value: '' },
331 { key: 'booleanFalse', value: false },
332 { key: 'booleanTrue', value: true },
333 { key: 'emptyObject array', value: '' },
334 { key: 'emptyObject object', value: '' },
335 { key: 'emptyObject string', value: '' },
336 { key: 'object', value: '' },
337 { key: 'someArray', value: '0, 1' },
338 { key: 'someDifferentNumber', value: 1 },
339 { key: 'someNumber', value: 0 },
340 { key: 'someObject empty', value: '' },
341 { key: 'someObject something', value: 0.1 },
342 { key: 'someString', value: '0' },
343 { key: 'string', value: '' }
347 it('should hide all empty items', () => {
348 component.hideEmpty = true;
349 component.ngOnInit();
350 expect(component.tableData).toEqual([
351 { key: 'booleanFalse', value: false },
352 { key: 'booleanTrue', value: true },
353 { key: 'someArray', value: '0, 1' },
354 { key: 'someDifferentNumber', value: 1 },
355 { key: 'someNumber', value: 0 },
356 { key: 'someObject something', value: 0.1 },
357 { key: 'someString', value: '0' }
362 describe('columns set up', () => {
363 let columns: CdTableColumn[];
370 cellTransformation: CellTemplate.bold
379 it('should have the following default column set up', () => {
380 component.ngOnInit();
381 expect(component.columns).toEqual(columns);
384 it('should have the following column set up if customCss is defined', () => {
385 component.customCss = {
386 'answer-of-everything': 42
388 component.ngOnInit();
389 columns[1].cellTransformation = CellTemplate.classAdding;
390 expect(component.columns).toEqual(columns);