]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
b4c756a16c30e9b73d915a0520b9a670e0f9b1ce
[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 } 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   beforeEach(async () => {
42     await TestBed.configureTestingModule({
43       declarations: [
44         CheckedTableFormComponent,
45         TableComponent,
46         TableKeyValueComponent,
47         TablePaginationComponent
48       ],
49       imports: [NgxDatatableModule]
50     }).compileComponents();
51   });
52
53   beforeEach(() => {
54     fixture = TestBed.createComponent(CheckedTableFormComponent);
55     component = fixture.componentInstance;
56     component.columns = fakeColumns;
57     component.data = [
58       { scope: 'owner', read: true, write: true, execute: true },
59       { scope: 'group', read: true, write: true, execute: true },
60       { scope: 'other', read: true, write: true, execute: true }
61     ];
62     component.scopes = ['owner', 'group', 'others'];
63     component.form = new CdFormGroup({
64       scopes_permissions: new FormControl({})
65     });
66     component.inputField = 'scopes_permissions';
67     component.isTableForOctalMode = true;
68     form = component.form;
69     formHelper = new FormHelper(form);
70     component.ngOnInit();
71     fixture.detectChanges();
72   });
73
74   it('should create', () => {
75     expect(component).toBeTruthy();
76   });
77
78   it('should check all perms for a scope', () => {
79     formHelper.setValue('scopes_permissions', { owner: ['read'] });
80     component.onClickCellCheckbox('group', 'scope');
81     const scopes_permissions = form.getValue('scopes_permissions');
82     expect(Object.keys(scopes_permissions)).toContain('group');
83     expect(scopes_permissions['group']).toEqual(['read', 'write', 'execute'].sort());
84   });
85
86   it('should uncheck all perms for a scope', () => {
87     formHelper.setValue('scopes_permissions', { owner: ['read', 'write', 'execute'] });
88     component.onClickCellCheckbox('owner', 'scope');
89     const scopes_permissions = form.getValue('scopes_permissions');
90     expect(Object.keys(scopes_permissions)).not.toContain('owner');
91   });
92
93   it('should uncheck all scopes and perms', () => {
94     component.scopes = ['owner', 'group'];
95     formHelper.setValue('scopes_permissions', {
96       owner: ['read', 'execute'],
97       group: ['write']
98     });
99     component.onClickHeaderCheckbox('scope', ({
100       target: { checked: false }
101     } as unknown) as Event);
102     const scopes_permissions = form.getValue('scopes_permissions');
103     expect(scopes_permissions).toEqual({});
104   });
105
106   it('should check all scopes and perms', () => {
107     component.scopes = ['owner', 'group'];
108     formHelper.setValue('scopes_permissions', {
109       owner: ['read', 'write'],
110       group: ['execute']
111     });
112     component.onClickHeaderCheckbox('scope', ({ target: { checked: true } } as unknown) as Event);
113     const scopes_permissions = form.getValue('scopes_permissions');
114     const keys = Object.keys(scopes_permissions);
115     expect(keys).toEqual(['owner', 'group']);
116     keys.forEach((key) => {
117       expect(scopes_permissions[key].sort()).toEqual(['execute', 'read', 'write']);
118     });
119   });
120
121   it('should check if column is checked', () => {
122     component.data = [
123       { scope: 'a', read: true, write: true, execute: true },
124       { scope: 'b', read: false, write: true, execute: false }
125     ];
126     expect(component.isRowChecked('a')).toBeTruthy();
127     expect(component.isRowChecked('b')).toBeFalsy();
128     expect(component.isRowChecked('c')).toBeFalsy();
129   });
130
131   it('should check if header is checked', () => {
132     component.data = [
133       { scope: 'a', read: true, write: true, execute: true },
134       { scope: 'b', read: false, write: true, execute: false }
135     ];
136     expect(component.isHeaderChecked('read')).toBeFalsy();
137     expect(component.isHeaderChecked('write')).toBeTruthy();
138     expect(component.isHeaderChecked('execute')).toBeFalsy();
139   });
140 });