Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSnapshot returning inconsistent results #1396

Open
3 tasks done
MiroslavGannoha opened this issue Sep 27, 2019 · 0 comments
Open
3 tasks done

getSnapshot returning inconsistent results #1396

MiroslavGannoha opened this issue Sep 27, 2019 · 0 comments
Labels
bug Confirmed bug

Comments

@MiroslavGannoha
Copy link

Bug report

  • I've checked documentation and searched for existing issues
  • I've made sure my project is based on the latest MST version
  • Fork this code sandbox or another minimal reproduction.

Sandbox link or minimal reproduction code
https://codesandbox.io/embed/snowy-dawn-c7elr

import { types, getSnapshot, applyPatch, applySnapshot } from "mobx-state-tree";

const Product = types.model("Product", {
  price: 0,
  id: types.identifier
});

const Cart = types.model("Cart", {
  products: types.array(Product)
});

const UserStore = types.model("Store", {
  user: "Some User",
  cart: types.optional(Cart, () =>
    Cart.create({
      products: [Product.create({ price: 1, id: "111" })]
    })
  )
});

const userStore = UserStore.create();
console.log(
  "before Patch - snapshot: ",
  getSnapshot(userStore).cart.products[0]
);
// before Patch - snapshot:  Object {price: 1, id: "111"}

applyPatch(userStore, {
  op: "replace",
  path: "/cart/products/0/price",
  value: 2
});
console.log(
  "after Patch - userStore snapshot (WRONG): ",
  getSnapshot(userStore).cart.products[0]
);
// after Patch - userStore snapshot (WRONG):  Object {price: 1, id: "111"}

console.log(
  "after Patch - products snapshot (OK): ",
  getSnapshot(userStore.cart.products)[0]
);
// after Patch - products snapshot (OK):  Object {price: 2, id: "111"}

applySnapshot(userStore.cart.products, [{ price: 3, id: "111" }]);
console.log(
  "after applySnapshot - userStore snapshot (NOW OK): ",
  getSnapshot(userStore).cart.products[0]
);
// after applySnapshot - userStore snapshot (NOW OK):  Object {price: 3, id: "111"}

Describe the expected behavior
getSnapshot should return updated price both times

Describe the observed behavior
getSnapshot returning default or updated value depending on node level

P.S. I should've not probably do
products: [Product.create({ price: 1, id: "111" })]
and instead do
products: [{ price: 1, id: "111" }]
or
products: [getSnapshot(Product.create({ price: 1, id: "111" }))]
But still it should produce some warning or error. Not very obvious thing.

@mweststrate mweststrate added the bug Confirmed bug label Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants