Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

reducer returning wrong value when current state is falsey #9

Closed
cinnabarcaracal opened this issue Feb 7, 2019 · 2 comments
Closed

Comments

@cinnabarcaracal
Copy link
Contributor

cinnabarcaracal commented Feb 7, 2019

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
Copy link
Owner

knpwrs commented Feb 10, 2019

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
Copy link
Owner

knpwrs commented Feb 10, 2019

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

@knpwrs knpwrs closed this as completed Feb 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants