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

  • 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. – Estus Flask Aug 06 '22 at 09:27
  • Possible duplicate of https://stackoverflow.com/questions/61572070/how-to-test-filereader-onload-using-simulate-change-in-jest – Estus Flask Aug 06 '22 at 09:28
  • @EstusFlask will update that, you're right it's jest – Jackie Chan Aug 07 '22 at 00:48

0 Answers0