import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing'; import { AnomalyModalComponent } from './anomaly.modal'; import { BsModalRef, ModalModule } from 'ngx-bootstrap/modal'; import { ANOMALY_DATA, FEATURE_IMPORTANCE } from 'projects/esp-common/src/lib/mockup'; import { ModalService } from '../../../../../df/src/app/services/modal.service'; import { BrowserModule } from '@angular/platform-browser'; import { CommentsModalComponent } from '../comments-modal/comments.modal'; import { AddCommentComponent } from '../comments-modal/components/add-comment/add.comment.component'; import { ListCommentsComponent } from '../comments-modal/components/list-comments/list.comments.component'; import { FormsModule } from '@angular/forms'; // import { ApolloModule } from 'apollo-angular'; import { RouterTestingModule } from '@angular/router/testing'; describe('AnomalyModal', () => { let component: AnomalyModalComponent; let fixture: ComponentFixture; let bsRef: BsModalRef; let modalService: ModalService; beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ declarations: [AnomalyModalComponent, CommentsModalComponent, AddCommentComponent, ListCommentsComponent], providers: [BsModalRef, ModalService], imports: [ModalModule.forRoot(), FormsModule, RouterTestingModule.withRoutes([])], }) .overrideModule(BrowserModule, { set: { entryComponents: [CommentsModalComponent] } }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(AnomalyModalComponent); bsRef = TestBed.inject(BsModalRef); modalService = TestBed.inject(ModalService); component = fixture.componentInstance; component.data = ANOMALY_DATA.data; component.bsModalRef = new BsModalRef(); component.bsModalRef.hide = jest.fn(); component.activeAnomaly = 1; component.totalAnomaly = 0; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should match the anomaly length', () => { const anomalyLength = ANOMALY_DATA.data.anomaly.length; expect(component.totalAnomaly).toBe(anomalyLength); }); it('should load the previous anomaly and stop', () => { component.prevAnomaly(); component.activeAnomaly === 1 ? (component.activeAnomaly = 1) : component.activeAnomaly--; const curAnomaly = ANOMALY_DATA.data.anomaly[component.activeAnomaly - 1]; component.getCurrentAnomaly(); expect(component.currentAnomaly).toBe(curAnomaly); }); it('should load the previous anomaly and change', () => { component.activeAnomaly = 2; component.prevAnomaly(); component.activeAnomaly === 1 ? (component.activeAnomaly = 1) : component.activeAnomaly--; const curAnomaly = ANOMALY_DATA.data.anomaly[component.activeAnomaly - 1]; component.getCurrentAnomaly(); expect(component.currentAnomaly).toBe(curAnomaly); }); it('should load the next anomaly and stop', () => { component.nextAnomaly(); component.activeAnomaly === ANOMALY_DATA.data.anomaly.length ? (component.activeAnomaly = ANOMALY_DATA.data.anomaly.length) : component.activeAnomaly++; const curAnomaly = ANOMALY_DATA.data.anomaly[component.activeAnomaly - 2]; component.getCurrentAnomaly(); expect(component.currentAnomaly).toBe(curAnomaly); }); it('should load the next anomaly and change', () => { component.activeAnomaly = ANOMALY_DATA.data.anomaly.length; component.nextAnomaly(); component.activeAnomaly === ANOMALY_DATA.data.anomaly.length ? (component.activeAnomaly = ANOMALY_DATA.data.anomaly.length) : component.activeAnomaly++; const curAnomaly = ANOMALY_DATA.data.anomaly[component.activeAnomaly - 1]; component.getCurrentAnomaly(); expect(component.currentAnomaly).toBe(curAnomaly); }); it('should fire the comments modal', () => { const commentsModal = jest.fn(); component.viewComments(); const featureImportanceData = { data: { title: 'Comments', data: FEATURE_IMPORTANCE.data, categories: FEATURE_IMPORTANCE.categories, }, }; modalService.openCommentsModal({ class: 'modal-comments-custom', initialState: featureImportanceData }); expect(commentsModal).toBeTruthy(); }); it('should fire bsRef hide method onClose', () => { component.onClose(); expect(component.bsModalRef.hide).toHaveBeenCalled(); }); });