import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RemissionListFilterComponent } from './remission-list-filter.component';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { IsaOverlayRef } from 'apps/sales/src/app/core/overlay';
import { RemissionFilterService } from '../../services/remission-filter.service';
import { RemissionService } from '@isa/remission';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NgxsModule } from '@ngxs/store';
import { BehaviorSubject } from 'rxjs';
import { HasSelectedFiltersPipe } from '../../pipes';

fdescribe('RemissionListFilterComponent', () => {
  let fixture: ComponentFixture<RemissionListFilterComponent>;
  let component: RemissionListFilterComponent;
  let filterService: RemissionFilterService;

  beforeEach(async () => {
    TestBed.configureTestingModule({
      imports: [NoopAnimationsModule, NgxsModule.forRoot([])],
      providers: [
        {
          provide: IsaOverlayRef,
          useValue: jasmine.createSpyObj('isaOverlayRef', ['close']),
        },
        {
          provide: RemissionFilterService,
          useValue: jasmine.createSpy('remissionFilterService'),
        },
        {
          provide: RemissionService,
          useValue: jasmine.createSpy('remissionService'),
        },
      ],
      declarations: [RemissionListFilterComponent, HasSelectedFiltersPipe],
      schemas: [NO_ERRORS_SCHEMA],
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(RemissionListFilterComponent);
    component = fixture.componentInstance;
    filterService = TestBed.inject(RemissionFilterService);

    spyOn(component, 'ngOnInit').and.callFake(() => {});

    fixture.detectChanges();
  });

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

  describe('close', () => {
    beforeEach(() => {
      spyOn(component, 'close').and.callThrough();
      filterService.resetFilters$ = new BehaviorSubject(null);
      fixture.detectChanges();
    });
    it('should set inTransition to true', () => {
      component.close();
      fixture.detectChanges();

      const result = component.inTransition;
      expect(result).toBeTruthy();
    });
  });

  describe('closeAnimationDone', () => {
    beforeEach(() => {
      spyOn(component, 'closeAnimationDone').and.callThrough();
      fixture.detectChanges();
    });
    it('should set inTransition to false if animation is complete (in)', () => {
      component.animate = 'in';
      component.inTransition = true;
      fixture.detectChanges();

      component.closeAnimationDone();
      fixture.detectChanges();

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

    it('should not set in transition if animation out is already complete', () => {
      component.animate = 'out';
      component.inTransition = true;
      fixture.detectChanges();

      component.closeAnimationDone();
      fixture.detectChanges();

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