|
| 1 | +import React from 'react'; |
| 2 | +import jsdom from 'jsdom'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import TestUtils from 'react-addons-test-utils'; |
| 5 | + |
| 6 | +import App from '../app/components/App'; |
| 7 | +import AddTodo from '../app/components/AddTodo'; |
| 8 | +require('react/node_modules/fbjs/lib/ExecutionEnvironment').canUseDOM = true; |
| 9 | +function setupDom() { |
| 10 | + // if (typeof document !== 'undefined') { |
| 11 | + // return; |
| 12 | + // } |
| 13 | + |
| 14 | + global.document = jsdom.jsdom('<html><body></body></html>'); |
| 15 | + global.window = document.defaultView; |
| 16 | + global.navigator = global.window.navigator; |
| 17 | +}; |
| 18 | +// setupDom(); |
| 19 | + |
| 20 | +describe('模拟DOM测试', function (done) { |
| 21 | + it('添加新的Todo', function () { |
| 22 | + const app = TestUtils.renderIntoDocument( |
| 23 | + <App/> |
| 24 | + ); |
| 25 | + let todoItems = TestUtils.scryRenderedDOMComponentsWithClass(app, 'todo-text'); |
| 26 | + let todoItemsLength = todoItems.length; |
| 27 | + let addTodo = TestUtils.findRenderedDOMComponentWithClass(app, 'add-todo'); |
| 28 | + // let addTodo = TestUtils.findRenderedComponentWithType(app, AddTodo); |
| 29 | + let addButton = addTodo.querySelector('button'); |
| 30 | + // let addButton = TestUtils.scryRenderedDOMComponentsWithTag(app, 'button')[3]; |
| 31 | + // let addInput = addTodo.querySelector('input'); |
| 32 | + let addInput = TestUtils.findRenderedDOMComponentWithTag(app, 'input'); |
| 33 | + addInput.value = '4th Todo'; |
| 34 | + // TestUtils.Simulate.change(addInput); |
| 35 | + // TestUtils.Simulate.keyDown(addInput, {key: "Enter", keyCode: 13, which: 13}); |
| 36 | + TestUtils.Simulate.click(addButton); |
| 37 | + let todoItemsAfterClick = TestUtils.scryRenderedDOMComponentsWithClass(app, 'todo-text'); |
| 38 | + expect(todoItemsAfterClick.length).to.be.equal(todoItemsLength + 1); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | + |
0 commit comments