]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
3 | import { ReactiveFormsModule } from '@angular/forms'; | |
4 | import { RouterTestingModule } from '@angular/router/testing'; | |
5 | ||
6 | import * as _ from 'lodash'; | |
11fdf7f2 | 7 | import { BsModalRef, ModalModule } from 'ngx-bootstrap/modal'; |
494da23a | 8 | import { ToastrModule } from 'ngx-toastr'; |
11fdf7f2 TL |
9 | |
10 | import { configureTestBed, i18nProviders } from '../../../../../testing/unit-test-helper'; | |
11 | import { NotificationType } from '../../../../shared/enum/notification-type.enum'; | |
12 | import { NotificationService } from '../../../../shared/services/notification.service'; | |
13 | import { SharedModule } from '../../../../shared/shared.module'; | |
14 | import { OsdFlagsModalComponent } from './osd-flags-modal.component'; | |
15 | ||
16 | function getFlagsArray(component: OsdFlagsModalComponent) { | |
17 | const allFlags = _.cloneDeep(component.allFlags); | |
18 | allFlags['purged_snapdirs'].value = true; | |
19 | allFlags['pause'].value = true; | |
20 | return _.toArray(allFlags); | |
21 | } | |
22 | ||
23 | describe('OsdFlagsModalComponent', () => { | |
24 | let component: OsdFlagsModalComponent; | |
25 | let fixture: ComponentFixture<OsdFlagsModalComponent>; | |
26 | let httpTesting: HttpTestingController; | |
27 | ||
28 | configureTestBed({ | |
29 | imports: [ | |
30 | ReactiveFormsModule, | |
31 | ModalModule.forRoot(), | |
32 | SharedModule, | |
33 | HttpClientTestingModule, | |
34 | RouterTestingModule, | |
494da23a | 35 | ToastrModule.forRoot() |
11fdf7f2 TL |
36 | ], |
37 | declarations: [OsdFlagsModalComponent], | |
38 | providers: [BsModalRef, i18nProviders] | |
39 | }); | |
40 | ||
41 | beforeEach(() => { | |
42 | httpTesting = TestBed.get(HttpTestingController); | |
43 | fixture = TestBed.createComponent(OsdFlagsModalComponent); | |
44 | component = fixture.componentInstance; | |
45 | }); | |
46 | ||
47 | it('should create', () => { | |
48 | expect(component).toBeTruthy(); | |
49 | }); | |
50 | ||
51 | it('should finish running ngOnInit', () => { | |
52 | fixture.detectChanges(); | |
53 | ||
54 | const flags = getFlagsArray(component); | |
55 | ||
56 | const req = httpTesting.expectOne('api/osd/flags'); | |
57 | req.flush(['purged_snapdirs', 'pause', 'foo']); | |
58 | ||
59 | expect(component.flags).toEqual(flags); | |
60 | expect(component.unknownFlags).toEqual(['foo']); | |
61 | }); | |
62 | ||
e306af50 | 63 | describe('test submitAction', function () { |
11fdf7f2 TL |
64 | let notificationType: NotificationType; |
65 | let notificationService: NotificationService; | |
66 | let bsModalRef: BsModalRef; | |
67 | ||
68 | beforeEach(() => { | |
69 | notificationService = TestBed.get(NotificationService); | |
70 | spyOn(notificationService, 'show').and.callFake((type) => { | |
71 | notificationType = type; | |
72 | }); | |
73 | ||
74 | bsModalRef = TestBed.get(BsModalRef); | |
75 | spyOn(bsModalRef, 'hide').and.callThrough(); | |
76 | component.unknownFlags = ['foo']; | |
77 | }); | |
78 | ||
79 | it('should run submitAction', () => { | |
80 | component.flags = getFlagsArray(component); | |
81 | component.submitAction(); | |
82 | const req = httpTesting.expectOne('api/osd/flags'); | |
83 | req.flush(['purged_snapdirs', 'pause', 'foo']); | |
84 | expect(req.request.body).toEqual({ flags: ['pause', 'purged_snapdirs', 'foo'] }); | |
85 | ||
86 | expect(notificationType).toBe(NotificationType.success); | |
87 | expect(component.bsModalRef.hide).toHaveBeenCalledTimes(1); | |
88 | }); | |
89 | ||
90 | it('should hide modal if request fails', () => { | |
91 | component.flags = []; | |
92 | component.submitAction(); | |
93 | const req = httpTesting.expectOne('api/osd/flags'); | |
94 | req.flush([], { status: 500, statusText: 'failure' }); | |
95 | ||
96 | expect(notificationService.show).toHaveBeenCalledTimes(0); | |
97 | expect(component.bsModalRef.hide).toHaveBeenCalledTimes(1); | |
98 | }); | |
99 | }); | |
100 | }); |