简体   繁体   English

触发select2的事件以在RSpec中进行测试

[英]Trigger event of select2 for testing in RSpec

I have a select2 on my page which has the following onchange event: 我的页面上有一个select2,它具有以下onchange事件:

$("#product_menu_category_id").on("change", function(e) {
  $.ajax( {
  url: '/product_prices',
  data: {id: e.val, product_id: <%= @product.id %> },
  success:  function(data){ data }
  })
})

and use the follow helper function to set a value during testing: 并在测试过程中使用跟随帮助器功能设置一个值:

def set_select2_value(index, id, value)
  "$('.select2-container').each(function(index) {if (index == " + index.to_s + ") $(this).select2('data', {id: '" + id.to_s + "', text: '"+ value + "'})});"
end

This works perfect for all the select2 controls that don't have a onchange trigger hooked to them. 对于所有没有钩挂onchange触发器的select2控件,这都是完美的选择。

How can I set the value during my RSpec (capybara) tests and also trigger any onchange event (if one would be present)? 如何在RSpec(水豚)测试期间设置值,并触发任何onchange事件(如果存在)?

最后找到了一种方法,您需要使用{triggerChange:true})并在屏幕外的select而不是select2上设置值:

"$('select.select2-offscreen').each(function(index) {if (index == " + index.to_s + ") $(this).select2('val', '" + id.to_s + "', {triggerChange:true}); });"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM