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

Broken setting of Date-Objects by setData #1717

Closed
cokuna-pavelkosolapov opened this issue Aug 11, 2022 · 2 comments · Fixed by #1720
Closed

Broken setting of Date-Objects by setData #1717

cokuna-pavelkosolapov opened this issue Aug 11, 2022 · 2 comments · Fixed by #1720
Labels
bug Something isn't working

Comments

@cokuna-pavelkosolapov
Copy link

Subject of the issue

If you try to set a Date to any data-variable it becomes an object.

Steps to reproduce

package.json

"devDependencies": {
    "@babel/core": "^7.18.10",
    "@babel/preset-env": "^7.18.10",
    "@vue/cli-plugin-babel": "^5.0.8",
    "@vue/cli-plugin-eslint": "~5.0.8",
    "@vue/cli-service": "~5.0.8",
    "@vue/test-utils": "^2.0.2",
    "@vue/vue3-jest": "^28.0.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^28.1.3",
    "jest-environment-jsdom": "^28.1.3",
    "jest-transform-stub": "^2.0.0"
  },

test.spec.js

import {shallowMount} from '@vue/test-utils'

describe('App', () => {
   
    const DateInput = {
        template: `<div><input type="text" :value="value" @input="emit" /></div>`,
        props: { modelValue: Date },
        computed: {
            value() {
                return '' + this.modelValue + ''
            }
        },
        methods: {
            emit(value) {
                this.$emit('update:modelValue', new Date(value))
            }
        }
    }
  
    const App = {
        template: ` <div><DateInput ref="input" v-model="modelValue" /></div> `, 
        components: { DateInput },
        props: {modelValue: Date},
        data() { return { defaultValue: this.modelValue}}
    }    

    it('set Date to data', async () => {
        const wrapper = shallowMount(App, {
            props: {
                modelValue: new Date('2022-08-10T12:15:54Z'),
            },
            global: {
                stubs: {
                    DateInput
                }
            }
        })
        expect(wrapper.vm.defaultValue + '').toBe((new Date('2022-08-10T12:15:54Z').toString()))

        await wrapper.setData({defaultValue: new Date('2022-09-10T12:15:54Z')})

        expect(wrapper.vm.defaultValue+ '').toBe((new Date('2022-09-10T12:15:54Z').toString()))
    })
})

Expected behaviour

Test "set Date to data" should be successful.

Actual behaviour

The test fails:

    Expected: "Sat Sep 10 2022 14:15:54 GMT+0200 (Central European Summer Time)"
    Received: "[object Object]"

Possible Solution

The Date should stay a Date!

@cokuna-pavelkosolapov cokuna-pavelkosolapov added the bug Something isn't working label Aug 11, 2022
@freakzlike
Copy link
Collaborator

This was caused by mergeDeep merging Date objects. Will be fixed by #1720.

One other thing:
IMO wrapper.vm.defaultValue + '' is not a nice solution and personally I would prefer to compare the ISO strings with toISOString(). But that's just my opinion 😉

@cokuna-pavelkosolapov
Copy link
Author

IMO wrapper.vm.defaultValue + '' is not a nice solution and personally I would prefer to compare the ISO strings with toISOString(). But that's just my opinion 😉

also an option. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants