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

setData doesn't work #538

Closed
human33 opened this issue Apr 18, 2021 · 6 comments · Fixed by #540
Closed

setData doesn't work #538

human33 opened this issue Apr 18, 2021 · 6 comments · Fixed by #540
Labels
bug Something isn't working

Comments

@human33
Copy link

human33 commented Apr 18, 2021

wrapper.setData doesn't set data in component. It sets data only in wrapper.vm object.

I created a simple components:

<template>
    nothing here
</template>

<script>
export default {
    data() {
        return {
            field: null
        };
    },
    computed: {
        isFieldNull() {
            return this.field == null;
        }
    }
}
</script>

And a test:

import { shallowMount } from '@vue/test-utils'
import testSetData from '../../src/cart/testSetData';
import { nextTick } from 'vue';


describe("test set data component", () => {
    it ("should set data", async () => {
        let wrapper = shallowMount(testSetData, {
            data () {
                return {
                    field: null
                }
            }
        });

        expect(wrapper.vm.isFieldNull).toBe(true);

        await wrapper.setData({ field: 10 });
        await nextTick();


        expect(wrapper.vm.field).toEqual(10);
        expect(wrapper.vm.isFieldNull).toBe(false);
    })
})

And the test fails on expect(wrapper.vm.isFieldNull).toBe(false); because field is still null.
Am I doing something wrong?

@human33
Copy link
Author

human33 commented Apr 18, 2021

I found out that if I don't pass data function to mount, it works fine.
So, this works:

import { shallowMount } from '@vue/test-utils'
import testSetData from '../../src/cart/testSetData';
import { nextTick } from 'vue';


describe("test set data component", () => {
    it ("should set data", async () => {
        let wrapper = shallowMount(testSetData);

        expect(wrapper.vm.isFieldNull).toBe(true);

        await wrapper.setData({ field: 10 });
        await nextTick();


        expect(wrapper.vm.field).toEqual(10);
        expect(wrapper.vm.isFieldNull).toBe(false);
    })
})

@lmiller1990 lmiller1990 added the bug Something isn't working label Apr 18, 2021
@lmiller1990
Copy link
Member

Seems like a bug - it should work as you are describing in the first test, too.

I will take a look (or you can, if you have time!)

@lmiller1990
Copy link
Member

I fixed it: #540

If you can give me a quick review I will do a release with this!

@human33
Copy link
Author

human33 commented Apr 19, 2021

I'm not familiar with this repository, so I'm not sure if I can review anything here.

@lmiller1990
Copy link
Member

All good, someone reviewed it.

Good way to learn it to review :)

@lmiller1990
Copy link
Member

Released in https://github.com/vuejs/vue-test-utils-next/releases/tag/v2.0.0-rc.5

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