Mobx-state-tree: resolveIdentifier regression issue after upgrading from 3.0.2 to 3.2.2

Created on 19 Sep 2018  路  6Comments  路  Source: mobxjs/mobx-state-tree

I have:

  • [ ] A conceptual question.

    • [ ] I've checked documentation and searched for existing issues

    • [ ] I tried the spectrum channel first

  • [x] I think something is not working as it should.

    • [x] I've checked documentation and searched for existing issues

    • [ ] I've made sure your project is based on the latest MST version

    • [x] Fork this code sandbox or another minimal reproduction.

    • [x] Describe expected behavior

      MST3.0.2 resolveIdentifier working:

      https://codesandbox.io/s/24zrq4n9n0 (models.CommentStore.js, line 15...)

 changeSelectedItem(uid: string) {
      // Working in MST3.0.2
      const item = resolveIdentifier(CommentModel, self.items, uid);
      console.log("item", item && item.toJSON());
      const item2 = self.items.find(i => i.uid === uid);
      console.log("item2", item2 && item2.toJSON());

      self.selectedItem = item;
    }
  • [x] Describe observed behavior

Regression issue: After upgrading to MST3.2.2 resolveIdentifier returns undefined

https://codesandbox.io/s/294ylryp0

  • [ ] Feature request

    • [ ] Describe the feature and why you feel your feature needs to be generically solved inside MST

    • [ ] Are you willing to (attempt) a PR?

_Not following the above template might result in your issue being closed without further notice_

bug has PR

Most helpful comment

The PR I just opened should fix both issues

All 6 comments

I took a look into it, it seems your id generator does not pass the test for the TOptionalId refinement

nevermind, it seems to be about the identifier being optional with a function, looking further into it

unit test
```ts
test("#1019", () => {
function randomUuid() {
return "1bbc17fd-2068-4cc9-aff0-82d37c9cdf20"
}

const CommentModel = types.model("CommentModel", {
    uid: types.optional(types.identifier, randomUuid)
})

const CommentStore = types
    .model("CommentStore", {
        items: types.array(CommentModel)
    })
    .actions(self => {
        function test() {
            const comment = CommentModel.create({})
            expect(comment.uid).toBe(randomUuid())

            self.items.push(comment)
            const item = resolveIdentifier(CommentModel, self.items, comment.uid)
            const item2 = self.items.find(i => i.uid === comment.uid)
            expect(item).toBe(item2)
        }

        return {
            test
        }
    })

CommentStore.create({}).test()

})
````

I had a similar (I think) issue today, a minimal repro is

  const Item = types
    .model({
      id: types.optional(types.identifier, 'dummykey'),
    })
  console.log(getIdentifier(Item.create())) // undefined, should be 'dummykey'

The PR I just opened should fix both issues

should be fixed in the recently released 3.4.0

Was this page helpful?
0 / 5 - 0 ratings