Code Monkey home page Code Monkey logo

immer-issue-768's Introduction

Reproduction of Immer Issue #768

This repository reproduces the issue outlined in immerjs/immer#768.

In short, I believe applying a replace patch on certain types of state causes undesirable behavior when the result of that patch is passed to produce.

The error is that any subsequent updates using produce to the result of applyPatch will mutate the produce function's base parameter, which is undesirable for an immutability library.

Project setup

npm install
npm start

Test cases

This repository tests 9 scenarios that test different structures of state. You can find the tests in src/reproduce.test.ts. I outline the basic setup of each test and their results below.

TEST 1 ✅

state.stocks is a map and stocks are plain objects, with patch replacing state.stocks

type State = { 
  stocks: Map<string, { ticker: string; name: string; priceHistory: number[] }>;
};

const errorProducingPatch = [
  {
    op: "replace",
    path: ["stocks"],
    value: makeInitialState().stocks, // Map<string, {ticker: string; name: string; priceHistory: number[]>
  },
] as Patch[];

️This setup does not reproduce the issue described above.

TEST 2 ⚠️

state.stocks is a map of [immerable] classes, with patch replacing state.stocks

type State = {
  stocks: Map<string, Stock>;
};

const errorProducingPatch = [
  {
    op: "replace",
    path: ["stocks"],
    value: makeInitialState().stocks, // Map<string, Stock>
  },
] as Patch[];

️This setup reproduces the issue described above.

TEST 3 ✅

state.stocks is a map of [immerable] classes, with patch replacing state root

type State = {
  stocks: Map<string, Stock>;
};

const errorProducingPatch = [
  {
    op: "replace",
    path: [],
    value: makeInitialState(), // State
  },
] as Patch[];

️This setup does not reproduce the issue described above.

TEST 4 ⚠️

state.stocks is an object keying [immerable] classes, with patch replacing state.stocks

type State = {
  stocks: { [key: string]: Stock };
};

const errorProducingPatch = [
  {
    op: "replace",
    path: ["stocks"],
    value: makeInitialState().stocks, // { [key: string]: Stock }
  },
] as Patch[];

️This setup reproduces the issue described above.

TEST 5 ⚠️

state.stocks is an array of [immerable] classes, with patch replacing state.stocks

type State = {
  stocks: Stock[];
};

const errorProducingPatch = [
  {
    op: "replace",
    path: ["stocks"],
    value: makeInitialState().stocks, // Stock[]
  },
] as Patch[];

️This setup reproduces the issue described above.

TEST 6 ⚠️

state.stock is a single [immerable] class, with patch replacing state.stock

type State = {
  stock: Stock;
};

const errorProducingPatch = [
  {
    op: "replace",
    path: ["stock"],
    value: makeInitialState().stock, // Stock
  },
] as Patch[];

️This setup reproduces the issue described above.

TEST 7 ⚠️

state is an array of [immerable] classes, with patch replacing state[0]

type State = Stock[];

const errorProducingPatch = [
  {
    op: "replace",
    path: [0],
    value: makeInitialState()[0], // Stock
  },
] as Patch[];

️This setup reproduces the issue described above.

TEST 8 ⚠️

state is a map of [immerable] classes, with patch replacing state["INTC"]

type State = Map<string, Stock>;

const errorProducingPatch = [
  {
    op: "replace",
    path: ["INTC"],
    value: makeInitialState().get("INTC"), // Stock
  },
] as Patch[];

️This setup reproduces the issue described above.

TEST 9 ✅

state is an [immerable] class, with patch replacing state root

type State = Stock;

const errorProducingPatch = [
  {
    op: "replace",
    path: [],
    value: makeInitialState(), // Stock
  },
] as Patch[];

This setup does not reproduce the issue described above.

immer-issue-768's People

Contributors

colesam avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.