1 import { ComponentFixture, TestBed } from '@angular/core/testing';
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';
13 class MultisiteServiceMock {
14 createEditSyncPipe = jest.fn().mockReturnValue(of(null));
17 describe('RgwMultisiteSyncPipeModalComponent', () => {
18 let component: RgwMultisiteSyncPipeModalComponent;
19 let fixture: ComponentFixture<RgwMultisiteSyncPipeModalComponent>;
20 let multisiteServiceMock: MultisiteServiceMock;
22 beforeEach(async () => {
23 await TestBed.configureTestingModule({
24 declarations: [RgwMultisiteSyncPipeModalComponent],
26 HttpClientTestingModule,
27 ToastrModule.forRoot(),
32 providers: [NgbActiveModal, { provide: RgwMultisiteService, useClass: MultisiteServiceMock }]
33 }).compileComponents();
35 fixture = TestBed.createComponent(RgwMultisiteSyncPipeModalComponent);
36 multisiteServiceMock = (TestBed.inject(RgwMultisiteService) as unknown) as MultisiteServiceMock;
37 component = fixture.componentInstance;
38 fixture.detectChanges();
41 it('should create', () => {
42 expect(component).toBeTruthy();
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);
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);
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);
75 it('should call createEditSyncPipe for creating/editing sync pipe', () => {
76 component.editing = false;
77 component.pipeForm.patchValue({
81 source_zones: { added: ['zone1-zg1-realm1'], removed: [] },
82 destination_bucket: '',
83 destination_zones: { added: ['zone2-zg1-realm1'], removed: [] }
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');
90 expect(spy).toHaveBeenCalled();
91 expect(putDataSpy).toHaveBeenCalled();
92 expect(putDataSpy).toHaveBeenCalledWith({
93 ...component.pipeForm.getRawValue(),
99 it('should pass "user" and "mode" while creating/editing pipe', () => {
100 component.editing = true;
101 component.pipeForm.patchValue({
103 group_id: 's3-bucket-replication:enabled',
105 source_zones: { added: ['zone1-zg1-realm1'], removed: [] },
106 destination_bucket: '',
107 destination_zones: { added: ['zone2-zg1-realm1'], removed: [] }
109 component.pipeSelectedRow = {
110 dest: { bucket: '*', zones: ['zone2-zg1-realm1'] },
116 source: { filter: { tags: [] } },
119 source: { bucket: '*', zones: ['zone1-zg1-realm1'] }
122 component.sourceZones.data.selected = ['zone1-zg1-realm1'];
123 component.destZones.data.selected = ['zone2-zg1-realm1'];
124 const spy = jest.spyOn(component, 'submit');
125 const putDataSpy = jest.spyOn(multisiteServiceMock, 'createEditSyncPipe');
127 expect(spy).toHaveBeenCalled();
128 expect(putDataSpy).toHaveBeenCalled();
129 expect(putDataSpy).toHaveBeenCalledWith({
130 ...component.pipeForm.getRawValue(),