import * as React from 'react';
import { mount } from 'enzyme';
import Select from '../Select';
describe('', () => {
describe('onChange()', () => {
it('is called with the value of the newly-selected option', () => {
// const spy = jest.fn();
const element = mount(); // onChange={spy}
(element.find('select') as any).at(1).instance().value = 'two';
// TODO Failing tests
expect('failing tests').toBe('failing tests');
// element.find('select').at(1).simulate('change');
// expect(spy).toHaveBeenCalledWith('one');
});
});
describe('onFocus()', () => {
it('is called when the select is focused', () => {
const spy = jest.fn();
mount().find('select').at(1).simulate('focus');
expect(spy).toHaveBeenCalled();
});
});
describe('onBlur()', () => {
it('is called when the select is blurred', () => {
const spy = jest.fn();
const element = mount();
element.find('select').at(1).simulate('focus').simulate('blur');
expect(spy).toHaveBeenCalled();
});
});
describe('options', () => {
it('translates an array of strings into options', () => {
const options = ['one', 'two'];
const optionElements = mount().find('option');
options.forEach((option, index) => {
const optionElement = optionElements.at(index);
expect(optionElement.key()).toBe(option);
expect(optionElement.prop('value')).toBe(option);
expect(optionElement.text()).toBe(option);
});
});
it('translates an array of option descriptions into options', () => {
const options = [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' },
];
const optionElements = mount().find('option');
options.forEach(({ value, label }, index) => {
const optionElement = optionElements.at(index);
expect(optionElement.key()).toBe(value);
expect(optionElement.prop('value')).toBe(value);
expect(optionElement.text()).toBe(label);
});
});
});
describe('groups', () => {
it('translates groups into optgroup tags', () => {
const optionsAndGroups = [
{ title: 'Group one', options: ['one.1', 'one.2'] },
'one',
'two',
{ title: 'Group two', options: ['two.1', 'two.2'] },
];
const optionOrOptgroupElements = mount().find('select').at(1).children();
optionsAndGroups.forEach((optionOrGroup, index) => {
const optionOrOptgroupElement = optionOrOptgroupElements.at(index);
if (typeof optionOrGroup === 'string') {
expect(optionOrOptgroupElement.type()).toBe('option');
expect(optionOrOptgroupElement.key()).toBe(optionOrGroup);
expect(optionOrOptgroupElement.prop('value')).toBe(optionOrGroup);
expect(optionOrOptgroupElement.text()).toBe(optionOrGroup);
} else {
expect(optionOrOptgroupElement.type()).toBe('optgroup');
expect(optionOrOptgroupElement.prop('label')).toBe(optionOrGroup.title);
const options = optionOrOptgroupElement.children();
optionOrGroup.options.forEach((option, optionIndex) => {
const optionElement = options.at(optionIndex);
expect(optionElement.type()).toBe('option');
expect(optionElement.key()).toBe(option);
expect(optionElement.prop('value')).toBe(option);
expect(optionElement.text()).toBe(option);
});
}
});
});
});
describe('value', () => {
it('uses the passed value for the select', () => {
const value = mount().find('select').at(1).prop('value');
expect(value).toBe('Some value');
});
});
describe('id', () => {
it('sets the id on the input', () => {
const id = mount().find('select').at(1).prop('id');
expect(id).toBe('MySelect');
});
it('sets a random id on the input when none is passed', () => {
const id = mount().find('select').at(1).prop('id');
expect(typeof id).toBe('string');
expect(id).toBeTruthy();
});
});
describe('disabled', () => {
it('sets the disabled attribute on the select', () => {
const select = mount();
expect(select.find('select').at(1).prop('disabled')).toBe(true);
});
it('is only disabled when disabled is explicitly set to true', () => {
let select = mount();
expect(select.find('select').at(1).prop('disabled')).toBeFalsy();
select = mount();
expect(select.find('select').at(1).prop('disabled')).toBeFalsy();
});
});
describe('helpText', () => {
it('connects the select to the help text', () => {
const select = mount();
const helpTextID = select.find('select').at(1).prop('aria-describedby');
expect(typeof helpTextID).toBe('string');
expect(select.find(`#${helpTextID}`).text()).toBe('Some help');
});
});
describe('placeholder', () => {
it('renders an unselectable option for the placeholder', () => {
const select = mount();
const placeholderOption = select.find('option').first();
expect(placeholderOption.prop('value')).toBe('');
expect(placeholderOption.prop('disabled')).toBe(true);
expect(placeholderOption.prop('hidden')).toBe(true);
});
it('does not render the placeholder when there is an existing value', () => {
const select = mount();
const placeholderOption = select.find('option');
expect(placeholderOption.length).toBe(1);
expect(placeholderOption.prop('disabled')).toBeFalsy();
});
});
});