1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
5 import * as _ from 'lodash';
6 import { ToastModule } from 'ng2-toastr';
7 import { BsModalRef, ModalModule } from 'ngx-bootstrap';
9 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
10 import { NotificationService } from '../../../../shared/services/notification.service';
11 import { SharedModule } from '../../../../shared/shared.module';
12 import { configureTestBed } from '../../../../shared/unit-test-helper';
13 import { OsdFlagsModalComponent } from './osd-flags-modal.component';
15 function getFlagsArray(component: OsdFlagsModalComponent) {
16 const allFlags = _.cloneDeep(component.allFlags);
17 allFlags['purged_snapdirs'].value = true;
18 allFlags['pause'].value = true;
19 return _.toArray(allFlags);
22 describe('OsdFlagsModalComponent', () => {
23 let component: OsdFlagsModalComponent;
24 let fixture: ComponentFixture<OsdFlagsModalComponent>;
25 let httpTesting: HttpTestingController;
30 ModalModule.forRoot(),
32 HttpClientTestingModule,
35 declarations: [OsdFlagsModalComponent],
36 providers: [BsModalRef]
40 httpTesting = TestBed.get(HttpTestingController);
41 fixture = TestBed.createComponent(OsdFlagsModalComponent);
42 component = fixture.componentInstance;
45 it('should create', () => {
46 expect(component).toBeTruthy();
49 it('should finish running ngOnInit', () => {
50 fixture.detectChanges();
52 const flags = getFlagsArray(component);
54 const req = httpTesting.expectOne('api/osd/flags');
55 req.flush(['purged_snapdirs', 'pause', 'foo']);
57 expect(component.flags).toEqual(flags);
58 expect(component.unknownFlags).toEqual(['foo']);
61 describe('test submitAction', function() {
62 let notificationType: NotificationType;
63 let notificationService: NotificationService;
64 let bsModalRef: BsModalRef;
67 notificationService = TestBed.get(NotificationService);
68 spyOn(notificationService, 'show').and.callFake((type) => {
69 notificationType = type;
72 bsModalRef = TestBed.get(BsModalRef);
73 spyOn(bsModalRef, 'hide').and.callThrough();
74 component.unknownFlags = ['foo'];
77 it('should run submitAction', () => {
78 component.flags = getFlagsArray(component);
79 component.submitAction();
80 const req = httpTesting.expectOne('api/osd/flags');
81 req.flush(['purged_snapdirs', 'pause', 'foo']);
82 expect(req.request.body).toEqual({ flags: ['pause', 'purged_snapdirs', 'foo'] });
84 expect(notificationType).toBe(NotificationType.success);
85 expect(component.bsModalRef.hide).toHaveBeenCalledTimes(1);
88 it('should hide modal if request fails', () => {
90 component.submitAction();
91 const req = httpTesting.expectOne('api/osd/flags');
92 req.flush([], { status: 500, statusText: 'failure' });
94 expect(notificationService.show).toHaveBeenCalledTimes(0);
95 expect(component.bsModalRef.hide).toHaveBeenCalledTimes(1);