/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { EventEmitter } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { of } from 'rxjs';

import { ModalComponent } from './modal.component';
import { MatIconModule } from '@angular/material/icon';

describe('ModalComponent', () => {
  let component: ModalComponent;
  let fixture: ComponentFixture<ModalComponent>;

  const dialog = {
    _containerInstance: {
      _config: {},
    },
    // eslint-disable-next-line no-empty-function
    close: () => {},
    backdropClick: () => of(''),
    keydownEvents: () => of(''),
  };

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [MatDialogModule, MatIconModule],
        declarations: [ModalComponent],
        providers: [
          {
            provide: MatDialogRef,
            useValue: dialog,
          },
        ],
      }).compileComponents();
    })
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(ModalComponent);
    component = fixture.componentInstance;
    component.title = "Test Title";
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should close the dialog if a custom close handler is set', (done) => {
    component.closeHandler = new EventEmitter<any>();

    component.closeHandler.subscribe(() => {
      done();
    });
    component.close();
  });

  it('should close the dialog if no close handler is set', () => {
    jest.spyOn(component.dialogRef, 'close');
    component.close();
    expect(component.dialogRef.close).toHaveBeenCalled();
  });

  it('should contain the correct title', () => {
    const titleNode: HTMLHeadingElement = fixture.nativeElement.querySelector('.dialog-header');
    expect(titleNode.textContent.trim()).toBe('Test Title');
  });
});
