|
| 1 | +import * as React from 'react' |
| 2 | +import * as TestUtils from '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 | + function renderElement (el: React.ReactElement<{}>) { |
| 13 | + return TestUtils.renderIntoDocument(el) as React.Component<{}, {}> |
| 14 | + } |
| 15 | + |
| 16 | + function findComponentByType(root: React.Component<{}, {}>, type: any): React.Component<{}, {}> { |
| 17 | + return TestUtils.findRenderedComponentWithType(root, type) |
| 18 | + } |
| 19 | + |
| 20 | + function setup (): React.Component<{}, {}> { |
| 21 | + const store = createStore(reducers) |
| 22 | + const wrapper = renderElement( |
| 23 | + <Provider store={store}> |
| 24 | + <Counter label='a counter!' /> |
| 25 | + </Provider>) |
| 26 | + const counter = findComponentByType(wrapper, Counter) |
| 27 | + return counter |
| 28 | + } |
| 29 | + |
| 30 | + it('starts at 0', () => { |
| 31 | + const counter = setup() |
| 32 | + const pre = TestUtils.findRenderedDOMComponentWithTag(counter, 'pre') |
| 33 | + expect(pre.textContent).toEqual('counter = 0') |
| 34 | + }) |
| 35 | + |
| 36 | + it('shows a label', () => { |
| 37 | + const counter = setup() |
| 38 | + const label = TestUtils.findRenderedDOMComponentWithTag(counter, 'label') |
| 39 | + expect(label.textContent).toEqual('a counter!') |
| 40 | + }) |
| 41 | + |
| 42 | + |
| 43 | + describe('clicking "increment"', () => { |
| 44 | + let counter: React.Component<{}, {}> |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + counter = setup() |
| 48 | + const buttonEl = 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 | + const pre = TestUtils.findRenderedDOMComponentWithTag(counter, 'pre') |
| 56 | + expect(pre.textContent).toEqual('counter = 3') |
| 57 | + }) |
| 58 | + }) |
| 59 | +}) |
0 commit comments