Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

React.jsを非ブラウザ環境で動かす

More than 5 years have passed since last update.

node.jsとjsdomを使う。ユニットテスト環境想定

# setup dom before react
jsdom = require('jsdom').jsdom
global.document  = jsdom('<html><body></body></html>')
global.window    = document.parentWindow
global.navigator = window.navigator

React = require 'react'
{TestUtils} = require 'react-addons'

Greeting = React.createClass
  getInitialState: -> title: 'foo'

  setTitle: (title) -> @setState title: title

  render: -> React.DOM.h1 {}, [
    React.DOM.span key: 1, @state.title
    React.DOM.button ref: 'btn', key: 2, onClick: (-> console.log 'clicked'), 'button'
  ]

comp = TestUtils.renderIntoDocument Greeting title: 'foo'
console.log comp.getDOMNode().innerHTML
comp.setState title: 'bar'
console.log comp.getDOMNode().innerHTML

しかしTestUtils.simulate.events(comp.refs.btn) が動かなかった

plaid
CXプラットフォーム「KARTE」の開発・運営、EC特化型メディア「Shopping Tribe」の企画・運営、CX特化型メディア「XD(クロスディー)」の企画・運営
https://plaid.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away