Skip to content

Instantly share code, notes, and snippets.

View bmfteixeira's full-sized avatar

Bruno Teixeira bmfteixeira

View GitHub Profile
@bmfteixeira
bmfteixeira / simple_action.spec.js
Last active September 16, 2017 13:57
Simple Vue action test
// the action
[actionTypes.ACTION_SET_FAV_ELEM] (context, elemId) {
context.commit(mutationTypes.MUTATION_SET_FAV_ELEM, elemId)
}
// the test
it('should invoke mutation to set the favourite element', done => {
const elemId = 'AAAAAA'
testAction(actions[actionTypes.ACTION_SET_FAV_ELEM], elemId, {}, [
{ type: mutationTypes.MUTATION_SET_FAV_ELEM, payload: elemId }
// the mutation
[mutationTypes.MUTATION_SET_FAV_ELEM] (state, elemId) {
state.favElem = elemId
}
// the test
it('should set state.favElem', () => {
const state = {
favElem: ''
}
// the getter
getElemsByCategory: state => (category) => {
return state.elems.filter((el, index, arr) => el.category === category)
}
// the test
it('should return the elements that have Cat1 as category', done => {
const expected = [
{
'id': '1',
@bmfteixeira
bmfteixeira / api_action.spec.js
Last active September 18, 2017 10:05
Vue action test with API call
// the action
[actionTypes.ACTION_SET_FAV_ELEM] (context) {
return APIAdapter.services.fetchFavElem()
.then((response) => {
context.commit(mutationTypes.MUTATION_SET_FAV_ELEM, response)
resolve(true)
}).catch((error) => {
context.commit(mutationTypes.MUTATION_SET_FAV_ELEM, undefined)
reject(error)
})
<template>
<div></div>
</template>
<script>
export default {
// == Lifecycle
mounted () {
window.addEventListener('scroll', this.handleScroll)
},
describe('Basic component', () => {
describe('Lifecycle', () => {
it('Mounted', done => {
const handleScrollStub = sinon.stub(BasicComponent.methods, 'handleScroll')
const addEventStub = sinon.stub(window, 'addEventListener')
const wrapper = mount(BasicComponent)
expect(addEventStub).to.be.calledWith('scroll', wrapper.vm.handleScroll)
<template>
<div></div>
</template>
<script type="text/javascript">
export default {
...
data () {
return {
currentObjects: [],
<template>
<div></div>
</template>
<script type="text/javascript">
import { GET_OBJECTS } from 'services/constants/action-types'
export default {
...
data () {
<template>
<div></div>
</template>
<script type="text/javascript">
export default {
props: {
propId: {
type: Number,
required: true
describe('Watcher component', () => {
let props
beforeEach(() => {
props = {
propId: 1
}
})
describe('Watcher', () => {