]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
369658d7d427fd22b2c3785b15a94965b07dbbac
[ceph.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2
3 import { RgwMultisiteSyncPipeModalComponent } from './rgw-multisite-sync-pipe-modal.component';
4 import { HttpClientTestingModule } from '@angular/common/http/testing';
5 import { ToastrModule } from 'ngx-toastr';
6 import { PipesModule } from '~/app/shared/pipes/pipes.module';
7 import { ReactiveFormsModule } from '@angular/forms';
8 import { CommonModule } from '@angular/common';
9 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
10 import { of } from 'rxjs';
11 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
12
13 class MultisiteServiceMock {
14   createEditSyncPipe = jest.fn().mockReturnValue(of(null));
15 }
16
17 describe('RgwMultisiteSyncPipeModalComponent', () => {
18   let component: RgwMultisiteSyncPipeModalComponent;
19   let fixture: ComponentFixture<RgwMultisiteSyncPipeModalComponent>;
20   let multisiteServiceMock: MultisiteServiceMock;
21
22   beforeEach(async () => {
23     await TestBed.configureTestingModule({
24       declarations: [RgwMultisiteSyncPipeModalComponent],
25       imports: [
26         HttpClientTestingModule,
27         ToastrModule.forRoot(),
28         PipesModule,
29         ReactiveFormsModule,
30         CommonModule
31       ],
32       providers: [NgbActiveModal, { provide: RgwMultisiteService, useClass: MultisiteServiceMock }]
33     }).compileComponents();
34
35     fixture = TestBed.createComponent(RgwMultisiteSyncPipeModalComponent);
36     multisiteServiceMock = (TestBed.inject(RgwMultisiteService) as unknown) as MultisiteServiceMock;
37     component = fixture.componentInstance;
38     fixture.detectChanges();
39   });
40
41   it('should create', () => {
42     expect(component).toBeTruthy();
43   });
44
45   it('should replace `*` with `All Zones (*)`', () => {
46     let zones = ['*', 'zone1-zg1-realm1', 'zone2-zg1-realm1'];
47     let mockReturnVal = ['All Zones (*)', 'zone1-zg1-realm1', 'zone2-zg1-realm1'];
48     const spy = jest.spyOn(component, 'replaceAsteriskWithString').mockReturnValue(mockReturnVal);
49     const res = component.replaceAsteriskWithString(zones);
50     expect(spy).toHaveBeenCalled();
51     expect(spy).toHaveBeenCalledWith(zones);
52     expect(res).toEqual(mockReturnVal);
53   });
54
55   it('should replace `All Zones (*)` with `*`', () => {
56     let zones = ['All Zones (*)', 'zone1-zg1-realm1', 'zone2-zg1-realm1'];
57     let mockReturnVal = ['*', 'zone1-zg1-realm1', 'zone2-zg1-realm1'];
58     const spy = jest.spyOn(component, 'replaceWithAsterisk').mockReturnValue(mockReturnVal);
59     const res = component.replaceWithAsterisk(zones);
60     expect(spy).toHaveBeenCalled();
61     expect(spy).toHaveBeenCalledWith(zones);
62     expect(res).toEqual(mockReturnVal);
63   });
64
65   it('should assign zone value', () => {
66     let zonesAdded: string[] = [];
67     let selectedZone = ['zone2-zg1-realm1'];
68     const spy = jest.spyOn(component, 'assignZoneValue').mockReturnValue(selectedZone);
69     const res = component.assignZoneValue(zonesAdded, selectedZone);
70     expect(spy).toHaveBeenCalled();
71     expect(spy).toHaveBeenCalledWith(zonesAdded, selectedZone);
72     expect(res).toEqual(selectedZone);
73   });
74
75   it('should call createEditSyncPipe for creating/editing sync pipe', () => {
76     component.editing = false;
77     component.pipeForm.patchValue({
78       pipe_id: 'pipe1',
79       group_id: 'new',
80       source_bucket: '',
81       source_zones: { added: ['zone1-zg1-realm1'], removed: [] },
82       destination_bucket: '',
83       destination_zones: { added: ['zone2-zg1-realm1'], removed: [] }
84     });
85     component.sourceZones.data.selected = ['zone1-zg1-realm1'];
86     component.destZones.data.selected = ['zone2-zg1-realm1'];
87     const spy = jest.spyOn(component, 'submit');
88     const putDataSpy = jest.spyOn(multisiteServiceMock, 'createEditSyncPipe');
89     component.submit();
90     expect(spy).toHaveBeenCalled();
91     expect(putDataSpy).toHaveBeenCalled();
92     expect(putDataSpy).toHaveBeenCalledWith(component.pipeForm.getRawValue());
93   });
94 });