Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bug fix and tests for clearable prop
Pressing escape when clearable=false and menu
is closed now has no effect
  • Loading branch information
bruderstein committed Jul 27, 2015
commit 240c8a0abfcfcbb32922ca3f74d25349355acfe8
2 changes: 1 addition & 1 deletion src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ var Select = React.createClass({
case 27: // escape
if (this.state.isOpen) {
this.resetValue();
} else {
} else if (this.props.clearable) {
this.clearValue(event);
}
break;
Expand Down
141 changes: 139 additions & 2 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ describe('Select', function() {
);

// Focus on the input, such that mouse events are accepted
searchInputNode = instance.getInputNode().getDOMNode().querySelector('input');
searchInputNode = React.findDOMNode(instance.getInputNode()).querySelector('input');
TestUtils.Simulate.focus(searchInputNode);
return instance;

Expand All @@ -843,6 +843,143 @@ describe('Select', function() {
});
});

describe('clearable=true', function () {

beforeEach(function () {

var instance = createControl({
clearable: true,
options: defaultOptions,
value: 'three'
});

expect(React.findDOMNode(instance), 'queried for', DISPLAYED_SELECTION_SELECTOR,
'to have items satisfying', 'to have text', 'Three');

});

describe('on pressing escape', function () {

beforeEach(function () {

pressEscape();
});

it('calls onChange with empty', function () {

expect(onChange, 'was called with', '');
});

it('resets the display value', function () {

expect(React.findDOMNode(instance), 'queried for', DISPLAYED_SELECTION_SELECTOR,
'to have items satisfying', 'to have text', 'Select...');
});

it('resets the control value', function () {

expect(React.findDOMNode(instance).querySelector('input').value, 'to equal', '');
});
});

describe('on clicking `clear`', function () {

beforeEach(function () {
TestUtils.Simulate.click(React.findDOMNode(instance).querySelector('.Select-clear'));
});

it('calls onChange with empty', function () {

expect(onChange, 'was called with', '');
});

it('resets the display value', function () {

expect(React.findDOMNode(instance), 'queried for', DISPLAYED_SELECTION_SELECTOR,
'to have items satisfying', 'to have text', 'Select...');
});

it('resets the control value', function () {

expect(React.findDOMNode(instance).querySelector('input').value, 'to equal', '');
});
});
});

describe('clearable=false', function () {

beforeEach(function () {

var instance = createControl({
clearable: false,
options: defaultOptions,
value: 'three'
});

expect(React.findDOMNode(instance), 'queried for', DISPLAYED_SELECTION_SELECTOR,
'to have items satisfying', 'to have text', 'Three');

});

it('does not render a clear button', function () {

expect(React.findDOMNode(instance).querySelectorAll('.Select-clear'), 'to have length', 0);
});

describe('on escape', function () {
beforeEach(function () {

pressEscape();
});

it('does not call onChange', function () {

expect(onChange, 'was not called');
});

it('does not reset the display value', function () {

expect(React.findDOMNode(instance), 'queried for', DISPLAYED_SELECTION_SELECTOR,
'to have items satisfying', 'to have text', 'Three');
});

it('does not reset the control value', function () {

expect(React.findDOMNode(instance).querySelector('input').value, 'to equal', 'three');
});

});

describe('when open', function () {

beforeEach(function () {

typeSearchText('abc');
});

describe('on escape', function () {

beforeEach(function () {
pressEscape();
});

it('closes the menu', function () {

expect(React.findDOMNode(instance).querySelectorAll('.Select-menu'), 'to have length', 0);
});

it('resets the control value to the original', function () {

expect(React.findDOMNode(instance).querySelector('input').value, 'to equal', 'three');
});

it('renders the original display label', function () {

expect(React.findDOMNode(instance), 'queried for', DISPLAYED_SELECTION_SELECTOR,
'to have items satisfying', 'to have text', 'Three');
});
});
});
});
});

});