Skip to content

Commit

Permalink
refactor asserter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkp committed Sep 17, 2015
1 parent 8231cf4 commit dbeba19
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 109 deletions.
192 changes: 96 additions & 96 deletions test/assertions/Asserter.js
@@ -1,105 +1,105 @@

import React from 'react/addons';
import chai from 'chai';
const { TestUtils } = React.addons;
const expect = chai.expect;


export default class Asserter {


constructor(jsx) {
this.autoSuggest = TestUtils.renderIntoDocument(jsx);
this.searchBox = TestUtils.findRenderedDOMComponentWithClass(this.autoSuggest, 'SearchBox');
this.dropDown = TestUtils.findRenderedDOMComponentWithClass(this.autoSuggest, 'DropDown');
}

assertValue(value) {
console.debug(`is the value ${value}?`);
expect(this.searchBox.getDOMNode().value).to.equal(value);
return this;
}

enterNewValue(value) {
console.debug(`enter ${value}`);
TestUtils.Simulate.change(this.searchBox.getDOMNode(), {target: {value: value}});
return this;
}

pressArrowLeft() {
console.debug(`press arrow left`);
TestUtils.Simulate.keyDown(this.searchBox, {keyCode: 37});
return this;
}

pressArrowUp() {
console.debug(`press arrow up`);
TestUtils.Simulate.keyDown(this.searchBox, {keyCode: 38});
return this;
}

pressArrowRight() {
console.debug(`press arrow right`);
TestUtils.Simulate.keyDown(this.searchBox, {keyCode: 39});
return this;
}

pressArrowDown() {
console.debug(`press arrow down`);
TestUtils.Simulate.keyDown(this.searchBox, {keyCode: 40});
return this;
}

pressEscape() {
console.debug(`press escape`);
TestUtils.Simulate.keyDown(this.searchBox, {keyCode: 27});
return this;
}

pressEnter() {
console.debug(`press enter`);
TestUtils.Simulate.keyDown(this.searchBox, {keyCode: 13});
return this;
}

assertDropDownDisplayed() {
console.debug(`drop down displayed?`);
expect(this.dropDown.getDOMNode().style.display).to.equal('block');
return this;
}

assertDropDownNotDisplayed() {
console.debug(`is drop down not displayed?`);
expect(this.dropDown.getDOMNode().style.display).to.equal('none');
return this;
}

assertNumberOfSuggestions(expectedNumberOfSuggestions) {
console.debug(`are there ${expectedNumberOfSuggestions} suggestions?`);
var children = this.dropDown.getDOMNode().children;
expect(children.length).to.equal(expectedNumberOfSuggestions);
return this;
}

assertSuggestions(expectedSuggestions) {
console.debug(`are the suggestions ${expectedSuggestions}?`);
let suggestions = TestUtils.scryRenderedDOMComponentsWithClass(this.dropDown, 'Suggestion');
var values = suggestions.map((suggestion) => {
return suggestion.getDOMNode().innerHTML;
});
expect(values).to.eql(expectedSuggestions);
return this;
}

assertSelectedSuggestion(expected) {
console.debug(`is the selected suggestion ${expected}?`);
let selected = TestUtils.scryRenderedDOMComponentsWithClass(this.dropDown, 'selected');
if (expected) {
expect(selected.length).to.equal(1);
expect(selected[0].getDOMNode().innerHTML).to.equal(expected);
} else {
expect(selected.length).to.equal(0);
export default (jsx) => {


const autoSuggest = TestUtils.renderIntoDocument(jsx);
const searchBox = TestUtils.findRenderedDOMComponentWithClass(autoSuggest, 'SearchBox');
const dropDown = TestUtils.findRenderedDOMComponentWithClass(autoSuggest, 'DropDown');

return {

assertValue(value) {
console.debug(`is the value ${value}?`);
expect(searchBox.getDOMNode().value).to.equal(value);
return this;
},

enterNewValue(value) {
console.debug(`enter ${value}`);
TestUtils.Simulate.change(searchBox.getDOMNode(), {target: {value: value}});
return this;
},

pressArrowLeft() {
console.debug(`press arrow left`);
TestUtils.Simulate.keyDown(searchBox, {keyCode: 37});
return this;
},

pressArrowUp() {
console.debug(`press arrow up`);
TestUtils.Simulate.keyDown(searchBox, {keyCode: 38});
return this;
},

pressArrowRight() {
console.debug(`press arrow right`);
TestUtils.Simulate.keyDown(searchBox, {keyCode: 39});
return this;
},

pressArrowDown() {
console.debug(`press arrow down`);
TestUtils.Simulate.keyDown(searchBox, {keyCode: 40});
return this;
},

pressEscape() {
console.debug(`press escape`);
TestUtils.Simulate.keyDown(searchBox, {keyCode: 27});
return this;
},

pressEnter() {
console.debug(`press enter`);
TestUtils.Simulate.keyDown(searchBox, {keyCode: 13});
return this;
},

assertDropDownDisplayed() {
console.debug(`drop down displayed?`);
expect(dropDown.getDOMNode().style.display).to.equal('block');
return this;
},

assertDropDownNotDisplayed() {
console.debug(`is drop down not displayed?`);
expect(dropDown.getDOMNode().style.display).to.equal('none');
return this;
},

assertNumberOfSuggestions(expectedNumberOfSuggestions) {
console.debug(`are there ${expectedNumberOfSuggestions} suggestions?`);
var children = dropDown.getDOMNode().children;
expect(children.length).to.equal(expectedNumberOfSuggestions);
return this;
},

assertSuggestions(expectedSuggestions) {
console.debug(`are the suggestions ${expectedSuggestions}?`);
let suggestions = TestUtils.scryRenderedDOMComponentsWithClass(dropDown, 'Suggestion');
var values = suggestions.map((suggestion) => {
return suggestion.getDOMNode().innerHTML;
});
expect(values).to.eql(expectedSuggestions);
return this;
},

assertSelectedSuggestion(expected) {
console.debug(`is the selected suggestion ${expected}?`);
let selected = TestUtils.scryRenderedDOMComponentsWithClass(dropDown, 'selected');
if (expected) {
expect(selected.length).to.equal(1);
expect(selected[0].getDOMNode().innerHTML).to.equal(expected);
} else {
expect(selected.length).to.equal(0);
}
return this;
}
return this;
}
}
26 changes: 13 additions & 13 deletions test/auto-suggest-tests.js
@@ -1,7 +1,7 @@

import React from 'react/addons';
import AutoSuggest from '../src/AutoSuggest';
import Asserter from './assertions/Asserter';
import asserter from './assertions/Asserter';
import chai from 'chai';

const { TestUtils } = React.addons;
Expand All @@ -27,7 +27,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest);
asserter(autoSuggest);
});


Expand All @@ -36,7 +36,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.assertDropDownNotDisplayed()
;
});
Expand All @@ -47,7 +47,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.assertDropDownNotDisplayed()
.enterNewValue('C').assertDropDownDisplayed()
;
Expand All @@ -59,7 +59,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('C').assertSuggestions(['one', 'two', 'three'])
;
});
Expand All @@ -70,7 +70,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('C').assertSelectedSuggestion()
.pressArrowDown().assertSelectedSuggestion('one')
.pressArrowDown().assertSelectedSuggestion('two')
Expand All @@ -86,7 +86,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('C').assertSelectedSuggestion()
.pressArrowUp().assertSelectedSuggestion('three')
.pressArrowUp().assertSelectedSuggestion('two')
Expand All @@ -102,7 +102,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('cat').assertValue('cat');
});

Expand All @@ -117,7 +117,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('cat').assertValue('cat');
});

Expand All @@ -127,7 +127,7 @@ describe('AutoSuggest', () => {
<AutoSuggest suggestions={fetchSuggestions} onSuggestion={onSuggestion}/>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('cat').assertDropDownDisplayed()
.pressEscape().assertDropDownNotDisplayed()
});
Expand All @@ -146,7 +146,7 @@ describe('AutoSuggest', () => {
</AutoSuggest>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('C').assertSuggestions(['one', 'two', 'three'])
;
});
Expand All @@ -169,7 +169,7 @@ describe('AutoSuggest', () => {
</AutoSuggest>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('C').assertSuggestions(['one', 'two', 'three'])
;
});
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('AutoSuggest', () => {
</AutoSuggest>
);

new Asserter(autoSuggest)
asserter(autoSuggest)
.enterNewValue('c').assertValue('c').assertSuggestions(['cow', 'cat', 'chicken'])
.pressArrowDown().assertValue('cow')
.pressArrowDown().assertValue('cat')
Expand Down

0 comments on commit dbeba19

Please sign in to comment.