knpwrs / redux-ts-utils

Everything you need to create type-safe applications with Redux!
MIT License
55 stars 4 forks source link

reducer returning wrong value when current state is falsey #9

Closed cinnabarcaracal closed 5 years ago

cinnabarcaracal commented 5 years ago

It's entirely possible that I'm just not understanding some part of the API, but I think there is an issue with the action handler working on a falsey current state (e.g. null or false). I think it might be the logic that returns the initial/default state when the current state is undefined being a little over eager.

e.g.

test('handles non-undefined falsey current state', () => {
  const ac1 = createAction('foo6');
  const state: string | null = null;
  const re = handleAction<typeof state>(ac1, _ => {
    return "bar"
  });
  const newState1 = re(state, ac1());
  expect(newState1).toEqual("bar");
  expect(newState1).not.toBe(null);
});
    Expected value to equal:
      "bar"
    Received:
      undefined

    Difference:

      Comparing two different types of values. Expected string but received undefined.

      73 |   });
      74 |   const newState1 = re(state, ac1());
    > 75 |   expect(newState1).toEqual("bar");
         |                     ^
      76 |   expect(newState1).not.toBe(null);
      77 | });
      78 |

If you think this could be a bug too, I would be happy to submit a pull request.

knpwrs commented 5 years ago

This has to do with being based around immer. Since the conversion to createDraft/finishDraft in v3.0.0 and the work done in #7 it should be easier to support primitive state.

knpwrs commented 5 years ago

Published 3.1.1 with support for primitive state. Thanks for the report!