Skip to content

Commit

Permalink
update suggestor specs..
Browse files Browse the repository at this point in the history
  • Loading branch information
carloluis committed Dec 24, 2017
1 parent f9d334b commit 617c88f
Showing 1 changed file with 109 additions and 120 deletions.
229 changes: 109 additions & 120 deletions src/suggestor/__tests__/Suggestor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import renderer from 'react-test-renderer';
import ReactTestUtils from 'react-dom/test-utils';
import { shallow, mount } from 'enzyme';

import { KEY_CODES } from '../../utils/values';
import removeAccents from '../../utils/remove-accents';
import noop from '../../utils/noop';
import * as utils from '../../utils';
import Ssuggestor, { Suggestor } from '../Suggestor';

jest.mock('../../utils/remove-accents', () => {
return jest.fn(text => text);
});
const { KEY_CODES, noop } = utils;

jest.mock('../../utils/noop', () => {
return jest.fn();
Expand Down Expand Up @@ -77,138 +73,130 @@ describe('Suggestor component', () => {
PROPS.onSelect.mockReset();
});

it('should initialize state.open as false', () => {
const component = mount(<Suggestor {...PROPS} />);

expect(component.state().open).toBeFalsy();
});

it('should filter list into initial state ', () => {
const component = mount(<Suggestor {...PROPS} />);

expect(component.state()).toMatchObject({
filtered: [
{ word: 'temporise', index: 0 },
{ word: 'whencesoeve', index: 0 },
{ word: 'turophile', index: 0 },
{ word: 'umlaut', index: 0 }
],
value: PROPS.value,
index: 0
});
});

it('should call _bind when created', () => {
const spy = jest.spyOn(Suggestor.prototype, '_bind');

it('should call this.filter on component creation', () => {
const filterSpy = jest.spyOn(Suggestor.prototype, 'filter');
const component = shallow(<Suggestor {...PROPS} />);

expect(spy).toHaveBeenCalled();
expect(filterSpy).toHaveBeenCalled();
});

it('should call changeValue when remove the value: remove -> changeValue', () => {
const component = mount(<Suggestor {...PROPS} openOnClick />);

const spy = jest.spyOn(component.instance(), 'changeValue');

component.instance().remove();

expect(spy).toBeCalled();
});

it('handleItemMouseEnter -> setState', () => {
const component = mount(<Suggestor {...PROPS} />);

const spy = jest.spyOn(component.instance(), 'setState');
const index = 2;

component.instance().handleItemMouseEnter(index);
describe('mounted component', () => {
let mounted;
beforeEach(() => {
mounted = mount(<Suggestor {...PROPS} />);
});

expect(spy).toBeCalledWith({ index });
});
it('should initialize state.open as false', () => {
expect(mounted.state().open).toBeFalsy();
});

it('handleItemClick -> changeValue', () => {
const component = mount(<Suggestor {...PROPS} />);
it('should filter list into initial state ', () => {
expect(mounted.state()).toMatchObject({
filtered: [
{ word: 'temporise', index: 0 },
{ word: 'whencesoeve', index: 0 },
{ word: 'turophile', index: 0 },
{ word: 'umlaut', index: 0 }
],
value: PROPS.value,
index: 0
});
});

const spy = jest.spyOn(component.instance(), 'changeValue');
const payload = { word: 'decrassify' };
it('should call changeValue when remove the value: remove -> changeValue', () => {
// mounted.setProps({ openOnClick: true });
const spy = jest.spyOn(mounted.instance(), 'changeValue');

component.instance().handleItemClick(payload);
mounted.instance().remove();

expect(spy).toBeCalledWith(payload.word, true);
});
expect(spy).toBeCalled();
});

it('focus -> input.focus', () => {
const component = mount(<Suggestor {...PROPS} />);
it('handleItemMouseEnter -> setState', () => {
const spy = jest.spyOn(mounted.instance(), 'setState');

const spy = jest.spyOn(component.instance().input, 'focus');
mounted.instance().handleItemMouseEnter(2);

component.instance().focus();
expect(spy).toBeCalledWith({ index: 2 });
});

expect(spy).toBeCalled();
});
it('handleItemClick -> changeValue', () => {
const spy = jest.spyOn(mounted.instance(), 'changeValue');
const payload = { word: 'decrassify' };

it('changeValue -> [setState, props.onChange, props.onSelect]', () => {
const component = shallow(<Suggestor {...PROPS} />);
const instance = component.instance();
mounted.instance().handleItemClick(payload);

const spies = {
setState: jest.spyOn(instance, 'setState'),
handleClose: jest.spyOn(instance, 'handleClose')
};
expect(spy).toBeCalledWith(payload.word, true);
});

const value = 'umlaut';
instance.changeValue(value, true);
it('focus -> input.focus', () => {
const spy = jest.spyOn(mounted.instance().input, 'focus');
mounted.instance().focus();

expect(spies.setState).toBeCalled();
expect(spies.handleClose).toBeCalledWith();
expect(PROPS.onChange).toBeCalledWith(value);
expect(PROPS.onSelect).toBeCalledWith(value);
expect(spy).toBeCalled();
});
});

it('toggleList -> setState (open suggestion list)', () => {
const component = shallow(<Suggestor {...PROPS} />);

const spy = jest.spyOn(component.instance(), 'setState');
expect(component.state().open).toBeFalsy();

component.instance().toggleList();
describe('shallow component', () => {
let component, instance;
let setStateSpy, handleCloseSpy, changeValueSpy;
const autoBindSpy = jest.spyOn(utils, 'autoBind');

expect(spy).toBeCalled();
expect(component.state().open).toBeTruthy();
});
beforeEach(() => {
autoBindSpy.mockClear();
component = shallow(<Suggestor {...PROPS} />);
instance = component.instance();
setStateSpy = jest.spyOn(instance, 'setState');
handleCloseSpy = jest.spyOn(instance, 'handleClose');
changeValueSpy = jest.spyOn(instance, 'changeValue');
});

it('toggleList -> handleClose (if suggestion list is visible)', () => {
const component = shallow(<Suggestor {...PROPS} />);
afterEach(() => {
expect(autoBindSpy).toHaveBeenCalledTimes(1);
})

const spy = jest.spyOn(component.instance(), 'handleClose');
it('changeValue -> [setState, props.onChange, props.onSelect]', () => {
const value = 'umlaut';
instance.changeValue(value, true);

component.instance().toggleList();
expect(setStateSpy).toBeCalled();
expect(handleCloseSpy).toBeCalledWith();
expect(PROPS.onChange).toBeCalledWith(value);
expect(PROPS.onSelect).toBeCalledWith(value);
});

expect(spy).not.toBeCalled();
it('toggleList -> setState (open suggestion list)', () => {
expect(component.state().open).toBeFalsy();
instance.toggleList();

component.instance().toggleList();
expect(setStateSpy).toBeCalled();
expect(component.state().open).toBeTruthy();
});

expect(spy).toBeCalled();
});
it('toggleList -> handleClose (if suggestion list is visible)', () => {
instance.toggleList();

it('handleChange -> changeValue', () => {
const event = {
stopPropagation: jest.fn(),
target: {
value: 'whencesoeve'
}
};
const component = shallow(<Suggestor {...PROPS} />);
expect(handleCloseSpy).not.toBeCalled();

const spy = jest.spyOn(component.instance(), 'changeValue');
instance.toggleList();

component.instance().handleChange(event);
expect(handleCloseSpy).toBeCalled();
});

expect(spy).toBeCalled();
expect(component.state()).toMatchObject({
value: event.target.value,
open: true
it('handleChange -> changeValue', () => {
const event = {
stopPropagation: jest.fn(),
target: {
value: 'whencesoeve'
}
};
instance.handleChange(event);

expect(changeValueSpy).toBeCalled();
expect(component.state()).toMatchObject({
value: event.target.value,
open: true
});
});
});

Expand Down Expand Up @@ -472,18 +460,19 @@ describe('Suggestor component', () => {
});

describe('filter', () => {
const removeAccentsSpy = jest.spyOn(utils, 'removeAccents');
let component, instance;

beforeEach(() => {
removeAccents.mockClear();
removeAccentsSpy.mockClear();
component = shallow(<Suggestor {...PROPS} />);
instance = component.instance();
});

it('should call removeAccents (if accents not allowed)', () => {
const component = shallow(<Suggestor {...PROPS} />);
instance.filter(PROPS.list, 'illaudable');

expect(removeAccents).toBeCalled();
expect(removeAccentsSpy).toBeCalled();
});

it('should return all item on suggestion list (if onlyMatch arg set to falsy)', () => {
Expand Down Expand Up @@ -517,31 +506,31 @@ describe('Suggestor component', () => {

it('should not call removeAccents (if accents support)', () => {
component.setProps({ accents: true });
removeAccents.mockClear();
removeAccentsSpy.mockClear();

component.instance().filter(PROPS.list, 'illaudable');

expect(removeAccents).not.toBeCalled();
expect(removeAccentsSpy).not.toBeCalled();
});
});
});

describe('Suggestor - default cb props use noop', () => {
const { onChange, onSelect, onKey, ...props } = PROPS;
const component = shallow(<Suggestor {...props} />);
let component, componentProps;

beforeEach(() => {
noop.mockReset();
const { onChange, onSelect, onKey, ...props } = PROPS;
component = mount(<Suggestor {...props} />);
componentProps = component.props();
});

it('should set noop when not onChange, onSelect, oKey props func provided', () => {
const props = component.props();
expect(props.onChange).toBe(props.onSelect);
expect(props.onSelect).toBe(props.onKey);
expect(componentProps.onChange).toBe(componentProps.onSelect);
expect(componentProps.onSelect).toBe(componentProps.onKey);
});

it('sould call noop func', () => {
expect(component.instance().props.onSelect()).toBe(undefined);
expect(componentProps.onSelect()).toBe(undefined);
expect(noop).toBeCalled();
});
});
Expand Down

0 comments on commit 617c88f

Please sign in to comment.