Created
April 13, 2016 23:42
-
-
Save t3h2mas/64f1143693f21de418e7fc532398ce38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { | |
renderIntoDocument, | |
scryRenderedDOMComponentsWithTag, | |
Simulate | |
} from 'react-addons-test-utils'; | |
import Voting from '../../src/components/Voting'; | |
import {expect} from 'chai'; | |
describe('voting', () => { | |
it('renders a pair of buttons', () => { | |
const component = renderIntoDocument( | |
<Voting pair={["Trainspotting", "28 Days Later"]} /> | |
); | |
const buttons = scryRenderedDOMComponentsWithTag(component, 'button'); | |
expect(buttons.length).to.equal(2); | |
expect(buttons[0].textContent).to.equal('Trainspotting'); | |
expect(buttons[1].textContent).to.equal('28 Days Later'); | |
}); | |
it('invokes callback when a button is clicked', () => { | |
let votedWith; | |
const vote = (entry) => votedWith = entry; | |
const component = renderIntoDocument( | |
<Voting pair={["Trainspotting", "28 Days Later"]} | |
vote={vote} /> | |
); | |
const buttons = scryRenderedDOMComponentsWithTag(component, 'button'); | |
Simulate.click(buttons[0]); | |
expect(votedWith).to.equal('Trainspotting'); | |
}); | |
it('disables buttons when user has voted', () => { | |
const component = renderIntoDocument( | |
<Voting pair={["Trainspotting", "28 Days Later"]} | |
hasVoted="Trainspotting" /> | |
); | |
const buttons = scryRenderedDOMComponentsWithTag(component, 'button'); | |
expect(buttons.length).to.equal(2); | |
expect(buttons[0].hasAttribute('disabled')).to.equal(true); | |
expect(buttons[1].hasAttribute('disabled')).to.equal(true); | |
}); | |
it('adds label to the voted entry', () => { | |
const component = renderIntoDocument( | |
<Voting pair={["Trainspotting", "28 Days Later"]} | |
hasVoted="Trainspotting" /> | |
); | |
const buttons = scryRenderedDOMComponentsWithTag(component, 'button'); | |
expect(buttons[0].textContent).to.contain('Voted'); | |
}); | |
it('renders just the winner when there is one', () => { | |
const component = renderIntoDocument( | |
<Voting winner="Trainspotting" /> | |
); | |
const buttons = scryRenderedDOMComponentsWithTag(component, 'button'); | |
expect(buttons.length).to.equal(0); | |
const winner = ReactDOM.findDOMNode(component.refs.winner); | |
expect(winner).to.be.ok; | |
expect(winner.textContent).to.contain('Trainspotting'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment