]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
21ef3a4f8b74d4d10ddf7688cc68714a14233875
[ceph.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2
3 import { CheckedTableFormComponent } from './checked-table-form.component';
4 import { TableComponent } from '../table/table.component';
5 import { TableKeyValueComponent } from '../table-key-value/table-key-value.component';
6 import { TablePaginationComponent } from '../table-pagination/table-pagination.component';
7 import { NgxDatatableModule } from '@swimlane/ngx-datatable';
8 import { FormHelper, configureTestBed } from '~/testing/unit-test-helper';
9 import { CdFormGroup } from '../../forms/cd-form-group';
10 import { FormControl } from '@angular/forms';
11
12 describe('CheckedTableFormComponent', () => {
13   let component: CheckedTableFormComponent;
14   let fixture: ComponentFixture<CheckedTableFormComponent>;
15   let formHelper: FormHelper;
16   let form: CdFormGroup;
17
18   let fakeColumns = [
19     {
20       prop: 'scope',
21       name: $localize`All`,
22       flexGrow: 1
23     },
24     {
25       prop: 'read',
26       name: $localize`Read`,
27       flexGrow: 1
28     },
29     {
30       prop: 'write',
31       name: $localize`Write`,
32       flexGrow: 1
33     },
34     {
35       prop: 'execute',
36       name: $localize`Execute`,
37       flexGrow: 1
38     }
39   ];
40
41   configureTestBed({
42     declarations: [
43       CheckedTableFormComponent,
44       TableComponent,
45       TableKeyValueComponent,
46       TablePaginationComponent
47     ],
48     imports: [NgxDatatableModule]
49   });
50
51   beforeEach(() => {
52     fixture = TestBed.createComponent(CheckedTableFormComponent);
53     component = fixture.componentInstance;
54     component.columns = fakeColumns;
55     component.data = [
56       { scope: 'owner', read: true, write: true, execute: true },
57       { scope: 'group', read: true, write: true, execute: true },
58       { scope: 'other', read: true, write: true, execute: true }
59     ];
60     component.scopes = ['owner', 'group', 'others'];
61     component.form = new CdFormGroup({
62       scopes_permissions: new FormControl({})
63     });
64     component.inputField = 'scopes_permissions';
65     component.isTableForOctalMode = true;
66     form = component.form;
67     formHelper = new FormHelper(form);
68     component.ngOnInit();
69     fixture.detectChanges();
70   });
71
72   it('should create', () => {
73     expect(component).toBeTruthy();
74   });
75
76   it('should check all perms for a scope', () => {
77     formHelper.setValue('scopes_permissions', { owner: ['read'] });
78     component.onClickCellCheckbox('group', 'scope');
79     const scopes_permissions = form.getValue('scopes_permissions');
80     expect(Object.keys(scopes_permissions)).toContain('group');
81     expect(scopes_permissions['group']).toEqual(['read', 'write', 'execute'].sort());
82   });
83
84   it('should uncheck all perms for a scope', () => {
85     formHelper.setValue('scopes_permissions', { owner: ['read', 'write', 'execute'] });
86     component.onClickCellCheckbox('owner', 'scope');
87     const scopes_permissions = form.getValue('scopes_permissions');
88     expect(Object.keys(scopes_permissions)).not.toContain('owner');
89   });
90
91   it('should uncheck all scopes and perms', () => {
92     component.scopes = ['owner', 'group'];
93     formHelper.setValue('scopes_permissions', {
94       owner: ['read', 'execute'],
95       group: ['write']
96     });
97     component.onClickHeaderCheckbox('scope', ({
98       target: { checked: false }
99     } as unknown) as Event);
100     const scopes_permissions = form.getValue('scopes_permissions');
101     expect(scopes_permissions).toEqual({});
102   });
103
104   it('should check all scopes and perms', () => {
105     component.scopes = ['owner', 'group'];
106     formHelper.setValue('scopes_permissions', {
107       owner: ['read', 'write'],
108       group: ['execute']
109     });
110     component.onClickHeaderCheckbox('scope', ({ target: { checked: true } } as unknown) as Event);
111     const scopes_permissions = form.getValue('scopes_permissions');
112     const keys = Object.keys(scopes_permissions);
113     expect(keys).toEqual(['owner', 'group']);
114     keys.forEach((key) => {
115       expect(scopes_permissions[key].sort()).toEqual(['execute', 'read', 'write']);
116     });
117   });
118
119   it('should check if column is checked', () => {
120     component.data = [
121       { scope: 'a', read: true, write: true, execute: true },
122       { scope: 'b', read: false, write: true, execute: false }
123     ];
124     expect(component.isRowChecked('a')).toBeTruthy();
125     expect(component.isRowChecked('b')).toBeFalsy();
126     expect(component.isRowChecked('c')).toBeFalsy();
127   });
128
129   it('should check if header is checked', () => {
130     component.data = [
131       { scope: 'a', read: true, write: true, execute: true },
132       { scope: 'b', read: false, write: true, execute: false }
133     ];
134     expect(component.isHeaderChecked('read')).toBeFalsy();
135     expect(component.isHeaderChecked('write')).toBeTruthy();
136     expect(component.isHeaderChecked('execute')).toBeFalsy();
137   });
138 });