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();
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' }];
88 expect(() => component.ngOnInit()).toThrowError(
89 'Wrong object array format: {key: string, value: any}[]'
93 it('tests _makePairs', () => {
94 expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
95 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
96 expect(component._makePairs(pair)).toEqual(pair);
97 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
98 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pair);
101 it('tests _makePairsFromArray', () => {
102 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
103 { key: 'dash', value: 'board' }
105 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
106 expect(component._makePairsFromArray(pair)).toEqual(pair);
109 it('tests _makePairsFromObject', () => {
110 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
111 { key: 'dash', value: 'board' }
113 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
114 { key: 'dash', value: 'board' },
115 { key: 'ceph', value: 'mimic' }
119 describe('tests _convertValue', () => {
120 const v = (value) => ({ key: 'sth', value: value });
121 const testConvertValue = (value, result) =>
122 expect(component._convertValue(v(value)).value).toBe(result);
124 it('should leave a string as it is', () => {
125 testConvertValue('something', 'something');
128 it('should leave an int as it is', () => {
129 testConvertValue(29, 29);
132 it('should convert arrays with any type to string', () => {
133 testConvertValue([1, 2, 3], '1, 2, 3');
134 testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
135 testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
138 it('should convert only allow objects if renderObjects is set to true', () => {
139 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
140 component.renderObjects = true;
141 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
145 it('tests _insertFlattenObjects', () => {
146 component.renderObjects = true;
163 expect(component._insertFlattenObjects(v)).toEqual([
164 { key: 'no', value: 'change' },
165 { key: 'first second l3_1', value: 33 },
166 { key: 'first second l3_2', value: 44 },
167 { key: 'first layer', value: 'something' }
171 describe('render objects', () => {
176 anotherSetting2: 'somethingElse',
185 component.renderObjects = true;
188 it('with parent key', () => {
189 component.ngOnInit();
190 expect(component.tableData).toEqual([
191 { key: 'options someSetting1', value: 38 },
192 { key: 'options anotherSetting2', value: 'somethingElse' },
193 { key: 'options suboptions sub1', value: 12 },
194 { key: 'options suboptions sub2', value: 34 },
195 { key: 'options suboptions sub3', value: 56 },
196 { key: 'someKey', value: 0 }
200 it('without parent key', () => {
201 component.appendParentKey = false;
202 component.ngOnInit();
203 expect(component.tableData).toEqual([
204 { key: 'someSetting1', value: 38 },
205 { key: 'anotherSetting2', value: 'somethingElse' },
206 { key: 'sub1', value: 12 },
207 { key: 'sub2', value: 34 },
208 { key: 'sub3', value: 56 },
209 { key: 'someKey', value: 0 }