import { Breadcrumb } from '../defs';
import * as action from './breadcrumb.actions';
import { breadcrumbReducer } from './breadcrumb.reducer';
import { INIT } from './breadcrumb.state';

describe('Breadcrumb Reducer', () => {
  it('should return the initial state if the current state is empty', () => {
    const fixture = breadcrumbReducer(undefined, { type: '' });

    expect(fixture).toEqual(INIT);
  });

  describe('addBreadcrumb', () => {
    it('should add the breadcrumb to the state', () => {
      const breadcrumb: Breadcrumb = {
        id: 1,
        key: 'unit-test',
        name: 'Test Name',
        path: 'Test Patch',
      };

      const fixture = breadcrumbReducer(INIT, action.addBreadcrumb({ breadcrumb }));

      expect(fixture.entities[1]).toEqual(breadcrumb);
    });
  });

  describe('updateBreadcrumb', () => {
    it('should update an existing breadcrumb', () => {
      const breadcrumb: Breadcrumb = {
        id: 1,
        key: 'unit-test',
        name: 'Test Name',
        path: 'Test Patch',
      };

      const expected = {
        ...breadcrumb,
        name: 'Test Name 2',
      };

      const state = breadcrumbReducer(INIT, action.addBreadcrumb({ breadcrumb }));

      const fixture = breadcrumbReducer(state, action.updateBreadcrumb({ id: breadcrumb.id, changes: { name: 'Test Name 2' } }));

      expect(fixture.entities[breadcrumb.id]).toEqual(expected);
    });
  });

  describe('removeBreadcrumb', () => {
    it('should remove the breadcrumb', () => {
      const breadcrumb: Breadcrumb = {
        id: 1,
        key: 'unit-test',
        name: 'Test Name',
        path: 'Test Patch',
      };

      const state = breadcrumbReducer(INIT, action.addBreadcrumb({ breadcrumb }));

      const fixture = breadcrumbReducer(state, action.removeBreadcrumb({ id: breadcrumb.id }));

      expect(fixture.entities[breadcrumb.id]).toBeUndefined();
    });
  });

  describe('removeManyBreadcrumb', () => {
    it('should remove all breadcrumbs for the given ids', () => {
      const breadcrumb1: Breadcrumb = {
        id: 1,
        key: 'unit-test',
        name: 'Test Name',
        path: 'Test Patch',
      };
      const breadcrumb2 = { ...breadcrumb1, id: 2 };
      const breadcrumb3 = { ...breadcrumb1, id: 3 };

      let state = breadcrumbReducer(INIT, action.addBreadcrumb({ breadcrumb: breadcrumb1 }));
      state = breadcrumbReducer(state, action.addBreadcrumb({ breadcrumb: breadcrumb2 }));
      state = breadcrumbReducer(state, action.addBreadcrumb({ breadcrumb: breadcrumb3 }));

      const fixture = breadcrumbReducer(state, action.removeManyBreadcrumb({ ids: [1, 3] }));

      expect(Object.keys(fixture.entities).length).toEqual(1);
      expect(fixture.entities[breadcrumb2.id]).toEqual(breadcrumb2);
    });
  });
});
