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 }
@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)
})
// 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',
<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: [],
describe('Objects component', () => {
describe('Methods', () => {
it('getObjects - should return true and set the data.currentObjects if getObjects retrieves an array of objects', done => {
const mockedObjects = [
{ id: 1 },
{ id: 2 }
]
const wrapper = mount(ObjectsComponent)
const getObjectsStub = sinon.stub(wrapper.vm, 'getObjects').returns(mockedObjects)
<template>
<div></div>
</template>
<script type="text/javascript">
import { GET_OBJECTS } from 'services/constants/action-types'
export default {
...
data () {
import { GET_OBJECTS } from 'services/constants/action-types'
describe('Objects component', () => {
let store
let state
let actions
beforeEach(() => {
actions = {}
actions[GET_OBJECTS] = sinon.stub()