]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
3 | import { FormControl, ReactiveFormsModule } from '@angular/forms'; | |
4 | import { Router } from '@angular/router'; | |
5 | import { RouterTestingModule } from '@angular/router/testing'; | |
6 | ||
494da23a | 7 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
8 | import { of as observableOf } from 'rxjs'; |
9 | ||
10 | import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper'; | |
11 | import { RgwBucketService } from '../../../shared/api/rgw-bucket.service'; | |
12 | import { NotificationType } from '../../../shared/enum/notification-type.enum'; | |
13 | import { NotificationService } from '../../../shared/services/notification.service'; | |
14 | import { SharedModule } from '../../../shared/shared.module'; | |
15 | import { RgwBucketFormComponent } from './rgw-bucket-form.component'; | |
16 | ||
17 | describe('RgwBucketFormComponent', () => { | |
18 | let component: RgwBucketFormComponent; | |
19 | let fixture: ComponentFixture<RgwBucketFormComponent>; | |
20 | let rwgBucketService: RgwBucketService; | |
21 | ||
22 | configureTestBed({ | |
23 | declarations: [RgwBucketFormComponent], | |
24 | imports: [ | |
25 | HttpClientTestingModule, | |
26 | ReactiveFormsModule, | |
27 | RouterTestingModule, | |
28 | SharedModule, | |
494da23a | 29 | ToastrModule.forRoot() |
11fdf7f2 TL |
30 | ], |
31 | providers: [i18nProviders] | |
32 | }); | |
33 | ||
34 | beforeEach(() => { | |
35 | fixture = TestBed.createComponent(RgwBucketFormComponent); | |
36 | component = fixture.componentInstance; | |
37 | fixture.detectChanges(); | |
38 | rwgBucketService = TestBed.get(RgwBucketService); | |
39 | }); | |
40 | ||
41 | it('should create', () => { | |
42 | expect(component).toBeTruthy(); | |
43 | }); | |
44 | ||
45 | describe('bucketNameValidator', () => { | |
46 | it('should validate name (1/4)', () => { | |
47 | const validatorFn = component.bucketNameValidator(); | |
48 | const ctrl = new FormControl(''); | |
49 | const validatorPromise = validatorFn(ctrl); | |
50 | expect(validatorPromise instanceof Promise).toBeTruthy(); | |
51 | if (validatorPromise instanceof Promise) { | |
52 | validatorPromise.then((resp) => { | |
53 | expect(resp).toBe(null); | |
54 | }); | |
55 | } | |
56 | }); | |
57 | ||
58 | it('should validate name (2/4)', () => { | |
59 | const validatorFn = component.bucketNameValidator(); | |
60 | const ctrl = new FormControl('ab'); | |
61 | ctrl.markAsDirty(); | |
62 | const validatorPromise = validatorFn(ctrl); | |
63 | expect(validatorPromise instanceof Promise).toBeTruthy(); | |
64 | if (validatorPromise instanceof Promise) { | |
65 | validatorPromise.then((resp) => { | |
66 | expect(resp.bucketNameInvalid).toBeTruthy(); | |
67 | }); | |
68 | } | |
69 | }); | |
70 | ||
71 | it('should validate name (3/4)', () => { | |
72 | const validatorFn = component.bucketNameValidator(); | |
73 | const ctrl = new FormControl('abc'); | |
74 | ctrl.markAsDirty(); | |
75 | const validatorPromise = validatorFn(ctrl); | |
76 | expect(validatorPromise instanceof Promise).toBeTruthy(); | |
77 | if (validatorPromise instanceof Promise) { | |
78 | validatorPromise.then((resp) => { | |
79 | expect(resp).toBe(null); | |
80 | }); | |
81 | } | |
82 | }); | |
83 | ||
84 | it('should validate name (4/4)', () => { | |
85 | spyOn(rwgBucketService, 'enumerate').and.returnValue(observableOf(['abcd'])); | |
86 | const validatorFn = component.bucketNameValidator(); | |
87 | const ctrl = new FormControl('abcd'); | |
88 | ctrl.markAsDirty(); | |
89 | const validatorPromise = validatorFn(ctrl); | |
90 | expect(validatorPromise instanceof Promise).toBeTruthy(); | |
91 | if (validatorPromise instanceof Promise) { | |
92 | validatorPromise.then((resp) => { | |
93 | expect(resp instanceof Object).toBeTruthy(); | |
94 | expect(resp.bucketNameExists).toBeTruthy(); | |
95 | }); | |
96 | } | |
97 | }); | |
98 | }); | |
99 | ||
100 | describe('submit form', () => { | |
101 | let notificationService: NotificationService; | |
102 | ||
103 | beforeEach(() => { | |
104 | spyOn(TestBed.get(Router), 'navigate').and.stub(); | |
105 | notificationService = TestBed.get(NotificationService); | |
106 | spyOn(notificationService, 'show'); | |
107 | }); | |
108 | ||
109 | it('tests create success notification', () => { | |
110 | spyOn(rwgBucketService, 'create').and.returnValue(observableOf([])); | |
111 | component.editing = false; | |
112 | component.bucketForm.markAsDirty(); | |
113 | component.submit(); | |
114 | expect(notificationService.show).toHaveBeenCalledWith( | |
115 | NotificationType.success, | |
116 | 'Created Object Gateway bucket ""' | |
117 | ); | |
118 | }); | |
119 | ||
120 | it('tests update success notification', () => { | |
121 | spyOn(rwgBucketService, 'update').and.returnValue(observableOf([])); | |
122 | component.editing = true; | |
123 | component.bucketForm.markAsDirty(); | |
124 | component.submit(); | |
125 | expect(notificationService.show).toHaveBeenCalledWith( | |
126 | NotificationType.success, | |
127 | 'Updated Object Gateway bucket ""' | |
128 | ); | |
129 | }); | |
130 | }); | |
131 | }); |