Skip to content

Commit

Permalink
Merge pull request JedWatson#2245 from yuri-sakharov/fix/focused-option
Browse files Browse the repository at this point in the history
Fixed focused option. Issue JedWatson#2237
  • Loading branch information
JedWatson authored Jan 5, 2018
2 parents 125a32f + 022ce93 commit baafaf4
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,13 @@ class Select extends React.Component {
this.setState({
isOpen: toOpen,
isPseudoFocused: false,
focusedOption: null,
});
} else {
// otherwise, focus the input and open the menu
this._openAfterFocus = this.props.openOnClick;
this.focus();
this.setState({ focusedOption: null });
}
}

Expand Down
200 changes: 200 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4550,4 +4550,204 @@ describe('Select', () => {
});
});
});

describe('handleMouseDown method', () => {
let preventDefault = {};
let event = {};
let focusStub = {};
let setStateStub = {};

beforeEach(() => {
preventDefault = sinon.spy();
event = {
type: 'mousedown',
button: 0,
target: {
tagName: 'yo',
},
preventDefault,
};

instance = createControl({
openOnClick: true,
});

focusStub = sinon.stub(instance, 'focus');
setStateStub = sinon.stub(instance, 'setState');
});

afterEach(() => {
focusStub.restore();
setStateStub.restore();
});

it('for isFocused=false should set _openAfterFocus and call focus, setState, preventDefault', () => {
instance.state.isFocused = false;
expect(instance._openAfterFocus, 'to equal', false );
expect(instance.props.openOnClick, 'to equal', true );
expect(instance.state.isFocused, 'to equal', false );

instance.handleMouseDown(event);

expect(preventDefault, 'was called once');
expect(focusStub, 'was called once');
expect(setStateStub, 'was called once');
expect(instance._openAfterFocus, 'to equal', true );
expect(setStateStub, 'was called with', { focusedOption: null });
});

it('for isFocused=true and _focusAfterClear=false should call focus, setState, preventDefault', () => {
expect(instance._focusAfterClear, 'to equal', false );
expect(instance.state.isFocused, 'to equal', true );

instance.handleMouseDown(event);

expect(preventDefault, 'was called once');
expect(focusStub, 'was called once');
expect(setStateStub, 'was called once');
expect(setStateStub, 'was called with',
{
isOpen: true,
isPseudoFocused: false,
focusedOption: null
});

});

it('for isFocused=true and _focusAfterClear=true should set _focusAfterClear and call focus, setState, preventDefault', () => {
instance._focusAfterClear = true;

expect(instance._focusAfterClear, 'to equal', true );
expect(instance.state.isFocused, 'to equal', true );

instance.handleMouseDown(event);

expect(instance._focusAfterClear, 'to equal', false );
expect(preventDefault, 'was called once');
expect(focusStub, 'was called once');
expect(setStateStub, 'was called once');
expect(setStateStub, 'was called with',
{
isOpen: false,
isPseudoFocused: false,
focusedOption: null
});
});

it('for searchable=false and should call focus, setState, preventDefault', () => {
instance = createControl({ searchable: false });

focusStub = sinon.stub(instance, 'focus');
setStateStub = sinon.stub(instance, 'setState');
const isOpen = instance.state.isOpen;

instance.handleMouseDown(event);

expect(preventDefault, 'was called once');
expect(focusStub, 'was called once');
expect(setStateStub, 'was called once');
expect(setStateStub, 'was called with', { isOpen: !isOpen });
});

it('for tagName="INPUT", isFocused=false should call only focus', () => {
event = {
type: 'mousedown',
button: 0,
target: {
tagName: 'INPUT',
},
preventDefault,
};

instance.state.isFocused = false;
expect(instance._openAfterFocus, 'to equal', false );

instance.handleMouseDown(event);

expect(instance._openAfterFocus, 'to equal', true );
expect(preventDefault, 'was not called');
expect(focusStub, 'was called once');
expect(setStateStub, 'was not called');
});

it('for tagName="INPUT", isFocused=true, isOpen=false should call setState', () => {
event = {
type: 'mousedown',
button: 0,
target: {
tagName: 'INPUT',
},
preventDefault,
};

instance.state.isFocused = true;
instance.state.isOpen = false;

instance.handleMouseDown(event);

expect(preventDefault, 'was not called');
expect(focusStub, 'was not called');
expect(setStateStub, 'was called once');
expect(setStateStub, 'was called with', { isOpen: true, isPseudoFocused: false });
});

it('for tagName="INPUT", isFocused=true, isOpen=true should return', () => {
event = {
type: 'mousedown',
button: 0,
target: {
tagName: 'INPUT',
},
preventDefault,
};

instance.state.isFocused = true;
instance.state.isOpen = true;

instance.handleMouseDown(event);

expect(preventDefault, 'was not called');
expect(focusStub, 'was not called');
expect(setStateStub, 'was not called');
});

it('should return for disabled', () => {
event = {
type: 'mousedown',
button: 0,
target: {
tagName: 'INPUT',
},
preventDefault,
};

instance = createControl({ disabled: true });

focusStub = sinon.stub(instance, 'focus');
setStateStub = sinon.stub(instance, 'setState');

instance.handleMouseDown(event);

expect(preventDefault, 'was not called');
expect(focusStub, 'was not called');
expect(setStateStub, 'was not called');
});

it('should return for button !=0', () => {
event = {
type: 'mousedown',
button: 2,
target: {
tagName: 'INPUT',
},
preventDefault,
};

instance.handleMouseDown(event);

expect(preventDefault, 'was not called');
expect(focusStub, 'was not called');
expect(setStateStub, 'was not called');
});
});
});

0 comments on commit baafaf4

Please sign in to comment.