]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
16e05bd1818c3d1355de58e41f146aee565fc299
[ceph.git] /
1 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2 import { FormsModule } from '@angular/forms';
3 import { RouterTestingModule } from '@angular/router/testing';
4
5 import { NgxDatatableModule } from '@swimlane/ngx-datatable';
6
7 import { ComponentsModule } from '../../components/components.module';
8 import { TableComponent } from '../table/table.component';
9 import { TableKeyValueComponent } from './table-key-value.component';
10
11 describe('TableKeyValueComponent', () => {
12   let component: TableKeyValueComponent;
13   let fixture: ComponentFixture<TableKeyValueComponent>;
14
15   beforeEach(async(() => {
16     TestBed.configureTestingModule({
17       declarations: [ TableComponent, TableKeyValueComponent ],
18       imports: [ FormsModule, NgxDatatableModule, ComponentsModule, RouterTestingModule ]
19     })
20     .compileComponents();
21   }));
22
23   beforeEach(() => {
24     fixture = TestBed.createComponent(TableKeyValueComponent);
25     component = fixture.componentInstance;
26   });
27
28   it('should create', () => {
29     expect(component).toBeTruthy();
30   });
31
32   it('should make key value object pairs out of arrays with length two', () => {
33     component.data = [
34       ['someKey', 0],
35       [3, 'something'],
36     ];
37     component.ngOnInit();
38     expect(component.tableData.length).toBe(2);
39     expect(component.tableData[0].key).toBe('someKey');
40     expect(component.tableData[1].value).toBe('something');
41   });
42
43   it('should transform arrays', () => {
44     component.data = [
45       ['someKey', [1, 2, 3]],
46       [3, 'something']
47     ];
48     component.ngOnInit();
49     expect(component.tableData.length).toBe(2);
50     expect(component.tableData[0].key).toBe('someKey');
51     expect(component.tableData[0].value).toBe('1, 2, 3');
52     expect(component.tableData[1].value).toBe('something');
53   });
54
55   it('should remove pure object values', () => {
56     component.data = [
57       [3, 'something'],
58       ['will be removed', { a: 3, b: 4, c: 5}]
59     ];
60     component.ngOnInit();
61     expect(component.tableData.length).toBe(1);
62     expect(component.tableData[0].value).toBe('something');
63   });
64
65   it('should make key value object pairs out of an object', () => {
66     component.data = {
67       3: 'something',
68       someKey: 0
69     };
70     component.ngOnInit();
71     expect(component.tableData.length).toBe(2);
72     expect(component.tableData[0].value).toBe('something');
73     expect(component.tableData[1].key).toBe('someKey');
74   });
75
76   it('should make do nothing if data is correct', () => {
77     component.data = [
78       {
79         key: 3,
80         value: 'something'
81       },
82       {
83         key: 'someKey',
84         value: 0
85       }
86     ];
87     component.ngOnInit();
88     expect(component.tableData.length).toBe(2);
89     expect(component.tableData[0].value).toBe('something');
90     expect(component.tableData[1].key).toBe('someKey');
91   });
92
93   it('should throw error if miss match', () => {
94     component.data = 38;
95     expect(() => component.ngOnInit()).toThrowError('Wrong data format');
96     component.data = [['someKey', 0, 3]];
97     expect(() => component.ngOnInit()).toThrowError('Wrong array format');
98   });
99 });