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

Fixing prototype methods being discarded when using setData #2166

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,22 @@ export const mergeDeep = (
if (!isObject(target) || !isObject(source)) {
return source
}
Object.keys(source).forEach((key) => {
const targetValue = target[key]
const sourceValue = source[key]

if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = sourceValue
} else if (sourceValue instanceof Date) {
target[key] = sourceValue
} else if (isObject(targetValue) && isObject(sourceValue)) {
target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue)
} else {
target[key] = sourceValue
}
})
Object.keys(source)
.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(source) ?? {}))
.forEach((key) => {
const targetValue = target[key]
const sourceValue = source[key]

if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = sourceValue
} else if (sourceValue instanceof Date) {
target[key] = sourceValue
} else if (isObject(targetValue) && isObject(sourceValue)) {
target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue)
} else {
target[key] = sourceValue
}
})

return target
}
Expand Down
31 changes: 31 additions & 0 deletions tests/setData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,35 @@ describe('setData', () => {
expect(wrapper.vm.value).toBeInstanceOf(Date)
expect(wrapper.vm.value!.toISOString()).toBe('2022-08-11T12:15:54.000Z')
})

it('should retain prototype methods for constructed objects when calling setData', async () => {
const expectedResult = 'success!'
class TestClass {
getResult() {
return expectedResult
}
}

const wrapper = mount(
defineComponent({
template: '<div/>',
data() {
return { value: new TestClass() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the initial value should be something else to see if setData properly updates it no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in a different class altogether?
Or just with some properties on the class that get changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example:

it('should retain prototype methods for constructed objects when calling setData', async () => {
  const expectedResult = 'success!'
  class TestClass {
    name: string
    constructor(name: string) {
      this.name = name
    }
    getResult(): string {
      return expectedResult
    }
  }

  const wrapper = mount(
    defineComponent({
      template: '<div/>',
      data() {
        return { value: new TestClass('test1') }
      },
      methods: {
        getResult() {
          return `${this.value.name}: ${this.value.getResult()}`
        }
      }
    })
  )

  expect(wrapper.vm.getResult()).toStrictEqual(`test1: ${expectedResult}`)

  await wrapper.setData({
    value: new TestClass('test2')
  })

  expect(wrapper.vm.getResult()).toStrictEqual(`test2: ${expectedResult}`)
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks good 👍
(nit: as it is TS, you can use constructor(private name: string))

},
methods: {
getResult() {
return this.value.getResult()
}
}
})
)

expect(wrapper.vm.getResult()).toStrictEqual(expectedResult)

await wrapper.setData({
value: new TestClass()
})

expect(wrapper.vm.getResult()).toStrictEqual(expectedResult)
})
})