import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { RemissionFinishComponent } from './remission-finish.component';
import { RemissionClientModule } from '../../remission-client.module';
import { NgxsModule } from '@ngxs/store';
import { RouterModule } from '@angular/router';
import { RemissionService } from '@isa/remission';
import { OAuthModule } from 'angular-oauth2-oidc';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { SsoModule } from 'sso';
import { NgIdleModule } from '@ng-idle/core';
import { AppConfiguration } from 'apps/sales/src/app/app-configuration';
import { of } from 'rxjs';

describe('RemissionFinishComponent', () => {
  let component: RemissionFinishComponent;
  let fixture: ComponentFixture<RemissionFinishComponent>;
  let element: HTMLElement;
  const confirmationModal = jasmine.createSpyObj('RemissionScanConfirmationDialogComponent', ['openDialog']);
  const invalidBarcodeModal = jasmine.createSpyObj('RemissionScanProductInvalidBarcodeComponent', { ['openDialog']: of({}) });

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RemissionClientModule,
        HttpClientTestingModule,
        NgxsModule.forRoot(),
        RouterModule.forRoot([]),
        OAuthModule.forRoot(),
        SsoModule.forRoot(),
        NgIdleModule.forRoot(),
      ],
      providers: [
        RemissionService,
        {
          provide: AppConfiguration,
          useValue: {} as AppConfiguration,
        },
      ],
    }).compileComponents();

    fixture = TestBed.createComponent(RemissionFinishComponent);

    component = fixture.componentInstance;
    element = fixture.nativeElement;
  }));

  it('should open the confirmation dialog after scanning the barcode', () => {
    spyOn(component, 'handleScanResult').and.callThrough();
    fixture.detectChanges();

    component.confirmScanModal = confirmationModal;

    component.handleScanResult('123456789');

    expect(component.confirmScanModal.openDialog).toHaveBeenCalledTimes(1);
  });

  it('should open the invalid barcode dialog if no barcode has been scanned', () => {
    spyOn(component, 'handleScanResult').and.callThrough();
    fixture.detectChanges();

    component.invalidBarcodeDialog = invalidBarcodeModal;

    component.handleScanResult('');

    expect(component.invalidBarcodeDialog.openDialog).toHaveBeenCalledTimes(1);
  });
});
