Created
December 20, 2017 04:31
-
-
Save lmiller1990/08838521a7e0bcacd347c2fc3e36ce98 to your computer and use it in GitHub Desktop.
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
<template> | |
<!-— markup and directives, v-if, v-for, etc --> | |
</template> | |
<script> | |
/* props, data, lifecycle hooks, methods, computed properties */ | |
</script> |
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
<template> | |
<div v-for=”member in members” | |
<input type=”checkbox” :checked=”member.checked” /> | |
</div> | |
</template> |
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
created: { | |
this.$store.dispatch(‘fetchPosts’) | |
this.setOldest() | |
} |
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
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) | |
}) |
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
<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) |
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
<template> | |
<Member v-for=”member in members” /> | |
</template> | |
<script> | |
export default { | |
computed: { | |
members () { | |
return this.$store.state.members.all | |
} | |
} | |
} | |
</script> |
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
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) |
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
<script> | |
export defaut { | |
props: { | |
memberName: { | |
type: String, | |
required: true | |
}, | |
isAdmin: { | |
type: Boolean, | |
default: true | |
} | |
} | |
} | |
</script> |
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
<template> | |
<button @click=”fetchPosts” /> | |
</template> | |
<script> | |
export default { | |
methods: { | |
fetchPosts () { | |
this.$store.dispatch(‘fetchPosts’) | |
} | |
} | |
} | |
</script> |
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
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) |
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
<template> | |
<! — don’t need the button anymore → | |
</template> | |
<script> | |
export default { | |
created: { | |
this.$store.dispatch(‘fetchPosts’) | |
}, | |
methods: { | |
// ... | |
} | |
} | |
</script> |
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
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