summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/form-input-disable.directive.spec.ts
blob: a79043b78fcceb4f4d195c79f5ae89c099d7b4dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Component, DebugElement, Input } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { configureTestBed } from '~/testing/unit-test-helper';
import { Permission } from '../models/permissions';
import { AuthStorageService } from '../services/auth-storage.service';
import { FormInputDisableDirective } from './form-input-disable.directive';
import { FormScopeDirective } from './form-scope.directive';

@Component({
  template: `
    <form cdFormScope="osd">
      <input type="checkbox" />
    </form>
  `
})
export class FormDisableComponent {}

class MockFormScopeDirective {
  @Input() cdFormScope = 'osd';
}

describe('FormInputDisableDirective', () => {
  let fakePermissions: Permission;
  let authStorageService: AuthStorageService;
  let directive: FormInputDisableDirective;
  let fixture: ComponentFixture<FormDisableComponent>;
  let inputElement: DebugElement;
  configureTestBed({
    declarations: [FormScopeDirective, FormInputDisableDirective, FormDisableComponent]
  });

  beforeEach(() => {
    directive = new FormInputDisableDirective(
      new MockFormScopeDirective(),
      new AuthStorageService(),
      null
    );

    fakePermissions = {
      create: false,
      update: false,
      read: false,
      delete: false
    };
    authStorageService = TestBed.inject(AuthStorageService);
    spyOn(authStorageService, 'getPermissions').and.callFake(() => ({
      osd: fakePermissions
    }));

    fixture = TestBed.createComponent(FormDisableComponent);
    inputElement = fixture.debugElement.query(By.css('input'));
  });

  afterEach(() => {
    directive = null;
  });

  it('should create an instance', () => {
    expect(directive).toBeTruthy();
  });

  it('should disable the input if update permission is false', () => {
    fixture.detectChanges();
    expect(inputElement.nativeElement.disabled).toBeTruthy();
  });

  it('should not disable the input if update permission is true', () => {
    fakePermissions.update = true;
    fakePermissions.read = false;
    fixture.detectChanges();
    expect(inputElement.nativeElement.disabled).toBeFalsy();
  });
});