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 const pairInverse = [{ key: 'ceph', value: 'mimic' }, { key: 'dash', value: 'board' }];
134 expect(component._makePairs(pair)).toEqual(pairInverse);
135 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
136 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
139 it('tests _makePairsFromArray', () => {
140 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
141 { key: 'dash', value: 'board' }
143 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
144 expect(component._makePairsFromArray(pair)).toEqual(pair);
147 it('tests _makePairsFromObject', () => {
148 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
149 { key: 'dash', value: 'board' }
151 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
152 { key: 'dash', value: 'board' },
153 { key: 'ceph', value: 'mimic' }
157 describe('tests _convertValue', () => {
158 const v = (value) => ({ key: 'sth', value: value });
159 const testConvertValue = (value, result) =>
160 expect(component._convertValue(v(value)).value).toBe(result);
162 it('should leave a string as it is', () => {
163 testConvertValue('something', 'something');
166 it('should leave an int as it is', () => {
167 testConvertValue(29, 29);
170 it('should convert arrays with any type to string', () => {
171 testConvertValue([1, 2, 3], '1, 2, 3');
172 testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
173 testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
176 it('should convert only allow objects if renderObjects is set to true', () => {
177 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
178 component.renderObjects = true;
179 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
183 describe('render objects', () => {
188 anotherSetting2: 'somethingElse',
204 additionalKeyContainingObject: { type: 'none' },
205 keyWithEmptyObject: {}
207 component.renderObjects = true;
210 it('with parent key', () => {
211 component.ngOnInit();
212 expect(component.tableData).toEqual([
213 { key: 'additionalKeyContainingObject type', value: 'none' },
214 { key: 'keyWithEmptyObject', value: '' },
215 { key: 'o2 sub1 x', value: 42 },
216 { key: 'o2 sub2 y', value: 555 },
217 { key: 'options anotherSetting2', value: 'somethingElse' },
218 { key: 'options someSetting1', value: 38 },
219 { key: 'options suboptions sub1', value: 12 },
220 { key: 'options suboptions sub2', value: 34 },
221 { key: 'options suboptions sub3', value: 56 },
222 { key: 'someKey', value: 0 }
226 it('without parent key', () => {
227 component.appendParentKey = false;
228 component.ngOnInit();
229 expect(component.tableData).toEqual([
230 { key: 'anotherSetting2', value: 'somethingElse' },
231 { key: 'keyWithEmptyObject', value: '' },
232 { key: 'someKey', value: 0 },
233 { key: 'someSetting1', value: 38 },
234 { key: 'sub1', value: 12 },
235 { key: 'sub2', value: 34 },
236 { key: 'sub3', value: 56 },
237 { key: 'type', value: 'none' },
238 { key: 'x', value: 42 },
239 { key: 'y', value: 555 }
244 describe('subscribe fetchData', () => {
245 it('should not subscribe fetchData of table', () => {
246 component.ngOnInit();
247 expect(component.table.fetchData.observers.length).toBe(0);
250 it('should call fetchData', () => {
252 component.fetchData.subscribe(() => {
255 component.ngOnInit();
256 expect(component.table.fetchData.observers.length).toBe(1);
257 component.table.fetchData.emit();
258 expect(called).toBeTruthy();
262 describe('hide empty items', () => {
274 someDifferentNumber: 1,
282 component.renderObjects = true;
285 it('should show all items as default', () => {
286 expect(component.hideEmpty).toBe(false);
287 component.ngOnInit();
288 expect(component.tableData).toEqual([
289 { key: 'array', value: '' },
290 { key: 'emptyObject array', value: '' },
291 { key: 'emptyObject object', value: '' },
292 { key: 'emptyObject string', value: '' },
293 { key: 'object', value: '' },
294 { key: 'someArray', value: '0, 1' },
295 { key: 'someDifferentNumber', value: 1 },
296 { key: 'someNumber', value: 0 },
297 { key: 'someObject empty', value: '' },
298 { key: 'someObject something', value: 0.1 },
299 { key: 'someString', value: '0' },
300 { key: 'string', value: '' }
304 it('should hide all empty items', () => {
305 component.hideEmpty = true;
306 component.ngOnInit();
307 expect(component.tableData).toEqual([
308 { key: 'someArray', value: '0, 1' },
309 { key: 'someDifferentNumber', value: 1 },
310 { key: 'someNumber', value: 0 },
311 { key: 'someObject something', value: 0.1 },
312 { key: 'someString', value: '0' }