]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/form-input-disable.directive.spec.ts
Import ceph 15.2.8
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / directives / form-input-disable.directive.spec.ts
1 import { Component, DebugElement, Input } from '@angular/core';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4
5 import { configureTestBed } from '../../../testing/unit-test-helper';
6 import { Permission } from '../models/permissions';
7 import { AuthStorageService } from '../services/auth-storage.service';
8 import { FormInputDisableDirective } from './form-input-disable.directive';
9 import { FormScopeDirective } from './form-scope.directive';
10
11 @Component({
12 template: `
13 <form cdFormScope="osd">
14 <input type="checkbox" />
15 </form>
16 `
17 })
18 export class FormDisableComponent {}
19
20 class MockFormScopeDirective {
21 @Input() cdFormScope = 'osd';
22 }
23
24 describe('FormInputDisableDirective', () => {
25 let fakePermissions: Permission;
26 let authStorageService: AuthStorageService;
27 let directive: FormInputDisableDirective;
28 let fixture: ComponentFixture<FormDisableComponent>;
29 let inputElement: DebugElement;
30 configureTestBed({
31 declarations: [FormScopeDirective, FormInputDisableDirective, FormDisableComponent]
32 });
33
34 beforeEach(() => {
35 directive = new FormInputDisableDirective(
36 new MockFormScopeDirective(),
37 new AuthStorageService(),
38 null
39 );
40
41 fakePermissions = {
42 create: false,
43 update: false,
44 read: false,
45 delete: false
46 };
47 authStorageService = TestBed.get(AuthStorageService);
48 spyOn(authStorageService, 'getPermissions').and.callFake(() => ({
49 osd: fakePermissions
50 }));
51
52 fixture = TestBed.createComponent(FormDisableComponent);
53 inputElement = fixture.debugElement.query(By.css('input'));
54 });
55
56 afterEach(() => {
57 directive = null;
58 });
59
60 it('should create an instance', () => {
61 expect(directive).toBeTruthy();
62 });
63
64 it('should disable the input if update permission is false', () => {
65 fixture.detectChanges();
66 expect(inputElement.nativeElement.disabled).toBeTruthy();
67 });
68
69 it('should not disable the input if update permission is true', () => {
70 fakePermissions.update = true;
71 fakePermissions.read = false;
72 fixture.detectChanges();
73 expect(inputElement.nativeElement.disabled).toBeFalsy();
74 });
75 });