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

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Component } from '@angular/core';
import { BciDirtyFlagDirective } from "./bci-dirty-flag.directive";

@Component({
  template: '<input id="targetedInput" bciDirtyFlag (isDirty)="mockMethod($event)"/>',
})
class TestComponent {
  inputDirty = false;
  mockMethod(event): void {
    this.inputDirty = event;
  }
}

describe('BciDirtyFlagDirective', () => {
  let fixture: ComponentFixture<TestComponent>;
  let directive: BciDirtyFlagDirective;
  let component: TestComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [TestComponent],
      imports: [BciDirtyFlagDirective]
    });
    fixture = TestBed.createComponent(TestComponent);
    directive = fixture.debugElement.nativeElement.querySelector('[bciDirtyFlag]');
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create an instance', (): void => {
    expect(directive).toBeTruthy();
  });

  it('should be false if user just clicks inside', (): void => {
    const input = fixture.debugElement.nativeElement.querySelector('#targetedInput');
    input.click()

    expect(component.inputDirty).toBeFalsy();
  });

  it('should emit true when input is dirty', (): void => {
    const input = fixture.debugElement.nativeElement.querySelector('#targetedInput');
    input.value = 'something';
    input.dispatchEvent(new Event('input'));

    expect(component.inputDirty).toBeTruthy();
  });
});
