import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ResultBarChartComponent } from './result-bar-chart.component';
import { ResultTipComponent } from './result-tip.component';
import { ResultTipHistogramComponent } from './result-tip-histogram/result-tip-histogram.component';
import { HistogramChartComponent } from './histogram-chart/histogram-chart.component';
import {
    JobLogDefaultService,
    JobLogService,
    ScanningResultDefaultService,
    ScanningResultService,
} from '../../../../../shared/services';
import { VULNERABILITY_SCAN_STATUS } from '../../../../../shared/units/utils';
import { NativeReportSummary } from '../../../../../../../ng-swagger-gen/models/native-report-summary';
import { SharedTestingModule } from '../../../../../shared/shared.module';

describe('ResultBarChartComponent (inline template)', () => {
    let component: ResultBarChartComponent;
    let fixture: ComponentFixture<ResultBarChartComponent>;
    let mockData: NativeReportSummary = {
        scan_status: VULNERABILITY_SCAN_STATUS.SUCCESS,
        severity: 'High',
        end_time: new Date().toUTCString(),
        summary: {
            total: 124,
            fixable: 50,
            summary: {
                High: 5,
                Low: 5,
            },
        },
    };

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            imports: [SharedTestingModule],
            declarations: [
                ResultBarChartComponent,
                ResultTipComponent,
                ResultTipHistogramComponent,
                HistogramChartComponent,
            ],
            providers: [
                {
                    provide: ScanningResultService,
                    useValue: ScanningResultDefaultService,
                },
                { provide: JobLogService, useValue: JobLogDefaultService },
            ],
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(ResultBarChartComponent);
        component = fixture.componentInstance;
        component.artifactDigest = 'mockTag';
        component.summary = mockData;
        fixture.detectChanges();
    });

    it('should be created', () => {
        expect(component).toBeTruthy();
    });
    it('should show "scan stopped" if status is STOPPED', () => {
        component.summary.scan_status = VULNERABILITY_SCAN_STATUS.STOPPED;
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            fixture.detectChanges();
            let el: HTMLElement = fixture.nativeElement.querySelector('span');
            expect(el).toBeTruthy();
            expect(el.textContent).toEqual('VULNERABILITY.STATE.STOPPED');
        });
    });

    it('should show progress if status is SCANNING', () => {
        component.summary.scan_status = VULNERABILITY_SCAN_STATUS.RUNNING;
        fixture.detectChanges();

        fixture.whenStable().then(() => {
            fixture.detectChanges();

            let el: HTMLElement =
                fixture.nativeElement.querySelector('.progress');
            expect(el).toBeTruthy();
        });
    });

    it('should show QUEUED if status is QUEUED', () => {
        component.summary.scan_status = VULNERABILITY_SCAN_STATUS.PENDING;
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            fixture.detectChanges();

            let el: HTMLElement =
                fixture.nativeElement.querySelector('.bar-state');
            expect(el).toBeTruthy();
            let el2: HTMLElement = el.querySelector('span');
            expect(el2).toBeTruthy();
            expect(el2.textContent).toEqual('VULNERABILITY.STATE.QUEUED');
        });
    });

    it('should show summary bar chart if status is COMPLETED', () => {
        component.summary.scan_status = VULNERABILITY_SCAN_STATUS.SUCCESS;
        fixture.detectChanges();

        fixture.whenStable().then(() => {
            fixture.detectChanges();
            let el: HTMLElement = fixture.nativeElement.querySelector(
                'hbr-result-tip-histogram'
            );
            expect(el).not.toBeNull();
        });
    });
});
