Created
August 17, 2017 04:35
-
-
Save pbalduino/add938d956bec8ad0d184353be7190da to your computer and use it in GitHub Desktop.
How to test a filter in VUE.js 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* component */ | |
<script> | |
export default { | |
name: 'sample', | |
filters: { | |
round: function (value) { | |
if (!value) return NaN; | |
return Math.round(value); | |
} | |
} | |
} | |
</script> | |
/* test */ | |
import Vue from 'vue' | |
import Sample from '@/components/Sample' | |
describe('Sample component', () => { | |
describe('#round filter', () => { | |
const Constructor = Vue.extend(Sample); | |
const Component = new Constructor().$mount(); | |
/* | |
here you access any of your component filter. | |
filters are stateless and immutable, so don't | |
worry about a single instantiation | |
*/ | |
const round = Component.$options.filters.round; | |
// then test just like a plain simple function | |
it("should round a number", () => { | |
expect(round(3.14)).to.equal(3); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment