I have a Vue app I am trying to unit test, using jasmine
/karma
. Below is the code in a component:
After using v-for=(data,index)
from database, I'm setting data.date
in the template:
<p class="date">
{{ dateFilter(data.date) }}
</p>
Now I can test whether a given data is in the proper format. I currently have something like this in the spec file:
import { mount } from '@vue/test-utils';
import moment from 'moment'
it('should call method', () => {
const selector = wrapper.find('date');
});
How can I call the method and pass a mock parameter to test the method? Can we also import the moment js?
To test whether the date was rendered in the correct format:
const wrapper = mount(MyComponent, {
propsData: {
// assuming component has `items` prop, used in:
// <p class="date" v-for="data in items"> {{ dateFilter(data.date) }} </p>
items: [
{
id: 100,
date: new Date('2020-12-10T12:30:45-08:00') // 12:30pm GMT-8 === 4:30pm UTC
}
]
}
})
const dateEl = wrapper.find('.date') // find first dateElement with `date` class
expect(dateEl.text()).toEqual('16:30 pm')
To test the component method directly, access the method through the wrapper's vm
property:
expect(wrapper.vm.dataFilter(new Date('2020-12-10T12:30:45-08:00'))).toEqual('16:30 pm')