import { TestBed, waitForAsync } from '@angular/core/testing';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { AlertService } from '@bci-web-core/core';
import { of } from 'rxjs';
import { LeavingApiService } from './leaving-api.service';
import { PinboardI18nService } from './pinboard-i18n.service';

export class AlertServiceMock {
  openAlertBox() {
    return {
      afterClosed: () => of('left'),
    };
  }
}

describe('LeavingApiService', () => {
  let dialogRef: AlertService;

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [MatDialogModule, BrowserAnimationsModule, NoopAnimationsModule],
        providers: [
          PinboardI18nService,
          { provide: MatDialogRef, useValue: {} },
          { provide: AlertService, useClass: AlertServiceMock },
        ],
      }).compileComponents();
      dialogRef = TestBed.inject(AlertService);
    })
  );

  it('should be created', () => {
    const service: LeavingApiService = TestBed.inject(LeavingApiService);
    expect(service).toBeTruthy();
  });

  it('should create subscription to Modal closing event and return true, false or undefined value', () => {
    const service: LeavingApiService = TestBed.inject(LeavingApiService);
    jest.spyOn(dialogRef, 'openAlertBox');
    service.showLeavingApiConfirmation('dashboard', true);
    expect(dialogRef.openAlertBox).toHaveBeenCalled();
    service.pageRoute$.next(true);
  });
});
