mswjs / data

Data modeling and relation library for testing JavaScript applications.

Home Page:https://npm.im/@mswjs/data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Relational property's getter is lost when updating the parent entity

kettanaito opened this issue · comments

Steps to reproduce

const db = factory({
  segment: {
    id: primaryKey(String),
    title: String,
    revision: oneOf('revision'),
  },
  revision: {
    id: primaryKey(String),
    title: String,
  },
})

db.segment.create({
  id: 'segment-1',
  revision: db.revision.create({
    id: 'revision-1',
    title: 'Initial',
  }),
})

// Update the parent entity "segment".
const segment = db.segment.update({
  where: { id: { equals: 'segment-1' } },
  data: { title: 'Updated segment' },
})!

expect(Object.getOwnPropertyDescriptor(segment, 'revision')).toHaveProperty(
  'get',
)

segment.revision is no longer a getter to resolve the referenced entity. Instead, it's the actual value of the referenced entity at the moment of the "segment" update.

Root cause

The issue is caused by spreading the updated entityChunk when performing update:

{ ...entityChunk },

When the object is spread its getters are resolved. Instead, we should clone the entityChunk while preserving its getters (any property descriptors, really).