Skip to content

Test computed property getter/setter #1981

Open
@CapInSpase

Description

@CapInSpase

My component get props and have computed property by props. And I have method with fetch where i call setter with $emit computed. All works but in test I have error

Child component

export default {
  props:{
    listProductDictionaries:{
      type: Array,
      required: true
    },
  },
  computed:{
    listProductDictionariesComputed:{
      get() { return this.listProductDictionaries },
      set(val) { this.$emit('update:listProductDictionaries', val) }
    },
  },
  methods:{
    onSelectTypeTarget(value){
      fetch(`/api/target_audiences/${value.id}`)
      .then((response) => {
        return response.json().then((data) => {
          this.listProductDictionariesComputed = data
        })
        .catch(err => console.log(err))
      });
    },
  },
}

Parent component

<MaterialInfo
             :list-product-dictionaries.sync="listProductDictionaries"
         />

Test spec

import {createLocalVue, shallowMount} from "@vue/test-utils"
import MaterialInfo from '../../assets/components/MaterialInfo'
import fetchMock from 'jest-fetch-mock'
fetchMock.enableMocks()

const localVue = createLocalVue()

describe('testing component', () => {
    let wrapper;
    beforeEach(() => {

        wrapper = shallowMount(MaterialInfo,
            {
                localVue,
                propsData: {
                    listProductDictionaries: [],
                },
            })
    })
    test('test fetch get "selectTypeTarget"', done => {
        fetchMock.mockResponseOnce(JSON.stringify({ data: ['value'] }), { status: 200, headers: { 'content-type': 'application/json' } });
        let value = {
            id: '91f60c16-83db-4fc5-8da5-967dc89cb47e'
        }

        wrapper.vm.onSelectTypeTarget(value.id)
        wrapper.vm.$nextTick(() => {
            expect(wrapper.vm.listProductDictionariesComputed).toBe(['value'])
            done();
        });
    })
})

ERROR IN TEST
Expected: "['value']"
Received: []"

Maybe I'm doing something wrong, tell me how to do it right

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions