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 { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
10 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
11 import { NotificationService } from '~/app/shared/services/notification.service';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
14 import { PoolEditPeerModalComponent } from './pool-edit-peer-modal.component';
15 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
17 describe('PoolEditPeerModalComponent', () => {
18 let component: PoolEditPeerModalComponent;
19 let fixture: ComponentFixture<PoolEditPeerModalComponent>;
20 let notificationService: NotificationService;
21 let rbdMirroringService: RbdMirroringService;
22 let formHelper: FormHelper;
25 declarations: [PoolEditPeerModalComponent],
27 HttpClientTestingModule,
31 ToastrModule.forRoot()
33 providers: [NgbActiveModal]
37 fixture = TestBed.createComponent(PoolEditPeerModalComponent);
38 component = fixture.componentInstance;
39 component.mode = 'add';
40 component.poolName = 'somePool';
42 notificationService = TestBed.inject(NotificationService);
43 spyOn(notificationService, 'show').and.stub();
45 rbdMirroringService = TestBed.inject(RbdMirroringService);
47 formHelper = new FormHelper(component.editPeerForm);
50 it('should create', () => {
51 expect(component).toBeTruthy();
54 describe('add pool peer', () => {
56 component.mode = 'add';
57 component.peerUUID = undefined;
58 spyOn(rbdMirroringService, 'refresh').and.stub();
59 spyOn(component.activeModal, 'close').and.callThrough();
60 fixture.detectChanges();
64 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
65 expect(component.activeModal.close).toHaveBeenCalledTimes(1);
68 it('should call addPeer', () => {
69 spyOn(rbdMirroringService, 'addPeer').and.callFake(() => of(''));
71 component.editPeerForm.patchValue({
72 clusterName: 'cluster',
79 expect(rbdMirroringService.addPeer).toHaveBeenCalledWith('somePool', {
80 cluster_name: 'cluster',
88 describe('edit pool peer', () => {
90 component.mode = 'edit';
91 component.peerUUID = 'somePeer';
93 const response = new PoolEditPeerResponseModel();
94 response.uuid = 'somePeer';
95 response.cluster_name = 'cluster';
96 response.client_id = 'id';
97 response.mon_host = '1.2.3.4:1234';
98 response.key = 'dGVzdA==';
100 spyOn(rbdMirroringService, 'getPeer').and.callFake(() => of(response));
101 spyOn(rbdMirroringService, 'refresh').and.stub();
102 spyOn(component.activeModal, 'close').and.callThrough();
103 fixture.detectChanges();
107 expect(rbdMirroringService.getPeer).toHaveBeenCalledWith('somePool', 'somePeer');
108 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
109 expect(component.activeModal.close).toHaveBeenCalledTimes(1);
112 it('should call updatePeer', () => {
113 spyOn(rbdMirroringService, 'updatePeer').and.callFake(() => of(''));
116 expect(rbdMirroringService.updatePeer).toHaveBeenCalledWith('somePool', 'somePeer', {
117 cluster_name: 'cluster',
119 mon_host: '1.2.3.4:1234',
125 describe('form validation', () => {
127 fixture.detectChanges();
130 it('should validate cluster name', () => {
131 formHelper.expectErrorChange('clusterName', '', 'required');
132 formHelper.expectErrorChange('clusterName', ' ', 'invalidClusterName');
135 it('should validate client ID', () => {
136 formHelper.expectErrorChange('clientID', '', 'required');
137 formHelper.expectErrorChange('clientID', 'client.id', 'invalidClientID');
140 it('should validate monitor address', () => {
141 formHelper.expectErrorChange('monAddr', '@', 'invalidMonAddr');
144 it('should validate key', () => {
145 formHelper.expectErrorChange('key', '(', 'invalidKey');