Skip to content

Commit

Permalink
Refactored previous added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-sakharov committed Dec 23, 2017
1 parent ef604c6 commit 022ce93
Showing 1 changed file with 29 additions and 166 deletions.
195 changes: 29 additions & 166 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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 );
Expand All @@ -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 );

Expand All @@ -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 );

Expand All @@ -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);
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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();
});
});
});

0 comments on commit 022ce93

Please sign in to comment.