select.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. export function getSelector(options = {}) {
  2. let selector = '';
  3. if (options.name) {
  4. selector = `[name="${options.name}"]`;
  5. }
  6. const field = `Select${options.control ? 'Control' : 'Field'}`;
  7. return `${field}${selector}`;
  8. }
  9. export function openMenu(wrapper, options = {}) {
  10. const selector = getSelector(options);
  11. wrapper.find(`${selector} input[role="combobox"]`).simulate('focus');
  12. wrapper.find(`${selector} .Select-control`).simulate('mouseDown', {button: 0});
  13. return wrapper;
  14. }
  15. export function clearValue(wrapper) {
  16. wrapper.find('.Select-clear-zone').simulate('mouseDown', {button: 0});
  17. }
  18. export function findOption(wrapper, {value, label} = {}, options) {
  19. const selector = getSelector(options);
  20. const valueSelector = !!value ? 'value' : 'label';
  21. return wrapper
  22. .find(`${selector} Option`)
  23. .findWhere(
  24. el => el.prop('option') && el.prop('option')[valueSelector] === (value || label)
  25. );
  26. }
  27. export function selectByLabel(wrapper, label, options = {}) {
  28. openMenu(wrapper, options);
  29. findOption(wrapper, {label}, options).simulate('mouseDown');
  30. }
  31. export function selectByValue(wrapper, value, options = {}) {
  32. openMenu(wrapper, options);
  33. findOption(wrapper, {value}, options).simulate('mouseDown');
  34. }