1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { FormsModule } from '@angular/forms';
3 import { RouterTestingModule } from '@angular/router/testing';
5 import { NgbDropdownModule, NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
6 import { NgxDatatableModule } from '@swimlane/ngx-datatable';
7 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
9 import { ComponentsModule } from '~/app/shared/components/components.module';
10 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
11 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
12 import { CdDatePipe } from '~/app/shared/pipes/cd-date.pipe';
13 import { PipesModule } from '~/app/shared/pipes/pipes.module';
14 import { configureTestBed } from '~/testing/unit-test-helper';
15 import { TablePaginationComponent } from '../table-pagination/table-pagination.component';
16 import { TableComponent } from '../table/table.component';
17 import { TableKeyValueComponent } from './table-key-value.component';
19 describe('TableKeyValueComponent', () => {
20 let component: TableKeyValueComponent;
21 let fixture: ComponentFixture<TableKeyValueComponent>;
24 declarations: [TableComponent, TableKeyValueComponent, TablePaginationComponent],
38 fixture = TestBed.createComponent(TableKeyValueComponent);
39 component = fixture.componentInstance;
42 it('should create', () => {
43 fixture.detectChanges();
44 expect(component).toBeTruthy();
47 it('should make key value object pairs out of arrays with length two', () => {
50 ['arrayKey', [1, 2, 3]],
54 const expected: any = [
55 { key: 'arrayKey', value: '1, 2, 3' },
56 { key: 'someKey', value: 0 },
57 { key: 3, value: 'something' }
59 expect(component.tableData).toEqual(expected);
62 it('should not show data supposed to be have hidden by key', () => {
67 component.hideKeys = ['a'];
69 expect(component.tableData).toEqual([{ key: 'b', value: 2 }]);
72 it('should remove items with objects as values', () => {
75 ['will be removed', { a: 3, b: 4, c: 5 }]
78 expect(component.tableData).toEqual(<any>[{ key: 3, value: 'something' }]);
81 it('makes key value object pairs out of an object', () => {
82 component.data = { 3: 'something', someKey: 0 };
84 expect(component.tableData).toEqual([
85 { key: '3', value: 'something' },
86 { key: 'someKey', value: 0 }
90 it('does nothing if data does not need to be converted', () => {
92 { key: 3, value: 'something' },
93 { key: 'someKey', value: 0 }
96 expect(component.tableData).toEqual(component.data);
99 it('throws errors if data cannot be converted', () => {
101 expect(() => component.ngOnInit()).toThrowError('Wrong data format');
102 component.data = [['someKey', 0, 3]];
103 expect(() => component.ngOnInit()).toThrowError(
104 'Array contains too many elements (3). Needs to be of type [string, any][]'
108 it('tests makePairs()', () => {
109 const makePairs = (data: any) => component['makePairs'](data);
110 expect(makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
112 { key: 'dash', value: 'board' },
113 { key: 'ceph', value: 'mimic' }
115 const pairInverse = [
116 { key: 'ceph', value: 'mimic' },
117 { key: 'dash', value: 'board' }
119 expect(makePairs(pair)).toEqual(pairInverse);
120 expect(makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
121 expect(makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
124 it('tests makePairsFromArray()', () => {
125 const makePairsFromArray = (data: any[]) => component['makePairsFromArray'](data);
126 expect(makePairsFromArray([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
128 { key: 'dash', value: 'board' },
129 { key: 'ceph', value: 'mimic' }
131 expect(makePairsFromArray(pair)).toEqual(pair);
134 it('tests makePairsFromObject()', () => {
135 const makePairsFromObject = (data: object) => component['makePairsFromObject'](data);
136 expect(makePairsFromObject({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
137 expect(makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
138 { key: 'dash', value: 'board' },
139 { key: 'ceph', value: 'mimic' }
143 describe('tests convertValue()', () => {
144 const convertValue = (data: any) => component['convertValue'](data);
145 const expectConvertValue = (value: any, expectation: any) =>
146 expect(convertValue(value)).toBe(expectation);
148 it('should not convert strings', () => {
149 expectConvertValue('something', 'something');
152 it('should not convert integers', () => {
153 expectConvertValue(29, 29);
156 it('should convert arrays with any type to strings', () => {
157 expectConvertValue([1, 2, 3], '1, 2, 3');
158 expectConvertValue([{ sth: 'something' }], '{"sth":"something"}');
159 expectConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
162 it('should only convert objects if renderObjects is set to true', () => {
163 expect(convertValue({ sth: 'something' })).toBe(null);
164 component.renderObjects = true;
165 expect(convertValue({ sth: 'something' })).toEqual({ sth: 'something' });
169 describe('automatically pipe UTC dates through cdDate', () => {
170 let datePipe: CdDatePipe;
173 datePipe = TestBed.inject(CdDatePipe);
174 spyOn(datePipe, 'transform').and.callThrough();
177 const expectTimeConversion = (date: string) => {
178 component.data = { dateKey: date };
179 component.ngOnInit();
180 expect(datePipe.transform).toHaveBeenCalledWith(date);
181 expect(component.tableData[0].key).not.toBe(date);
184 it('converts some date', () => {
185 expectTimeConversion('2019-04-15 12:26:52.305285');
188 it('converts UTC date', () => {
189 expectTimeConversion('2019-04-16T12:35:46.646300974Z');
193 describe('render objects', () => {
198 stringKey: 'somethingElse',
213 additionalKeyContainingObject: { type: 'none' },
214 keyWithEmptyObject: {}
216 component.renderObjects = true;
219 it('with parent key', () => {
220 component.ngOnInit();
221 expect(component.tableData).toEqual([
222 { key: 'additionalKeyContainingObject type', value: 'none' },
223 { key: 'keyWithEmptyObject', value: '' },
224 { key: 'options numberKey', value: 38 },
225 { key: 'options objectKey sub1', value: 12 },
226 { key: 'options objectKey sub2', value: 34 },
227 { key: 'options objectKey sub3', value: 56 },
228 { key: 'options stringKey', value: 'somethingElse' },
229 { key: 'otherOptions sub1 x', value: 42 },
230 { key: 'otherOptions sub2 y', value: 555 }
234 it('without parent key', () => {
235 component.appendParentKey = false;
236 component.ngOnInit();
237 expect(component.tableData).toEqual([
238 { key: 'keyWithEmptyObject', value: '' },
239 { key: 'numberKey', value: 38 },
240 { key: 'stringKey', value: 'somethingElse' },
241 { key: 'sub1', value: 12 },
242 { key: 'sub2', value: 34 },
243 { key: 'sub3', value: 56 },
244 { key: 'type', value: 'none' },
245 { key: 'x', value: 42 },
246 { key: 'y', value: 555 }
251 describe('subscribe fetchData', () => {
252 it('should not subscribe fetchData of table', () => {
253 component.ngOnInit();
254 expect(component.table.fetchData.observers.length).toBe(0);
257 it('should call fetchData', () => {
259 component.fetchData.subscribe(() => {
262 component.ngOnInit();
263 expect(component.table.fetchData.observers.length).toBe(1);
264 component.table.fetchData.emit();
265 expect(called).toBeTruthy();
269 describe('hide empty items', () => {
283 someDifferentNumber: 1,
291 component.renderObjects = true;
294 it('should show all items as default', () => {
295 expect(component.hideEmpty).toBe(false);
296 component.ngOnInit();
297 expect(component.tableData).toEqual([
298 { key: 'array', value: '' },
299 { key: 'booleanFalse', value: false },
300 { key: 'booleanTrue', value: true },
301 { key: 'emptyObject array', value: '' },
302 { key: 'emptyObject object', value: '' },
303 { key: 'emptyObject string', value: '' },
304 { key: 'object', value: '' },
305 { key: 'someArray', value: '0, 1' },
306 { key: 'someDifferentNumber', value: 1 },
307 { key: 'someNumber', value: 0 },
308 { key: 'someObject empty', value: '' },
309 { key: 'someObject something', value: 0.1 },
310 { key: 'someString', value: '0' },
311 { key: 'string', value: '' }
315 it('should hide all empty items', () => {
316 component.hideEmpty = true;
317 component.ngOnInit();
318 expect(component.tableData).toEqual([
319 { key: 'booleanFalse', value: false },
320 { key: 'booleanTrue', value: true },
321 { key: 'someArray', value: '0, 1' },
322 { key: 'someDifferentNumber', value: 1 },
323 { key: 'someNumber', value: 0 },
324 { key: 'someObject something', value: 0.1 },
325 { key: 'someString', value: '0' }
330 describe('columns set up', () => {
331 let columns: CdTableColumn[];
335 { prop: 'key', flexGrow: 1, cellTransformation: CellTemplate.bold },
336 { prop: 'value', flexGrow: 3 }
340 it('should have the following default column set up', () => {
341 component.ngOnInit();
342 expect(component.columns).toEqual(columns);
345 it('should have the following column set up if customCss is defined', () => {
346 component.customCss = { 'class-name': 42 };
347 component.ngOnInit();
348 columns[1].cellTransformation = CellTemplate.classAdding;
349 expect(component.columns).toEqual(columns);