2017-11-05 26 views
0

를 제출Vue.js 단위 테스트는 테스트하는 방법이 그 트리거가 적절하지 않은 것 같습니다 .. 이벤트에게 나는 양식을 제출 테스트하기 위해 노력하고

내 VUE 구성 요소를 테스트 할
1) calls store action login when the form is submitted 
LoginPage.vue 
TypeError: wrapper.find(...).trigger is not a function 
at Context.<anonymous> (webpack:///test/unit/specs/pages/LoginPage.spec.js:35:28 <- index.js:50826:29) 

LoginPage.vue

<template> 
    <div class="container"> 
     <div class="login-page"> 
     <h1 class="title">Login to existing account</h1> 
     <form @submit.prevent="submit()" class="form form--login grid"> 
      <div class="row"> 
      <label for="login__email">Email</label> 
      <input type="text" id="login__email" class="input--block" v-model="email" v-on:keyup="clearErrorMessage" /> 
      </div> 
      <div class="row"> 
      <label for="login__password">Password</label> 
      <input type="password" id="login__password" class="input--block" v-model="password" v-on:keyup="clearErrorMessage" /> 
      </div><!-- /.row --> 
      <div class="row"> 
      <label></label> 
      <button id="submit" type="submit">Login</button> 
      </div><!-- /.row --> 
      <div v-show='hasError' class="row"> 
      <label></label> 
      <p class="error">Invalid credentials</p> 
      </div> 
     </form> 
     </div><!-- /.login-page --> 
    </div> 
    </template> 

    <script> 
    import store from '@/vuex/store' 
    import { mapActions } from 'vuex' 
    import _ from 'underscore' 

    export default { 
     name: 'loginPage', 
     data() { 
     return { 
      email: '[email protected]', 
      password: 'john123', 
      hasError: false 
     } 
     }, 
     methods: _.extend({}, mapActions(['login']), { 
     clearErrorMessage() { 
      this.hasError = false 
     }, 
     submit() { 
      return this.login({user: { email: this.email, password: this.password }}) 
      .then((logged) => { 
      if (logged) { 
       this.$router.push('shoppinglists') 
      } else { 
       this.hasError = true 
      } 
      }) 
     } 
     }), 
     store 
    } 
    </script> 

LoginPage.spec.js

,
 import LoginPage from '@/pages/LoginPage' 
     import Vue from 'vue' 
     import Vuex from 'vuex' 
     import sinon from 'sinon' 
     import { mount } from 'avoriaz' 

     Vue.use(Vuex) 

     describe('LoginPage.vue',() => { 
      let actions 
      let getters 
      let store 

      beforeEach(() => { 
      actions = { 
       login: sinon.stub() 
      } 
      getters = { 
       isAuthenticated:() => { 
       state => state.isAuthenticated 
       } 
      } 
      store = new Vuex.Store({ 
       actions, 
       getters, 
       state: { 
       isAuthenticated: false, 
       currentUserId: '' 
       } 
      }) 
      }) 

      it('calls store action login when the form is submitted', (done) => { 
      const wrapper = mount(LoginPage, { store }) 
      wrapper.find('#submit').trigger('submit') 
      wrapper.vm.$nextTick(() => { 
       expect(actions.login.calledOnce).to.equal(true) 
       done() 
      }) 
      }) 
     }) 

답변

1

양식 태그에서 '클릭'해야합니다!

it('calls store action login when the form is submitted', (done) => { 
    const wrapper = mount(LoginPage, { store }) 
    const form = wrapper.find('form')[0] 
    form.trigger('submit') 
    wrapper.vm.$nextTick(() => { 
     expect(actions.login.calledOnce).to.equal(true) 
     done() 
    }) 
    })