2017-12-27 625 views
0

我一直有下面的錯誤,而我嘗試運行下面的測試用例,它應該驗證URL。如果需要,函數validateUrl應該爲URL添加前綴「http://」。我這樣做是因爲我用的是API沒有這個前綴:Jest TypeError:無法讀取未定義的'商店'

import userDetail from '../src/container/user-detail' 

describe('UrlValidation',() => { 
    it('should be a valid string',() => { 
    var result = new userDetail() 
    result.validateUrl("google.com") 
    exept (result).toBe("http://google.com") 
    }) 
}) 

userDetail看起來是這樣的:

import React, { Component } from 'react'; 
import { connect } from "react-redux"; 

class UserDetail extends Component { 
    renderUser(userData){ 
    return (
     <tr key={userData.id}> 
     <td> 
      {userData.name} 
     </td> 
     <td> 
      <a target="_blank" href={this.validateUrl(userData.website)}>{userData.website}</a> 
     </td> 
     <td> 
      {userData.address.city} 
     </td> 
     <td> 
      <a> 
       <img alt="edit" src="./media/edit-icon.svg" className="edit-img"></img> 
      </a> 
     </td> 

     </tr> 
    ); 
    } 
    validateUrl(providedUrl){ 
     var url = providedUrl; 
     var r = new RegExp('/^(http|https):\/\/[^ "]+$/'); 
     if (!r.test(url)) { 
      return "http://" + providedUrl 
     } else { 
      return providedUrl 
     } 
    } 
    render() { 
     const {users} = this.props 
     console.log(users.map(user => this.renderUser(user))); 
     return (
      <table className="table table-hover"> 
       <thead> 
        <tr> 
         <th>Name</th> 
         <th>Website</th> 
         <th>City</th> 
         <th>Edit</th> 
        </tr> 
       </thead> 
       <tbody> 
        {users.map(user => this.renderUser(user))} 
       </tbody> 
      </table> 
    )} 
    } 

    function mapStateToProps({ users }){ 
    return { users }; // { user } == { user: user } 
    } 

    export default connect(mapStateToProps)(UserDetail); 

我不知道,什麼是我的定義錯誤。

我首先想到函數validateUrl不可訪問,但它應該是。

輸出看起來是這樣的:

[email protected] ~/dev/User-Manager/UserManagement $ npm test

[email protected] test /home/joshii/dev/User-Manager/UserManagement jest

FAIL tests/app.test.jsx UrlValidation ✕ should be a valid string (3ms)

● UrlValidation › should be a valid string

TypeError: Cannot read property 'store' of undefined 

    3 | describe('UrlValidation',() => { 
    4 |  it('should be a valid string',() => { 
    >5 |  var result = new userDetail() 
    6 |  result.validateUrl("google.com") 
    7 |  exept (result).toBe("http://google.com") 
    8 |  }) 
    9 | }) 

    at new Connect (node_modules/react-redux/lib/components/connect.js:102:29) 
    at Object.<anonymous> (__tests__/app.test.jsx:5:18) 

Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 0.744s, estimated 1s Ran all test suites. npm ERR! Test failed. See above for more details.

+0

你有沒有試過嘲笑傳遞REDX'商店'開玩笑? –

+0

@JohnKennedy不,但我只是使用這個類,並給它一個字符串。它不使用任何來自redux商店的東西,還是我錯了? –

+0

你的根組件被封裝在'provider'組件中,它把'store'作爲一個道具。 –

回答

0

validateUrl功能看起來更像是庫函數,而不是一個類的成員(你不使用this任何地方它),所以我建議你將它移到一個單獨的lib文件(也可以在代碼中的其他地方重複使用)。 )然後,如果你只是想測試你的validateUrl函數而沒有Redux部分,請嘗試單獨導出類(例如,將導出添加到類定義中,例如export class UserDetail extends Component {....}),然後在你的測試用例中:

import {UserDetail} from ''../src/container/user-detail' 

describe('UrlValidation',() => { 
    it('should be a valid string',() => { 
    var result = new UserDetail() 
    result.validateUrl("google.com") 
    expect(result).toBe("http://google.com") 
    }) 
}) 
+0

謝謝,我會試試這個,然後我會更新我的狀態。 :) –

+0

這是不可能的只是導出類,因爲''是'user.map',如果我不使用連接方法將不可用。 –

+0

可以導出類。你提到的包含'user.map'的''是在'render()'函數中,它不被測試調用。 如果你正在渲染或測試一個依賴於道具的函數,你只需要關心道具(你也可以通過'UserDetail({users:mockedUsersArray})實例化來模擬道具') –

相關問題