import { TestBed } from '@angular/core/testing';
import { Store } from '@ngrx/store';
import { isNumber } from '@utils/common';
import { of } from 'rxjs';
import { BreadcrumbService } from './breadcrumb.service';
import { Breadcrumb } from './defs';

import * as actions from './store/breadcrumb.actions';
import * as selectors from './store/breadcrumb.selectors';

describe('Breadcrumb Service', () => {
  let store: jasmine.SpyObj<Store<any>>;
  let service: BreadcrumbService;

  beforeEach(() => {
    store = jasmine.createSpyObj<Store<any>>('Store', ['select', 'dispatch']);

    TestBed.configureTestingModule({
      providers: [BreadcrumbService, { provide: Store, useValue: store }],
    });

    service = TestBed.inject(BreadcrumbService);
  });

  describe('getByKey$', () => {
    it('should call store.select with the selectBreadcrumbsByKey selector', () => {
      store.select.and.returnValue(of([]));
      service.getByKey$('unit-test');
      expect(store.select).toHaveBeenCalledWith(selectors.selectBreadcrumbsByKey, 'unit-test');
    });
  });

  describe('addBreadcrumb', () => {
    it('should call store.dispatch with the addBreadecrumb action and retuns a breadcrumb with an id', () => {
      let breadcrumb: Breadcrumb = { name: 'unit-test', key: 'hello-key', path: 'Run The Test' };
      breadcrumb = service.addBreadcrumb(breadcrumb);
      expect(store.dispatch).toHaveBeenCalledWith(actions.addBreadcrumb({ breadcrumb }));
      expect(isNumber(breadcrumb.id)).toBeTruthy();
      expect(isNumber(breadcrumb.timestamp)).toBeTruthy();
    });
  });
});
