From ba0f10c73f5f6704f3b40242e5ee8400b3b374d1 Mon Sep 17 00:00:00 2001 From: Yuri S Date: Fri, 22 Dec 2017 06:34:43 +0200 Subject: [PATCH 1/3] Fixed focused option --- src/Select.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Select.js b/src/Select.js index 06d519c7ec..7969bdd0c8 100644 --- a/src/Select.js +++ b/src/Select.js @@ -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 }); } } From ef604c6e945aa5d95f072bc6d3f399d0e6d3ea64 Mon Sep 17 00:00:00 2001 From: Yuri S Date: Sat, 23 Dec 2017 19:22:40 +0200 Subject: [PATCH 2/3] Added tests --- test/Select-test.js | 337 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) diff --git a/test/Select-test.js b/test/Select-test.js index 3b00677e4d..56b40f54a7 100644 --- a/test/Select-test.js +++ b/test/Select-test.js @@ -4521,4 +4521,341 @@ describe('Select', () => { }); }); }); + + describe('handleMouseDown method', () => { + it('for isFocused=false should set _openAfterFocus and call focus, setState, preventDefault', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'yo', + }, + preventDefault, + }; + + instance = createControl({ + openOnClick: true, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + instance.state.isFocused = false; + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + 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 }); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('for isFocused=true and _focusAfterClear=false should call focus, setState, preventDefault', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'yo', + }, + preventDefault, + }; + + instance = createControl({ + openOnClick: true, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + 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 + }); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('for isFocused=true and _focusAfterClear=true should set _focusAfterClear and call focus, setState, preventDefault', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'yo', + }, + preventDefault, + }; + + instance = createControl({ + openOnClick: true, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + instance._focusAfterClear = true; + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + 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 + }); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('for searchable=false and should call focus, setState, preventDefault', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'yo', + }, + preventDefault, + }; + + instance = createControl({ + searchable: false, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + + const focusStub = sinon.stub(instance, 'focus'); + const 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 }); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('for tagName="INPUT", isFocused=false should call only focus', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'INPUT', + }, + preventDefault, + }; + + instance = createControl({ + openOnClick: true, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + instance.state.isFocused = false; + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + 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'); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('for tagName="INPUT", isFocused=true, isOpen=false should call setState', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'INPUT', + }, + preventDefault, + }; + + instance = createControl({ + openOnClick: true, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + instance.state.isFocused = true; + instance.state.isOpen = false; + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + 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 }); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('for tagName="INPUT", isFocused=true, isOpen=true should return', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'INPUT', + }, + preventDefault, + }; + + instance = createControl({ + openOnClick: true, + value: 'two', + options: [ + { value: 'one', label: 'One' }, + { value: 'two', label: 'Two' }, + { value: 'three', label: 'Three' } + ] + }); + instance.state.isFocused = true; + instance.state.isOpen = true; + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + instance.handleMouseDown(event); + + expect(preventDefault, 'was not called'); + expect(focusStub, 'was not called'); + expect(setStateStub, 'was not called'); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('should return for disabled', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 0, + target: { + tagName: 'INPUT', + }, + preventDefault, + }; + + instance = createControl({ + disabled: true, + value: 'two', + options: [] + }); + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + instance.handleMouseDown(event); + + expect(preventDefault, 'was not called'); + expect(focusStub, 'was not called'); + expect(setStateStub, 'was not called'); + + focusStub.restore(); + setStateStub.restore(); + }); + + it('should return for button !=0', () => { + const preventDefault = sinon.spy(); + const event = { + type: 'mousedown', + button: 2, + target: { + tagName: 'INPUT', + }, + preventDefault, + }; + + instance = createControl({ + value: '', + options: [] + }); + + const focusStub = sinon.stub(instance, 'focus'); + const setStateStub = sinon.stub(instance, 'setState'); + + instance.handleMouseDown(event); + + expect(preventDefault, 'was not called'); + expect(focusStub, 'was not called'); + expect(setStateStub, 'was not called'); + + focusStub.restore(); + setStateStub.restore(); + }); + }); }); From 022ce935867fa14f73ff0490f16fb3846588e930 Mon Sep 17 00:00:00 2001 From: Yuri S Date: Sat, 23 Dec 2017 22:58:39 +0200 Subject: [PATCH 3/3] Refactored previous added tests --- test/Select-test.js | 195 +++++++------------------------------------- 1 file changed, 29 insertions(+), 166 deletions(-) diff --git a/test/Select-test.js b/test/Select-test.js index 56b40f54a7..436cc90783 100644 --- a/test/Select-test.js +++ b/test/Select-test.js @@ -4523,9 +4523,14 @@ describe('Select', () => { }); describe('handleMouseDown method', () => { - it('for isFocused=false should set _openAfterFocus and call focus, setState, preventDefault', () => { - const preventDefault = sinon.spy(); - const event = { + let preventDefault = {}; + let event = {}; + let focusStub = {}; + let setStateStub = {}; + + beforeEach(() => { + preventDefault = sinon.spy(); + event = { type: 'mousedown', button: 0, target: { @@ -4536,18 +4541,19 @@ describe('Select', () => { instance = createControl({ openOnClick: true, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] }); - instance.state.isFocused = false; - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); + 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 ); @@ -4559,35 +4565,9 @@ describe('Select', () => { expect(setStateStub, 'was called once'); expect(instance._openAfterFocus, 'to equal', true ); expect(setStateStub, 'was called with', { focusedOption: null }); - - focusStub.restore(); - setStateStub.restore(); }); it('for isFocused=true and _focusAfterClear=false should call focus, setState, preventDefault', () => { - const preventDefault = sinon.spy(); - const event = { - type: 'mousedown', - button: 0, - target: { - tagName: 'yo', - }, - preventDefault, - }; - - instance = createControl({ - openOnClick: true, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] - }); - - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); - expect(instance._focusAfterClear, 'to equal', false ); expect(instance.state.isFocused, 'to equal', true ); @@ -4603,35 +4583,11 @@ describe('Select', () => { focusedOption: null }); - focusStub.restore(); - setStateStub.restore(); }); it('for isFocused=true and _focusAfterClear=true should set _focusAfterClear and call focus, setState, preventDefault', () => { - const preventDefault = sinon.spy(); - const event = { - type: 'mousedown', - button: 0, - target: { - tagName: 'yo', - }, - preventDefault, - }; - - instance = createControl({ - openOnClick: true, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] - }); instance._focusAfterClear = true; - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); - expect(instance._focusAfterClear, 'to equal', true ); expect(instance.state.isFocused, 'to equal', true ); @@ -4647,34 +4603,13 @@ describe('Select', () => { isPseudoFocused: false, focusedOption: null }); - - focusStub.restore(); - setStateStub.restore(); }); it('for searchable=false and should call focus, setState, preventDefault', () => { - const preventDefault = sinon.spy(); - const event = { - type: 'mousedown', - button: 0, - target: { - tagName: 'yo', - }, - preventDefault, - }; - - instance = createControl({ - searchable: false, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] - }); + instance = createControl({ searchable: false }); - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); + focusStub = sinon.stub(instance, 'focus'); + setStateStub = sinon.stub(instance, 'setState'); const isOpen = instance.state.isOpen; instance.handleMouseDown(event); @@ -4683,14 +4618,10 @@ describe('Select', () => { expect(focusStub, 'was called once'); expect(setStateStub, 'was called once'); expect(setStateStub, 'was called with', { isOpen: !isOpen }); - - focusStub.restore(); - setStateStub.restore(); }); it('for tagName="INPUT", isFocused=false should call only focus', () => { - const preventDefault = sinon.spy(); - const event = { + event = { type: 'mousedown', button: 0, target: { @@ -4699,36 +4630,19 @@ describe('Select', () => { preventDefault, }; - instance = createControl({ - openOnClick: true, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] - }); instance.state.isFocused = false; - - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); 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'); - - focusStub.restore(); - setStateStub.restore(); }); it('for tagName="INPUT", isFocused=true, isOpen=false should call setState', () => { - const preventDefault = sinon.spy(); - const event = { + event = { type: 'mousedown', button: 0, target: { @@ -4737,35 +4651,19 @@ describe('Select', () => { preventDefault, }; - instance = createControl({ - openOnClick: true, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] - }); instance.state.isFocused = true; instance.state.isOpen = false; - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); - 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 }); - - focusStub.restore(); - setStateStub.restore(); }); it('for tagName="INPUT", isFocused=true, isOpen=true should return', () => { - const preventDefault = sinon.spy(); - const event = { + event = { type: 'mousedown', button: 0, target: { @@ -4774,34 +4672,18 @@ describe('Select', () => { preventDefault, }; - instance = createControl({ - openOnClick: true, - value: 'two', - options: [ - { value: 'one', label: 'One' }, - { value: 'two', label: 'Two' }, - { value: 'three', label: 'Three' } - ] - }); instance.state.isFocused = true; instance.state.isOpen = true; - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); - instance.handleMouseDown(event); expect(preventDefault, 'was not called'); expect(focusStub, 'was not called'); expect(setStateStub, 'was not called'); - - focusStub.restore(); - setStateStub.restore(); }); it('should return for disabled', () => { - const preventDefault = sinon.spy(); - const event = { + event = { type: 'mousedown', button: 0, target: { @@ -4810,28 +4692,20 @@ describe('Select', () => { preventDefault, }; - instance = createControl({ - disabled: true, - value: 'two', - options: [] - }); + instance = createControl({ disabled: true }); - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); + 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'); - - focusStub.restore(); - setStateStub.restore(); }); it('should return for button !=0', () => { - const preventDefault = sinon.spy(); - const event = { + event = { type: 'mousedown', button: 2, target: { @@ -4840,22 +4714,11 @@ describe('Select', () => { preventDefault, }; - instance = createControl({ - value: '', - options: [] - }); - - const focusStub = sinon.stub(instance, 'focus'); - const setStateStub = sinon.stub(instance, 'setState'); - instance.handleMouseDown(event); expect(preventDefault, 'was not called'); expect(focusStub, 'was not called'); expect(setStateStub, 'was not called'); - - focusStub.restore(); - setStateStub.restore(); }); }); });