|
| 1 | +import Vuex from 'vuex' |
| 2 | +import VueRouter from 'vue-router' |
| 3 | +import { mount, createLocalVue } from '@vue/test-utils' |
| 4 | + |
| 5 | +import Posts from '@/components/Posts.vue' |
| 6 | +import { createRouter } from '@/createRouter' |
| 7 | +import { createStore } from '@/createStore' |
| 8 | + |
| 9 | +const createWrapper = (component, options = {}, storeState = {}) => { |
| 10 | + const localVue = createLocalVue() |
| 11 | + localVue.use(VueRouter) |
| 12 | + localVue.use(Vuex) |
| 13 | + const store = createStore(storeState) |
| 14 | + const router = createRouter() |
| 15 | + |
| 16 | + return mount(component, { |
| 17 | + store, |
| 18 | + router, |
| 19 | + localVue, |
| 20 | + ...options |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +describe('Posts.vue', () => { |
| 25 | + it('renders a message if passed', () => { |
| 26 | + const message = 'New content coming soon!' |
| 27 | + const wrapper = createWrapper(Posts, { |
| 28 | + propsData: { message }, |
| 29 | + }) |
| 30 | + |
| 31 | + expect(wrapper.find("#message").text()).toBe('New content coming soon!') |
| 32 | + }) |
| 33 | + |
| 34 | + it('renders posts', async () => { |
| 35 | + const wrapper = createWrapper(Posts, {}, { |
| 36 | + posts: [{ id: 1, title: 'Post' }] |
| 37 | + }) |
| 38 | + |
| 39 | + expect(wrapper.findAll('.post').length).toBe(1) |
| 40 | + }) |
| 41 | + |
| 42 | + it('renders new post link if authenticated', async () => { |
| 43 | + const wrapper = createWrapper(Posts, {}, { |
| 44 | + authenticated: true |
| 45 | + }) |
| 46 | + |
| 47 | + expect(wrapper.find('.new-post').exists()).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + it('does not render new post link if not authenticated', async () => { |
| 51 | + const wrapper = createWrapper(Posts, {}, { |
| 52 | + authenticated: false |
| 53 | + }) |
| 54 | + |
| 55 | + expect(wrapper.find('.new-post').exists()).toBe(false) |
| 56 | + }) |
| 57 | +}) |
0 commit comments