]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
1127db1c59a59bdb477f8bcc01bfd5fc4d6727a2
[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({
93       ...component.pipeForm.getRawValue(),
94       mode: '',
95       user: ''
96     });
97   });
98
99   it('should pass "user" and "mode" while creating/editing pipe', () => {
100     component.editing = true;
101     component.pipeForm.patchValue({
102       pipe_id: 'pipe1',
103       group_id: 's3-bucket-replication:enabled',
104       source_bucket: '',
105       source_zones: { added: ['zone1-zg1-realm1'], removed: [] },
106       destination_bucket: '',
107       destination_zones: { added: ['zone2-zg1-realm1'], removed: [] }
108     });
109     component.pipeSelectedRow = {
110       dest: { bucket: '*', zones: ['zone2-zg1-realm1'] },
111       id: 'pipi1',
112       params: {
113         dest: {},
114         mode: 'user',
115         priority: 0,
116         source: { filter: { tags: [] } },
117         user: 'dashboard'
118       },
119       source: { bucket: '*', zones: ['zone1-zg1-realm1'] }
120     };
121
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');
126     component.submit();
127     expect(spy).toHaveBeenCalled();
128     expect(putDataSpy).toHaveBeenCalled();
129     expect(putDataSpy).toHaveBeenCalledWith({
130       ...component.pipeForm.getRawValue(),
131       mode: 'user',
132       user: 'dashboard'
133     });
134   });
135 });