1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { ToastModule } from 'ng2-toastr';
7 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
8 import { of } from 'rxjs';
14 } from '../../../../../testing/unit-test-helper';
15 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
16 import { NotificationService } from '../../../../shared/services/notification.service';
17 import { SharedModule } from '../../../../shared/shared.module';
18 import { PoolEditPeerModalComponent } from './pool-edit-peer-modal.component';
19 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
21 describe('PoolEditPeerModalComponent', () => {
22 let component: PoolEditPeerModalComponent;
23 let fixture: ComponentFixture<PoolEditPeerModalComponent>;
24 let notificationService: NotificationService;
25 let rbdMirroringService: RbdMirroringService;
26 let formHelper: FormHelper;
29 declarations: [PoolEditPeerModalComponent],
31 HttpClientTestingModule,
37 providers: [BsModalRef, BsModalService, i18nProviders]
41 fixture = TestBed.createComponent(PoolEditPeerModalComponent);
42 component = fixture.componentInstance;
43 component.mode = 'add';
44 component.poolName = 'somePool';
46 notificationService = TestBed.get(NotificationService);
47 spyOn(notificationService, 'show').and.stub();
49 rbdMirroringService = TestBed.get(RbdMirroringService);
51 formHelper = new FormHelper(component.editPeerForm);
54 it('should create', () => {
55 expect(component).toBeTruthy();
58 describe('add pool peer', () => {
60 component.mode = 'add';
61 component.peerUUID = undefined;
62 spyOn(rbdMirroringService, 'refresh').and.stub();
63 spyOn(component.modalRef, 'hide').and.callThrough();
64 fixture.detectChanges();
68 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
69 expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
72 it('should call addPeer', () => {
73 spyOn(rbdMirroringService, 'addPeer').and.callFake(() => of(''));
75 component.editPeerForm.patchValue({
76 clusterName: 'cluster',
83 expect(rbdMirroringService.addPeer).toHaveBeenCalledWith('somePool', {
84 cluster_name: 'cluster',
92 describe('edit pool peer', () => {
94 component.mode = 'edit';
95 component.peerUUID = 'somePeer';
97 const response = new PoolEditPeerResponseModel();
98 response.uuid = 'somePeer';
99 response.cluster_name = 'cluster';
100 response.client_id = 'id';
101 response.mon_host = '1.2.3.4:1234';
102 response.key = 'dGVzdA==';
104 spyOn(rbdMirroringService, 'getPeer').and.callFake(() => of(response));
105 spyOn(rbdMirroringService, 'refresh').and.stub();
106 spyOn(component.modalRef, 'hide').and.callThrough();
107 fixture.detectChanges();
111 expect(rbdMirroringService.getPeer).toHaveBeenCalledWith('somePool', 'somePeer');
112 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
113 expect(component.modalRef.hide).toHaveBeenCalledTimes(1);
116 it('should call updatePeer', () => {
117 spyOn(rbdMirroringService, 'updatePeer').and.callFake(() => of(''));
120 expect(rbdMirroringService.updatePeer).toHaveBeenCalledWith('somePool', 'somePeer', {
121 cluster_name: 'cluster',
123 mon_host: '1.2.3.4:1234',
129 describe('form validation', () => {
131 fixture.detectChanges();
134 it('should validate cluster name', () => {
135 formHelper.expectErrorChange('clusterName', '', 'required');
136 formHelper.expectErrorChange('clusterName', ' ', 'invalidClusterName');
139 it('should validate client ID', () => {
140 formHelper.expectErrorChange('clientID', '', 'required');
141 formHelper.expectErrorChange('clientID', 'client.id', 'invalidClientID');
144 it('should validate monitor address', () => {
145 formHelper.expectErrorChange('monAddr', '@', 'invalidMonAddr');
148 it('should validate key', () => {
149 formHelper.expectErrorChange('key', '(', 'invalidKey');