mercredi 25 janvier 2017

How to test computed properties in Vue.js? Can't mock "data"

I wonder how to test computed properties in Vue.js's unit tests.

I have create a new project via vue-cli (webpack based).

For example here are my Component:

<script>
  export default {
    data () {
      return {
        source: []
      }
    },
    methods: {
      removeDuplicates (arr) {
        return [...new Set(arr)]
      }
    },
    computed: {
      types () {
        return this.removeDuplicates(this.source))
      }
    }
  }
</script>

I've tried to test it like this

it('should remove duplicates from array', () => { const arr = [1,2,1,2,3] const result = FiltersList.computed.removeDuplicates(arr) const expectedLength = 3

  expect(result).to.have.length(expectedLength)
})


QUESTION (two problems):

  1. this.source is undefined. How to mock or set value to it? (FiltersList.data is a function);
  2. Perhaps I don't wan't to call removeDuplicates method, but how to mock(stub) this call?

Aucun commentaire:

Enregistrer un commentaire