123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- export function getSelector(options = {}) {
- let selector = '';
- if (options.selector) {
- return options.selector;
- }
- if (options.name) {
- selector = `[name="${options.name}"]`;
- }
- const field = `Select${options.control ? 'Control' : 'Field'}`;
- return `${field}${selector}`;
- }
- export function openMenu(wrapper, options = {}) {
- const selector = getSelector(options);
- const control = wrapper.find(`${selector} SelectContainer Control`).at(options.at || 0);
- control.prop('innerProps').onMouseDown({target: {tagName: 'INPUT'}});
- control.find('input').simulate('focus');
- return wrapper;
- }
- export function clearValue(wrapper) {
- wrapper.find('.Select-clear-zone').simulate('mouseDown', {button: 0});
- }
- export function findOption(wrapper, {value, label} = {}, options) {
- const selector = getSelector(options);
- const valueSelector = !!value ? 'value' : 'label';
- return wrapper.find(`${selector} Option[${valueSelector}="${value || label}"]`);
- }
- export function selectByLabel(wrapper, label, options = {}) {
- openMenu(wrapper, options);
- findOption(wrapper, {label}, options).at(0).simulate('click');
- }
- export function getOptionByLabel(wrapper, label, options = {}) {
- openMenu(wrapper, options);
- return findOption(wrapper, {label}, options).at(0);
- }
- export function selectByValue(wrapper, value, options = {}) {
- openMenu(wrapper, options);
- findOption(wrapper, {value}, options).at(0).simulate('click');
- }
- // used for the text input to replicate a user typing
- export function changeInputValue(element, value) {
- element.instance().value = value;
- element.simulate('change', {target: {value}});
- }
- export async function selectByQuery(wrapper, query, options = {}) {
- openMenu(wrapper, options);
- const selector = getSelector({...options, control: true});
- const input = wrapper.find(`${selector} input[type="text"]`);
- changeInputValue(input, query);
- await tick(); // Hit the mock URL.
- selectByValue(wrapper, query, options);
- }
- export async function selectByValueAsync(wrapper, value, options = {}) {
- openMenu(wrapper, options);
- await tick();
- wrapper.update();
- findOption(wrapper, {value}, options).at(0).simulate('click');
- }
|