0

I am trying to write my first Vue unit test, but I am unable to get the data property to change after function runs It runs fine on the UI front

I've multiple data objects

  data() {
    return {
      collections: [],
      inlineVisibility: false,
      inlineTitle: '',
      inlineSubtitle: '',
      inlineKind: '',
      loading: false,
    }
  },

The function is

async loadcsv(element=null, testCase = null) {
  const reader = new FileReader()
  let file = new Blob([workingCSV])
  this.toggleLoading() //This works, this turns the loading to true but nothing after this works in test
  reader.onload = async (e) => {
    try {
    collections.push(e)
    } catch (err) {
    this.inlineTitle = "Error"
    }
  }
  reader.readAsText(file)
},

This is the test I wrote till now

describe('ModalAddCollectionCSV', () => {
  it('Test load csv function', async () => {
    const localVue = createLocalVue()
    const wrapper = mount(ModalAddCollectionCSV, {
      localVue,
      propsData: {
        visible: true,
      },
    })
    const data = wrapper.vm.loadcsv() //data is always {} and does not even update the data property
    expect(wrapper.vm.$data.collections).toContain([Array])
  })
})

I've even tried shallow mount

I am using jest, and it seems there's an issue with the file reader

3
  • This is primarily specific not to Vue but to test framework, which wasn't specified, I suppose it's Jest, this needs be clarified. There's no FileReader in Node and you can't expect it to work like in a browser, so it should be mocked according to your expectations. Commented Aug 6, 2022 at 9:27
  • Possible duplicate of stackoverflow.com/questions/61572070/… Commented Aug 6, 2022 at 9:28
  • @EstusFlask will update that, you're right it's jest Commented Aug 7, 2022 at 0:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.