|
| 1 | +import*asReactfrom'react' |
| 2 | +import*asTestUtilsfrom'react-addons-test-utils' |
| 3 | + |
| 4 | +import{createStore}from'redux' |
| 5 | +import{Provider}from'react-redux' |
| 6 | + |
| 7 | +import{Counter}from'../counter' |
| 8 | +import{reducers}from'../../reducers' |
| 9 | + |
| 10 | +describe('components/Counter',()=>{ |
| 11 | + |
| 12 | +functionrenderElement(el:React.ReactElement<{}>){ |
| 13 | +returnTestUtils.renderIntoDocument(el)asReact.Component<{},{}> |
| 14 | +} |
| 15 | + |
| 16 | +functionfindComponentByType(root:React.Component<{},{}>,type:any):React.Component<{},{}>{ |
| 17 | +returnTestUtils.findRenderedComponentWithType(root,type) |
| 18 | +} |
| 19 | + |
| 20 | +functionsetup():React.Component<{},{}>{ |
| 21 | +conststore=createStore(reducers) |
| 22 | +constwrapper=renderElement( |
| 23 | +<Providerstore={store}> |
| 24 | +<Counterlabel='a counter!'/> |
| 25 | +</Provider>) |
| 26 | +constcounter=findComponentByType(wrapper,Counter) |
| 27 | +returncounter |
| 28 | +} |
| 29 | + |
| 30 | +it('starts at 0',()=>{ |
| 31 | +constcounter=setup() |
| 32 | +constpre=TestUtils.findRenderedDOMComponentWithTag(counter,'pre') |
| 33 | +expect(pre.textContent).toEqual('counter = 0') |
| 34 | +}) |
| 35 | + |
| 36 | +it('shows a label',()=>{ |
| 37 | +constcounter=setup() |
| 38 | +constlabel=TestUtils.findRenderedDOMComponentWithTag(counter,'label') |
| 39 | +expect(label.textContent).toEqual('a counter!') |
| 40 | +}) |
| 41 | + |
| 42 | + |
| 43 | +describe('clicking "increment"',()=>{ |
| 44 | +letcounter:React.Component<{},{}> |
| 45 | + |
| 46 | +beforeEach(()=>{ |
| 47 | +counter=setup() |
| 48 | +constbuttonEl=TestUtils.findRenderedDOMComponentWithTag(counter,'button') |
| 49 | +TestUtils.Simulate.click(buttonEl) |
| 50 | +TestUtils.Simulate.click(buttonEl) |
| 51 | +TestUtils.Simulate.click(buttonEl) |
| 52 | +}) |
| 53 | + |
| 54 | +it('increments counter',()=>{ |
| 55 | +constpre=TestUtils.findRenderedDOMComponentWithTag(counter,'pre') |
| 56 | +expect(pre.textContent).toEqual('counter = 3') |
| 57 | +}) |
| 58 | +}) |
| 59 | +}) |