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

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatIconModule } from '@angular/material/icon';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { click } from '../../../testing/index';
import { BciLayoutIntl } from '../layout-intl';

import { LogoutComponent } from './logout.component';

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

  beforeEach(
    waitForAsync(() => {
      TestBed.configureTestingModule({
        imports: [ MatIconModule, BrowserAnimationsModule, NoopAnimationsModule ],
        declarations: [LogoutComponent],
        providers: [BciLayoutIntl]
      }).compileComponents();
    })
  );

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

    const allButtons = fixture.nativeElement.querySelectorAll('button');
    logoutButton = allButtons[0];
  });

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

  it('should display the text provided in config', () => {
    component.config = {
      title: 'test',
      // eslint-disable-next-line no-empty-function
      onLogout: () => {},
    };

    const titleElement: HTMLElement = fixture.nativeElement.querySelector('.bci-sidebar-nav-item-overlay-header span');

    fixture.detectChanges();

    expect(titleElement.textContent).toBe('test');
  });

  it('should emit the closing event and call the configured onLogout function when logout button is clicked', () => {
    component.config = {
      title: 'test',
      onLogout: jest.fn(),
    };

    let closeCalled = false;

    component.onClose.subscribe(() => {
      closeCalled = true;
    });

    click(logoutButton);

    expect(closeCalled).toBeTruthy();
    expect(component.config.onLogout).toHaveBeenCalled();
  });
});
