import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator';
import { CustomerService } from '@swagger/crm';
import { UiErrorModalComponent, UiModalService } from '@ui/modal';
import { cold, hot } from 'jasmine-marbles';
import { CustomerSearchStateSettingsLoader } from './customer.search-state-settings-loader';

describe('CustomerSearchStateSettingsLoader', () => {
  let spectator: SpectatorService<CustomerSearchStateSettingsLoader>;
  let customerServiceMock: SpyObject<CustomerService>;
  let modalServiceMock: SpyObject<UiModalService>;
  const createService = createServiceFactory({
    service: CustomerSearchStateSettingsLoader,
    mocks: [CustomerService, UiModalService],
  });

  beforeEach(() => {
    spectator = createService();
    customerServiceMock = spectator.inject(CustomerService);
    modalServiceMock = spectator.inject(UiModalService);
  });

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

  describe('load()', () => {
    it('should call _customerService.CustomerCustomerQuerySettings() and map the result to UISettingsDTO', () => {
      customerServiceMock.CustomerCustomerQuerySettings.and.returnValue(cold('-(a|)', { a: { result: { filter: [{ label: 'test' }] } } }));
      expect(spectator.service.load()).toBeObservable(hot('-(a|)', { a: { filter: [{ label: 'test' }] } }));
      expect(customerServiceMock.CustomerCustomerQuerySettings).toHaveBeenCalled();
    });

    it('should catch an Error and show UiErrorModalComponent', () => {
      customerServiceMock.CustomerCustomerQuerySettings.and.returnValue(cold('-#', undefined, { message: 'error' }));
      expect(spectator.service.load()).toBeObservable(hot('-(a|)', { a: {} }));
      expect(modalServiceMock.open).toHaveBeenCalledWith({
        content: UiErrorModalComponent,
        title: 'Fehler beim Laden des Kundenfilters',
        data: { message: 'error' },
      });
    });
  });
});
