{"size": 2835, "ext": "js", "max_stars_repo_path": "src/reducers/byId.spec.js", "max_stars_repo_name": "andresilveirah/yato", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/reducers/byId.spec.js", "max_issues_repo_name": "andresilveirah/yato", "max_issues_repo_issues_event_min_datetime": "2022-02-26T02:31:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:49:06.000Z", "max_forks_repo_path": "src/reducers/byId.spec.js", "max_forks_repo_name": "andresilveirah/yato", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 31.8539325843, "max_line_length": 100, "alphanum_fraction": 0.5601410935}
import { normalize } from 'normalizr';

import types from '../actions/types';
import { todos, todosList } from '../actions/schema';
import byId, { getTodo } from './byId';

describe('createFilteredListReducerFor', () => {
  const reducer = byId;
  let type, response, state, action, todo, listOfTodos;

  describe('when the action type is not known', () => {
    beforeEach(() => { action = { type: 'SOMETHING_UNKNOWN' }; });

    it('simply returns its state', () => {
      expect(reducer('some state', action)).toEqual('some state');
    });
  });

  describe('when the action type is not known', () => {
    beforeEach(() => { action = { type: 'SOMETHING_UNKNOWN' }; });

    it('simply returns its state', () => {
      expect(reducer('some state', action)).toEqual('some state');
    });
  });

  describe('when the action type is ADD_TODO_SUCCESS', () => {
    beforeEach(() => {
      type = types.ADD_TODO_SUCCESS;
      todo = { id: 1, text: 'hey' };
      response = normalize(todo, todos);
      action = { type, response };
    });

    it('adds the todo to its state', () => {
      expect(reducer({}, action)).toEqual({ 1: todo });
    });
  });

  describe('when the action type is TOGGLE_TODO_SUCCESS', () => {
    beforeEach(() => {
      type = types.TOGGLE_TODO_SUCCESS;
      todo = { id: 1, text: 'hey', completed: true };
      response = normalize(todo, todos);
      action = { type, response };
    });

    it('updates the toggled todo', () => {
      expect(reducer({ 1: { id: 1, text: 'hey', completed: false } }, action)).toEqual({ 1: todo });
    });
  });

  describe('when the action type is FETCH_TODOS_SUCCESS', () => {
    beforeEach(() => {
      type = types.FETCH_TODOS_SUCCESS;
      listOfTodos = [{ id: 1, text: 'hey' }, { id: 2, text: 'ho' }];
      response = normalize(listOfTodos, todosList);
      action = { type, response };
    });

    describe('and the state doesnt contain the todos in the response', () => {
      it('adds the todo to its state', () => {
        expect(reducer({}, action)).toEqual({ 1: listOfTodos[0], 2: listOfTodos[1] });
      });
    });

    describe('and the state already contain one of the todos in the response', () => {
      const updatedTodo = { id: 1, text: 'hoho' };

      beforeEach(() => {
        state = { 1: listOfTodos[0], 2: listOfTodos[1] };
        listOfTodos[0] = updatedTodo;
        response = normalize(listOfTodos, todosList);
        action = { type, response };
      });

      it('returns a new state object with the updated todo', () => {
        expect(reducer(state, action)).toEqual({ 1: updatedTodo, 2: listOfTodos[1] });
      });
    });

    describe('getTodo', () => {
      it('returns the value of a key in a list', () => {
        expect(getTodo({ 1: 'hey', 2: 'ho' }, 1)).toEqual('hey');
      });
    });
  });
});
