import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { AbholfachService, AutocompleteTokenDTO, QueryTokenDTO } from '@swagger/oms';
import { DateAdapter } from '@ui/common';
import { cold, hot } from 'jasmine-marbles';
import { DomainGoodsService } from './goods.service';

describe('DomainGoodsInServie', () => {
  let spectator: SpectatorService<DomainGoodsService>;
  const createService = createServiceFactory({
    service: DomainGoodsService,
    providers: [DateAdapter],
    mocks: [AbholfachService],
  });

  let abholfachServiceMock: jasmine.SpyObj<AbholfachService>;
  let dateAdapter: DateAdapter;

  beforeEach(() => {
    spectator = createService();
    abholfachServiceMock = spectator.inject(AbholfachService);
    dateAdapter = spectator.inject(DateAdapter);
  });

  it('should create', () => {
    expect(spectator.service).toBeDefined();
  });

  describe('searchWareneingang', () => {
    it('should call AbholfachService.AbholfachWareneingang', () => {
      const queryTokenDto: QueryTokenDTO = {};
      abholfachServiceMock.AbholfachWareneingang.and.returnValue(hot('-a|', { a: {} }));
      expect(spectator.service.searchWareneingang(queryTokenDto)).toBeObservable(cold('-a|', { a: {} }));
      expect(abholfachServiceMock.AbholfachWareneingang).toHaveBeenCalledWith(queryTokenDto);
    });
  });

  describe('searchWarenausgabe', () => {
    it('should call AbholfachService.AbholfachWarenausgabe', () => {
      const queryTokenDto: QueryTokenDTO = {};
      abholfachServiceMock.AbholfachWarenausgabe.and.returnValue(hot('-a|', { a: {} }));
      expect(spectator.service.searchWarenausgabe(queryTokenDto)).toBeObservable(cold('-a|', { a: {} }));
      expect(abholfachServiceMock.AbholfachWarenausgabe).toHaveBeenCalledWith(queryTokenDto);
    });
  });

  describe('wareneingangComplete', () => {
    it('should call AbholfachService.AbholfachWareneingangAutocomplete', () => {
      const autocompleteToken: AutocompleteTokenDTO = {};
      abholfachServiceMock.AbholfachWareneingangAutocomplete.and.returnValue(hot('-a|', { a: {} }));
      expect(spectator.service.wareneingangComplete(autocompleteToken)).toBeObservable(cold('-a|', { a: {} }));
      expect(abholfachServiceMock.AbholfachWareneingangAutocomplete).toHaveBeenCalledWith(autocompleteToken);
    });
  });

  describe('warenausgabeComplete', () => {
    it('should call AbholfachService.AbholfachWarenausgabeAutocomplete', () => {
      const autocompleteToken: AutocompleteTokenDTO = {};
      abholfachServiceMock.AbholfachWarenausgabeAutocomplete.and.returnValue(hot('-a|', { a: {} }));
      expect(spectator.service.warenausgabeComplete(autocompleteToken)).toBeObservable(cold('-a|', { a: {} }));
      expect(abholfachServiceMock.AbholfachWarenausgabeAutocomplete).toHaveBeenCalledWith(autocompleteToken);
    });
  });

  describe('list', () => {
    it('should call search with queryToken', () => {
      const queryToken: QueryTokenDTO = {
        filter: {
          orderitemprocessingstatus: '16;8192;1024;512;2048',
          estimatedshippingdate: '"2021-06-01T22:00:00.000Z"-"2021-06-07T22:00:00.000Z"',
        },
        orderBy: [{ by: 'estimatedshippingdate' }],
        skip: 0,
        take: 20,
      };

      spyOn(dateAdapter, 'today').and.returnValue(new Date(2021, 5, 7));
      spyOn(spectator.service, 'search').and.returnValue(hot('-a|', { a: {} }));
      expect(spectator.service.list()).toBeObservable(cold('-a|', { a: {} }));
      expect(spectator.service.searchWareneingang).toHaveBeenCalledWith(queryToken);
    });
  });

  describe('querySettings', () => {
    it('should call AbholfachService.AbholfachWareneingangQuerySettings', () => {
      abholfachServiceMock.AbholfachWareneingangQuerySettings.and.returnValue(hot('-a|', { a: {} }));
      expect(spectator.service.goodsInQuerySettings()).toBeObservable(cold('-a|', { a: {} }));
      expect(abholfachServiceMock.AbholfachWareneingangQuerySettings).toHaveBeenCalled();
    });
  });
});
