Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created December 20, 2017 04:31
Show Gist options
  • Save lmiller1990/08838521a7e0bcacd347c2fc3e36ce98 to your computer and use it in GitHub Desktop.
Save lmiller1990/08838521a7e0bcacd347c2fc3e36ce98 to your computer and use it in GitHub Desktop.
<template>
<!-— markup and directives, v-if, v-for, etc -->
</template>
<script>
 /* props, data, lifecycle hooks, methods, computed properties */
</script>
<template>
  <div v-for=”member in members”
  <input type=”checkbox” :checked=”member.checked” />
  </div>
</template>
created: {
  this.$store.dispatch(‘fetchPosts’)
  this.setOldest()
}
import Member from ‘./Member’
it(‘sets the oldest member’, () => {
const members = [{ 
  name: ‘a’, age: 20, checked: false, 
}, {
  name: ‘b’, age: 30, checked: false
  }]
let $this = { members }
  member.methods.setOldest.call($this)
  expect(members[‘b’].checked).toBe(true)
})
<template>
  <div id=”message” v-if=”submitted”></div>
</template>
// test
const wrapper = shallow(Component)
wrapper.setData({ submitted: false })
expect(wrapper.findAll(‘#message’).length).toBe(0)
wrapper.setData({ submitted: true })
expect(wrapper.findAll(‘#message’).length).toBe(1)
<template>
  <Member v-for=”member in members” />
</template>
<script>
export default {
  computed: {
  members () {
return this.$store.state.members.all
}
}
</script>
const Member = {
  name: ‘Member’,
  render: h => h(‘div’)
}
const wrapper = shallow(Component, {
  stubs: { Member }
})
wrapper.setComputed({ members: [1, 2, 3] })
expect(wrapper.findAll(Member).length).toBe(3)
<script>
  export defaut {
  props: {
  memberName: {
  type: String,
  required: true
  },
isAdmin: {
type: Boolean,
default: true
  }
  }
  }
</script>
<template>
  <button @click=”fetchPosts” />
</template>
<script>
  export default {
  methods: {
  fetchPosts () {
  this.$store.dispatch(‘fetchPosts’)
}
  } 
  }
</script>
import Vuex from ‘vuex’
import { shallow, createLocalVue } from ‘vue-test-utils’
const localVue = createLocalVue()
localVue.use(Vuex)
const actions = {
 fetchPosts: jest.fn()
}
const store = new Vuex.Store({ actions })
const wrapper = shallow(Component, { store })
wrapper.find(‘button’).trigger(‘click’)
expect(actions.mock.calls.length).toBe(1)
<template>
  <! — don’t need the button anymore →
</template>
<script>
export default {
  created: {
  this.$store.dispatch(‘fetchPosts’)
  },
methods: {
// ...
}
  }
</script>
data () {
  return {
  members: [{ 
  name: ‘a’, age: 20, checked: false, 
}, {
name: ‘b’, age: 30, checked: false
}]
  }
},
setOldest () {
let oldest = { age: 0 }
  for (let m in this.members) {
  if (this.members[m].age > oldest.age)
  oldest = this.members[m]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment