2 HttpClientTestingModule,
5 } from '@angular/common/http/testing';
6 import { ComponentFixture, TestBed } from '@angular/core/testing';
7 import { ReactiveFormsModule } from '@angular/forms';
8 import { By } from '@angular/platform-browser';
9 import { RouterTestingModule } from '@angular/router/testing';
11 import { BsModalRef } from 'ngx-bootstrap/modal';
12 import { ToastrModule } from 'ngx-toastr';
14 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
15 import { Permission } from '../../../shared/models/permissions';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { IscsiTargetDiscoveryModalComponent } from './iscsi-target-discovery-modal.component';
19 describe('IscsiTargetDiscoveryModalComponent', () => {
20 let component: IscsiTargetDiscoveryModalComponent;
21 let fixture: ComponentFixture<IscsiTargetDiscoveryModalComponent>;
22 let httpTesting: HttpTestingController;
25 const elem = (css: string) => fixture.debugElement.query(By.css(css));
26 const elemDisabled = (css: string) => elem(css).nativeElement.disabled;
29 declarations: [IscsiTargetDiscoveryModalComponent],
31 HttpClientTestingModule,
34 ToastrModule.forRoot(),
37 providers: [i18nProviders, BsModalRef]
41 fixture = TestBed.createComponent(IscsiTargetDiscoveryModalComponent);
42 component = fixture.componentInstance;
43 httpTesting = TestBed.get(HttpTestingController);
46 describe('with update permissions', () => {
48 component.permission = new Permission(['update']);
49 fixture.detectChanges();
50 req = httpTesting.expectOne('api/iscsi/discoveryauth');
53 it('should create', () => {
54 expect(component).toBeTruthy();
57 it('should create form', () => {
58 expect(component.discoveryForm.value).toEqual({
66 it('should patch form', () => {
70 mutual_user: 'mutual_foo',
71 mutual_password: 'mutual_bar'
73 expect(component.discoveryForm.value).toEqual({
76 mutual_user: 'mutual_foo',
77 mutual_password: 'mutual_bar'
81 it('should submit new values', () => {
82 component.discoveryForm.patchValue({
85 mutual_user: 'mutual_new_user',
86 mutual_password: 'mutual_new_pass'
88 component.submitAction();
90 const submit_req = httpTesting.expectOne('api/iscsi/discoveryauth');
91 expect(submit_req.request.method).toBe('PUT');
92 expect(submit_req.request.body).toEqual({
95 mutual_user: 'mutual_new_user',
96 mutual_password: 'mutual_new_pass'
100 it('should enable form if user has update permission', () => {
101 expect(elemDisabled('input#user')).toBeFalsy();
102 expect(elemDisabled('input#password')).toBeFalsy();
103 expect(elemDisabled('input#mutual_user')).toBeFalsy();
104 expect(elemDisabled('input#mutual_password')).toBeFalsy();
105 expect(elem('cd-submit-button')).toBeDefined();
109 it('should disabled form if user does not have update permission', () => {
110 component.permission = new Permission(['read', 'create', 'delete']);
111 fixture.detectChanges();
112 req = httpTesting.expectOne('api/iscsi/discoveryauth');
114 expect(elemDisabled('input#user')).toBeTruthy();
115 expect(elemDisabled('input#password')).toBeTruthy();
116 expect(elemDisabled('input#mutual_user')).toBeTruthy();
117 expect(elemDisabled('input#mutual_password')).toBeTruthy();
118 expect(elem('cd-submit-button')).toBeNull();