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

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatButtonModule } from '@angular/material/button';
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { AlertBoxOptions } from '../models/alert-box-options';
import { AlertClosingType, AlertType, PrimaryButton } from '../models/alert.enums';
import { AlertDialogComponent } from './alert-dialog.component';
import { BCI_ALERT_DIALOG_INTL_PROVIDER } from '../bci-alert-dialog-intl';

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

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [
          MatIconModule,
          MatDialogModule,
          MatButtonModule,
          RouterTestingModule,
          NoopAnimationsModule,
        ],
        declarations: [AlertDialogComponent],
        providers: [
          BCI_ALERT_DIALOG_INTL_PROVIDER,
          {
            provide: MatDialogRef,
            useValue: {
              // eslint-disable-next-line no-empty-function
              close: () => {},
            },
          },
          {
            provide: MAT_DIALOG_DATA,
            useValue: {
              title: 'Test Title',
              type: AlertType.Warning,
              leftButtonTitle: 'LeftBtn',
              description: 'Description',
              infoText: 'Info Text',
              primaryButton: PrimaryButton.LeftButton,
              rightButtonTitle: 'RightBtn',
              outerLeftButtonTitle: 'OLeftBtn',
              outerLeftButtonDisabled: false,
            } as AlertBoxOptions, // Add any data you wish to test if it is passed/used correctly
          },
        ],
      }).compileComponents();
    })
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(AlertDialogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

  it('close should close the matDialog', () => {
    jest.spyOn(component.dialogRef, 'close');
    component.close(AlertClosingType.LeftButton);
    expect(component.dialogRef.close).toHaveBeenCalled();
  });

  it('close should display the matDialogData', () => {
    const dialogNode: HTMLElement = fixture.nativeElement;
    const description: HTMLElement = dialogNode.querySelector('[data-test="alert-dialog-description"]');
    const infoText: HTMLElement = dialogNode.querySelector('.infoText');
    const warningIconNode: HTMLElement = dialogNode.querySelector(
      'mat-icon[fontIcon~="bosch-ic-alert-warning"]'
    );
    const leftRaisedBtn: HTMLElement = dialogNode.querySelector('button[bciPrimaryButton]');
    const rightBtn: HTMLButtonElement = dialogNode.querySelector('button[data-test="alert-dialog-right-button"]');
    expect(infoText.textContent.trim()).toBe('Info Text');
    expect(description.textContent.trim()).toBe('Description');
    expect(warningIconNode).toBeTruthy();
    expect(leftRaisedBtn.innerHTML).toContain('LeftBtn');
    expect(rightBtn.innerHTML).toContain('RightBtn');
  });
});
