]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
2c2ceedb0bc4e3ca6fca15d098e76266cf1704a1
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import _ from 'lodash';
8 import { of as observableOf } from 'rxjs';
9 import { ToastrModule } from 'ngx-toastr';
10 import { RgwRealmService } from '~/app/shared/api/rgw-realm.service';
11 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
12 import { NotificationService } from '~/app/shared/services/notification.service';
13 import { SharedModule } from '~/app/shared/shared.module';
14
15 import { RgwMultisiteRealmFormComponent } from './rgw-multisite-realm-form.component';
16
17 describe('RgwMultisiteRealmFormComponent', () => {
18   let component: RgwMultisiteRealmFormComponent;
19   let fixture: ComponentFixture<RgwMultisiteRealmFormComponent>;
20   let rgwRealmService: RgwRealmService;
21
22   beforeEach(async () => {
23     await TestBed.configureTestingModule({
24       imports: [
25         SharedModule,
26         ReactiveFormsModule,
27         RouterTestingModule,
28         HttpClientTestingModule,
29         ToastrModule.forRoot()
30       ],
31       providers: [NgbActiveModal],
32       declarations: [RgwMultisiteRealmFormComponent]
33     }).compileComponents();
34   });
35
36   beforeEach(() => {
37     fixture = TestBed.createComponent(RgwMultisiteRealmFormComponent);
38     component = fixture.componentInstance;
39     fixture.detectChanges();
40   });
41
42   it('should create', () => {
43     expect(component).toBeTruthy();
44   });
45
46   describe('submit form', () => {
47     let notificationService: NotificationService;
48
49     beforeEach(() => {
50       spyOn(TestBed.inject(Router), 'navigate').and.stub();
51       notificationService = TestBed.inject(NotificationService);
52       spyOn(notificationService, 'show');
53       rgwRealmService = TestBed.inject(RgwRealmService);
54     });
55
56     it('should validate name', () => {
57       component.action = 'create';
58       component.createForm();
59       const control = component.multisiteRealmForm.get('realmName');
60       expect(_.isFunction(control.validator)).toBeTruthy();
61     });
62
63     it('should not validate name', () => {
64       component.action = 'edit';
65       component.createForm();
66       const control = component.multisiteRealmForm.get('realmName');
67       expect(control.asyncValidator).toBeNull();
68     });
69
70     it('tests create success notification', () => {
71       spyOn(rgwRealmService, 'create').and.returnValue(observableOf([]));
72       component.action = 'create';
73       component.multisiteRealmForm.markAsDirty();
74       component.submit();
75       expect(notificationService.show).toHaveBeenCalledWith(
76         NotificationType.success,
77         "Realm: 'null' created successfully"
78       );
79     });
80
81     it('tests update success notification', () => {
82       spyOn(rgwRealmService, 'update').and.returnValue(observableOf([]));
83       component.action = 'edit';
84       component.info = {
85         data: { name: 'null' }
86       };
87       component.multisiteRealmForm.markAsDirty();
88       component.submit();
89       expect(notificationService.show).toHaveBeenCalledWith(
90         NotificationType.success,
91         "Realm: 'null' updated successfully"
92       );
93     });
94   });
95 });