import React from 'react';
import { shallow, mount } from 'enzyme';
import { CustomInput } from '../';
describe('Custom Inputs', () => {
describe('CustomCheckbox', () => {
it('should render an optional label', () => {
const checkbox = mount();
expect(checkbox.find('label').text()).toBe('Yo!');
});
it('should render children in the outer div', () => {
const checkbox = shallow();
expect(checkbox.type()).toBe('div');
});
it('should render an input with the type of checkbox', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('type')).toBe('checkbox');
});
it('should pass id to both the input and label nodes', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('id')).toBe('yo');
expect(checkbox.find('label').prop('htmlFor')).toBe('yo');
});
it('should pass id to both the input and label nodes, with an overriden for on the label node', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('id')).toBe('yo');
expect(checkbox.find('label').prop('htmlFor')).toBe('custom-for');
});
it('should pass classNames to the outer div', () => {
const checkbox = mount();
expect(checkbox.find('.custom-control').prop('className').indexOf('yo') > -1).toBeTruthy();
});
it('should add class is-invalid when invalid is true', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('className').indexOf('is-invalid') > -1).toBeTruthy();
});
it('should add class is-valid when valid is true', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('className').indexOf('is-valid') > -1).toBeTruthy();
});
it('should pass all other props to the input node', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('data-testprop')).toBe('yo');
});
it('should reference innerRef to the input node', () => {
const ref = React.createRef();
mount();
expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLInputElement);
});
});
describe('CustomRadio', () => {
it('should render an optional label', () => {
const radio = mount();
expect(radio.find('label').text()).toBe('Yo!');
});
it('should render children in the outer div', () => {
const radio = shallow();
expect(radio.type()).toBe('div');
});
it('should render an input with the type of radio', () => {
const radio = mount();
expect(radio.find('input').prop('type')).toBe('radio');
});
it('should add class is-invalid when invalid is true', () => {
const radio = mount();
expect(radio.find('input').prop('className').indexOf('is-invalid') > -1).toBeTruthy();
});
it('should add class is-valid when valid is true', () => {
const radio = mount();
expect(radio.find('input').prop('className').indexOf('is-valid') > -1).toBeTruthy();
});
it('should pass id to both the input and label nodes', () => {
const radio = mount();
expect(radio.find('input').prop('id')).toBe('yo');
expect(radio.find('label').prop('htmlFor')).toBe('yo');
});
it('should pass id to both the input and label nodes, with an overriden for on the label node', () => {
const radio = mount();
expect(radio.find('input').prop('id')).toBe('yo');
expect(radio.find('label').prop('htmlFor')).toBe('custom-for');
});
it('should pass classNames to the outer div', () => {
const radio = mount();
expect(radio.find('.custom-control').prop('className').indexOf('yo') > -1).toBeTruthy();
});
it('should pass all other props to the input node', () => {
const radio = mount();
expect(radio.find('input').prop('data-testprop')).toBe('yo');
});
it('should reference innerRef to the input node', () => {
const ref = React.createRef();
mount();
expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLInputElement);
});
});
describe('CustomSwitch', () => {
it('should render an optional label', () => {
const checkbox = mount();
expect(checkbox.find('label').text()).toBe('Yo!');
});
it('should render children in the outer div', () => {
const checkbox = shallow();
expect(checkbox.type()).toBe('div');
});
it('should render an input with the type of checkbox', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('type')).toBe('checkbox');
});
it('should pass id to both the input and label nodes', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('id')).toBe('yo');
expect(checkbox.find('label').prop('htmlFor')).toBe('yo');
});
it('should pass id to both the input and label nodes, with an overriden for on the label node', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('id')).toBe('yo');
expect(checkbox.find('label').prop('htmlFor')).toBe('custom-for');
});
it('should pass classNames to the outer div', () => {
const checkbox = mount();
expect(checkbox.find('.custom-control').prop('className').indexOf('yo') > -1).toBeTruthy();
});
it('should add class is-invalid when invalid is true', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('className').indexOf('is-invalid') > -1).toBeTruthy();
});
it('should add class is-valid when valid is true', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('className').indexOf('is-valid') > -1).toBeTruthy();
});
it('should pass all other props to the input node', () => {
const checkbox = mount();
expect(checkbox.find('input').prop('data-testprop')).toBe('yo');
});
it('should reference innerRef to the input node', () => {
const ref = React.createRef();
mount();
expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLInputElement);
});
});
describe('CustomSelect', () => {
it('should render children in the outer div', () => {
const select = shallow();
expect(select.type()).toBe('select');
});
it('should add class is-invalid when invalid is true', () => {
const select = mount();
expect(select.find('select').prop('className').indexOf('is-invalid') > -1).toBeTruthy();
});
it('should add class is-valid when valid is true', () => {
const select = mount();
expect(select.find('select').prop('className').indexOf('is-valid') > -1).toBeTruthy();
});
it('should add the size class when bsSize is provided', () => {
const select = mount();
expect(select.find('select').prop('className').indexOf('custom-select-lg') > -1).toBeTruthy();
});
it('should pass classNames to the outer div', () => {
const select = mount();
expect(select.find('.custom-select').prop('className').indexOf('yo') > -1).toBeTruthy();
});
it('should pass all other props to the input node', () => {
const select = mount();
expect(select.find('select').prop('data-testprop')).toBe('yo');
});
it('should remove type prop from the input node', () => {
const select = mount();
expect(select.find('select').prop('type')).toBeUndefined();
});
it('should reference innerRef to the select node', () => {
const ref = React.createRef();
mount();
expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLSelectElement);
});
});
describe('CustomFile', () => {
it('should render children in the outer div', () => {
const file = mount();
expect(file.find('.custom-file').first().type()).toBe('div');
});
it('should add class is-invalid when invalid is true', () => {
const file = mount();
expect(file.find('input').prop('className').indexOf('is-invalid') > -1).toBeTruthy();
});
it('should add class is-valid when valid is true', () => {
const file = mount();
expect(file.find('input').prop('className').indexOf('is-valid') > -1).toBeTruthy();
});
it('should render an input with the type of file', () => {
const file = mount();
expect(file.find('input').prop('type')).toBe('file');
});
it('should pass id to both the input and label nodes', () => {
const file = mount();
expect(file.find('input').prop('id')).toBe('yo');
expect(file.find('label').prop('htmlFor')).toBe('yo');
});
it('should pass id to both the input and label nodes, with an overriden for on the label node', () => {
const file = mount();
expect(file.find('input').prop('id')).toBe('yo');
expect(file.find('label').prop('htmlFor')).toBe('custom-for');
});
it('should pass classNames to the outer div', () => {
const file = mount();
expect(file.find('.custom-file').prop('className').indexOf('yo') > -1).toBeTruthy();
});
it('should set the label when provided', () => {
const file = mount();
expect(file.find('label').text()).toBe('yo');
});
it('should pass all other props to the input node', () => {
const file = mount();
expect(file.find('input').prop('data-testprop')).toBe('yo');
});
it('should reference innerRef to the input node', () => {
const ref = React.createRef();
mount();
expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLInputElement);
});
describe('onChange', () => {
it('calls props.onChange if it exists', () => {
const onChange = jest.fn();
const file = mount();
file.find('input').hostNodes().simulate('change');
expect(onChange).toHaveBeenCalled();
});
});
it('removes fakepath from file name', () => {
const file = mount();
file.find('input').hostNodes().simulate('change', {
target:{
value:'C:\\fakepath\\test.txt'
}
});
expect(file.find('.custom-file-label').text()).toBe('test.txt');
});
it('lists multiple files when supported', () => {
const file = mount();
file.find('input').hostNodes().simulate('change', {
target:{
value:'C:\\fakepath\\file1.txt',
files:[
{name:"file1.txt"},
{name:'file2.txt'},
{name:'file3.txt'},
]
}
})
expect(file.find('.custom-file-label').text()).toBe('file1.txt, file2.txt, file3.txt');
})
});
describe('CustomRange', () => {
it('should render children in the outer div', () => {
const range = shallow();
expect(range.type()).toBe('input');
});
it('should add class is-invalid when invalid is true', () => {
const range = mount();
expect(range.find('input').prop('className').indexOf('is-invalid') > -1).toBeTruthy();
});
it('should add class is-valid when valid is true', () => {
const range = mount();
expect(range.find('input').prop('className').indexOf('is-valid') > -1).toBeTruthy();
});
it('should render an input with the type of range', () => {
const range = mount();
expect(range.find('input').prop('type')).toBe('range');
});
it('should pass all other props to the input node', () => {
const range = mount();
expect(range.find('input').prop('data-testprop')).toBe('yo');
});
it('should reference innerRef to the input node', () => {
const ref = React.createRef();
mount();
expect(ref.current).not.toBeNull();
expect(ref.current).toBeInstanceOf(HTMLInputElement);
});
});
});