我测试时组件不会渲染

时间:2015-09-21 11:40:05

标签: javascript coffeescript reactjs jasmine react-router

我有一个非常奇怪的问题。我使用jasmine 2.0react 0.13.3react-router 0.13.3和其他包。我有一个测试套件,它不能findRenderedDOMComponentWithTag()。请考虑确切的例子:

React = require('react')
TestUtils = require('react/addons.js').addons.TestUtils
ReactRouterContext = require('../../helpers/react_router_context_helper.coffee')

NewSession = require('../../../../app/coffee/components/sessions/new.coffee')
subject = ReactRouterContext(NewSession)

describe 'NewSession', ->
  it 'should send the query after clicking on the button', ->
    instance = TestUtils.renderIntoDocument(<subject />)
    h3 = TestUtils.findRenderedDOMComponentWithTag(instance, 'h3') # can't find at least one h3 tag

  it 'should render wrong login/password on error', ->
    pending()

  it 'should process user after successful login', ->
    # localStorage
    # alt.js
    # redirect
    pending()

这看起来非常有趣,因为组件渲染功能中有警报方法,但没有任何警报

React = require('react')
ReactBootstrap = require('react-bootstrap')

Row = ReactBootstrap.Row
Col = ReactBootstrap.Col
Input = ReactBootstrap.Input
Button = ReactBootstrap.Button

WebUtils = require('../../web_utils.coffee')
SessionActions = require('../../actions/session_actions.coffee')

ErrorMessage = require('../error_message.coffee')

module.exports = React.createClass
  contextTypes: router: React.PropTypes.func
  getInitialState: ->
    { error_text: '' }
  handleSubmit: (e) ->
    e.preventDefault()
    WebUtils.query
      path: 'sessions'
      data: @getFormData()
      type: 'POST'
      callback: ((result) ->
        if result.status == 'success'
          SessionActions.create(result)
          @context.router.transitionTo('/')
        else
          @setState error_text: 'Неправильные логин и/или пароль'
      ).bind(this)
  getFormData: ->
    {
      login: @refs.login.getValue(),
      password: @refs.password.getValue()
    }
  render: ->
    alert 'hh'
    <form onSubmit={@handleSubmit}>
      <Row>
        <Col md={6} mdOffset={3} className='text-center'>
          <h3>Войти в систему</h3>

          { <ErrorMessage text={@state.error_text} /> if !!@state.error_text }

          <Input type='text' placeholder='Введите Ваш логин' ref='login' autoFocus />
          <Input type='password' placeholder='Пароль' ref='password' />
          <Button type='submit' bsStyle='success'>Вход</Button>
        </Col>
      </Row>
    </form>

-

# react_router_context_helper.coffee
React = require('react')
objectAssign = require('object-assign')

module.exports = (Component, props, stubs) ->
  RouterStub = ->
  objectAssign RouterStub, {
    makePath: ->
    makeHref: ->
    transitionTo: ->
    replaceWith: ->
    goBack: ->
    getCurrentPath: ->
    getCurrentRoutes: ->
    getCurrentPathname: ->
    getCurrentParams: ->
    getCurrentQuery: ->
    isActive: ->
    getRouteAtDepth: ->
    setRouteComponentAtDepth: ->
  }, stubs

  React.createClass
    childContextTypes:
      router: React.PropTypes.func
      routeDepth: React.PropTypes.number
    getChildContext: ->
      {
        router: RouterStub
        routeDepth: 0
      }
    render: ->
      React.createElement Component, props

1 个答案:

答案 0 :(得分:0)

这似乎很愚蠢,但我只需要将$sce.trustAsResourceUrl("ms-appdata:///local/image.jpg").$$unwrapTrustedValue()替换为subject。所以,React现在将我的变量视为组件类,而不是div标签。

Subject