import { INITIAL_HISTORY_STATE, INITIAL_ORDER_HISTORY, HistoryState } from './history.state';
import { historyReducer } from './history.reducer';
import * as actions from './history.actions';
import { HistoryDTO } from '@cmf/trade-api';
import { OrderHistoryStatus } from '@shelf-store/defs';

fdescribe('#HistoryStateReducer', () => {
  const id = 123;
  const mockHistories: HistoryDTO[] = [{ name: 'Fake History', id, values: [] }];

  it('should return the initial state if on Init Order action is dispatched', () => {
    const initialState = INITIAL_HISTORY_STATE;
    const action = actions.initOrderHistory({ id });

    const state = historyReducer(initialState, action);
    const entity = state.entities[id];

    expect(entity).toEqual({
      ...INITIAL_ORDER_HISTORY,
      id,
    });
  });

  it('should set status to fetching if fetchHistory action is dispatched (1)', () => {
    let state: HistoryState;
    const initialState = INITIAL_HISTORY_STATE;
    const initAction = actions.initOrderHistory({ id });

    state = historyReducer(initialState, initAction);

    const action = actions.fetchHistory({ id });
    state = historyReducer(state, action);

    expect(state.entities[id].status).toBe(OrderHistoryStatus.FETCHING);
  });

  it('should add history and set status to available (2)', () => {
    let state: HistoryState;
    const initialState = INITIAL_HISTORY_STATE;

    const initAction = actions.initOrderHistory({ id });
    state = historyReducer(initialState, initAction);

    const action = actions.addOrderHistory({ id, histories: mockHistories });
    state = historyReducer(state, action);

    const entity = state.entities[id];

    expect(entity.status).toBe(2);
    expect(entity.histories).toEqual(mockHistories);
    expect(entity.id).toEqual(id);
  });
});
