import { RemissionFilterService } from './remission-filter.service';
import { TestBed } from '@angular/core/testing';
import { RemissionResourceType } from '../models';
import { RemissionService } from '@isa/remission';
import { NgxsModule } from '@ngxs/store';
import { hot, cold } from 'jasmine-marbles';
import { Filter } from '../../filter';

fdescribe('#RemissionFilterService', () => {
  let remissionFilterService: RemissionFilterService;

  const mockSetFilterGroup: Filter = {
    type: 'select',
    name: 'Mock',
    options: [],
  };

  beforeEach(async () => {
    TestBed.configureTestingModule({
      imports: [NgxsModule.forRoot([])],
      providers: [
        RemissionFilterService,
        {
          provide: RemissionService,
          useValue: jasmine.createSpy('remissionService'),
        },
      ],
    });

    remissionFilterService = TestBed.inject(RemissionFilterService);
  });

  beforeEach(() => {
    spyOn(remissionFilterService, 'getFilters').and.callFake(() => {});
    spyOn(remissionFilterService, 'getFlattenedFilters').and.callFake(() => {});
  });

  it('should be created', () => {
    expect(remissionFilterService instanceof RemissionFilterService).toBeTruthy();
  });

  describe('get lastFilterGroupChanged$', () => {
    beforeEach(() => {
      const remissionSource$ = hot('-a', { a: RemissionResourceType.Central });
      spyOnProperty(remissionFilterService, 'remissionSource$').and.returnValue(remissionSource$);

      const remissionTarget$ = hot('-b', { b: 'libri' });
      spyOnProperty(remissionFilterService, 'remissionTarget$').and.returnValue(remissionTarget$);
    });
    it('should return the last filter group changed for the currently selected remisison filter and target', () => {
      spyOnProperty(remissionFilterService, 'lastFilterGroupChanged$').and.callThrough();

      remissionFilterService.lastFilterGroupChanged = mockSetFilterGroup;

      const expected$ = cold('-c', { c: mockSetFilterGroup });

      expect(remissionFilterService.lastFilterGroupChanged$).toBeObservable(expected$);
    });
  });

  describe('set lastFilterGroupChanged', () => {
    beforeEach(() => {
      const remissionSource$ = hot('-a--b', {
        a: RemissionResourceType.Central,
        b: RemissionResourceType.Central,
      });
      spyOnProperty(remissionFilterService, 'remissionSource$').and.returnValue(remissionSource$);

      const remissionTarget$ = hot('-b---', { b: 'libri' });
      spyOnProperty(remissionFilterService, 'remissionTarget$').and.returnValue(remissionTarget$);
    });
    it('should set the last filter group changed for the currently selected remisison filter and target', () => {
      spyOnProperty(remissionFilterService, 'lastFilterGroupChanged$').and.callThrough();

      remissionFilterService.lastFilterGroupChanged = mockSetFilterGroup;

      const expected$ = cold('-a--b', {
        a: mockSetFilterGroup,
        b: mockSetFilterGroup,
      });
      expect(remissionFilterService.lastFilterGroupChanged$).toBeObservable(expected$);
    });
  });
});
