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 { ComponentsModule } from '../../components/components.module';
8 import { configureTestBed } from '../../unit-test-helper';
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();
28 expect(component._convertValue).toBeTruthy();
29 expect(component._makePairs).toBeTruthy();
30 expect(component._makePairsFromObject).toBeTruthy();
31 expect(component._makePairsFromArray).toBeTruthy();
32 expect(component._insertFlattenObjects).toBeTruthy();
35 it('should make key value object pairs out of arrays with length two', () => {
36 component.data = [['someKey', 0], [3, 'something']];
38 expect(component.tableData.length).toBe(2);
39 expect(component.tableData[0].key).toBe('someKey');
40 expect(component.tableData[1].value).toBe('something');
43 it('should transform arrays', () => {
44 component.data = [['someKey', [1, 2, 3]], [3, 'something']];
46 expect(component.tableData.length).toBe(2);
47 expect(component.tableData[0].key).toBe('someKey');
48 expect(component.tableData[0].value).toBe('1, 2, 3');
49 expect(component.tableData[1].value).toBe('something');
52 it('should remove pure object values', () => {
53 component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
55 expect(component.tableData.length).toBe(1);
56 expect(component.tableData[0].value).toBe('something');
59 it('makes key value object pairs out of an object', () => {
65 expect(component.tableData.length).toBe(2);
66 expect(component.tableData[0].value).toBe('something');
67 expect(component.tableData[1].key).toBe('someKey');
70 it('does nothing if data is correct', () => {
82 expect(component.tableData.length).toBe(2);
83 expect(component.tableData[0].value).toBe('something');
84 expect(component.tableData[1].key).toBe('someKey');
87 it('throws errors if miss match', () => {
89 expect(() => component.ngOnInit()).toThrowError('Wrong data format');
90 component.data = [['someKey', 0, 3]];
91 expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
92 component.data = [{ somekey: 939, somethingElse: 'test' }];
93 expect(() => component.ngOnInit()).toThrowError(
94 'Wrong object array format: {key: string, value: any}[]'
98 it('tests _makePairs', () => {
99 expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
100 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
101 expect(component._makePairs(pair)).toEqual(pair);
102 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
103 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pair);
106 it('tests _makePairsFromArray', () => {
107 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
108 { key: 'dash', value: 'board' }
110 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
111 expect(component._makePairsFromArray(pair)).toEqual(pair);
114 it('tests _makePairsFromObject', () => {
115 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
116 { key: 'dash', value: 'board' }
118 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
119 { key: 'dash', value: 'board' },
120 { key: 'ceph', value: 'mimic' }
124 it('tests _convertValue', () => {
125 const v = (value) => ({ key: 'sth', value: value });
126 expect(component._convertValue(v('something'))).toEqual(v('something'));
127 expect(component._convertValue(v([1, 2, 3]))).toEqual(v('1, 2, 3'));
128 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
129 component.renderObjects = true;
130 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
133 it('tests _insertFlattenObjects', () => {
134 component.renderObjects = true;
151 expect(component._insertFlattenObjects(v)).toEqual([
152 { key: 'no', value: 'change' },
153 { key: 'first second l3_1', value: 33 },
154 { key: 'first second l3_2', value: 44 },
155 { key: 'first layer', value: 'something' }
159 describe('render objects', () => {
164 anotherSetting2: 'somethingElse',
173 component.renderObjects = true;
176 it('with parent key', () => {
177 component.ngOnInit();
178 expect(component.tableData).toEqual([
179 { key: 'options someSetting1', value: 38 },
180 { key: 'options anotherSetting2', value: 'somethingElse' },
181 { key: 'options suboptions sub1', value: 12 },
182 { key: 'options suboptions sub2', value: 34 },
183 { key: 'options suboptions sub3', value: 56 },
184 { key: 'someKey', value: 0 }
188 it('without parent key', () => {
189 component.appendParentKey = false;
190 component.ngOnInit();
191 expect(component.tableData).toEqual([
192 { key: 'someSetting1', value: 38 },
193 { key: 'anotherSetting2', value: 'somethingElse' },
194 { key: 'sub1', value: 12 },
195 { key: 'sub2', value: 34 },
196 { key: 'sub3', value: 56 },
197 { key: 'someKey', value: 0 }