import { createComponentFactory, Spectator } from '@ngneat/spectator';
import { UiFilterFilterGroupMainModule } from './filter-group/filter-filter-group-main';
import { UiFilterInputGroupMainModule } from './filter-group/filter-input-group-main';
import { UiInputGroupSelectorPipe } from './pipe';
import { querySettingsData } from './testing';
import { UiFilter } from './tree';
import { UiFilterComponent } from './ui-filter.component';

describe('UiFilterComponent', () => {
  let spectator: Spectator<UiFilterComponent>;
  const createComponent = createComponentFactory({
    component: UiFilterComponent,
    declarations: [UiInputGroupSelectorPipe],
    imports: [UiFilterFilterGroupMainModule, UiFilterInputGroupMainModule],
  });

  beforeEach(() => {
    spectator = createComponent();
  });

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

  describe('ngOnChanges()', () => {
    it('should check and create an instance of UiQuerySettingsDTO if querySettings is a plain object', () => {
      spectator.setInput({
        filter: querySettingsData,
      });

      expect(spectator.component.filter instanceof UiFilter).toBe(true);
    });

    it('should not create a new instance of UiQuerySettingsDTO if querySettings is already an instance of UiQuerySettingsDTO', () => {
      const querySettings = UiFilter.create(querySettingsData);
      spectator.setInput({
        filter: querySettings,
      });

      expect(spectator.component.filter).toBe(querySettings);
    });

    it('should skip querySettings check if there is no input change', () => {
      spectator.component.ngOnChanges({});
      expect(spectator.component.filter).toBeUndefined();
    });
  });
});
