import { CrmCustomerService } from '@domain/crm';
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator';
import { UiInput } from '@ui/filter';
import { of } from 'rxjs';
import { CustomerSearchMainAutocompleteProvider } from './customer-search-main-autocomplete.provider';

describe('CustomerSearchMainAutocompleteProvider', () => {
  let spectator: SpectatorService<CustomerSearchMainAutocompleteProvider>;
  let customerService: SpyObject<CrmCustomerService>;

  const createService = createServiceFactory({
    service: CustomerSearchMainAutocompleteProvider,
    mocks: [CrmCustomerService],
  });

  beforeEach(() => {
    spectator = createService();
    customerService = spectator.inject(CrmCustomerService);
  });

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

  describe('complete()', () => {
    it('should call _customerService.complete() with input value when input length is > 2', async () => {
      customerService.complete.and.returnValue(of({ result: {} }));

      let input = new UiInput();
      spyOnProperty(input, 'value', 'get').and.returnValue('test');
      const result = await spectator.service.complete(input).toPromise();
      expect(customerService.complete).toHaveBeenCalledWith('test', undefined);
      expect(result).toBeTruthy();
    });

    it('should not call _customerService.complete() when input length is < 2', () => {
      customerService.complete.and.returnValue(of({}));

      let input = new UiInput();
      spyOnProperty(input, 'value', 'get').and.returnValue('te');
      spectator.service.complete(input);
      expect(customerService.complete).not.toHaveBeenCalled();
    });
  });
});
