Capybara integration tests with jquery.selectize

后端 未结 5 1936
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 17:49

How can I write a capybara integration test with a form using jquery.selectize?

I\'d like to test a user entering a couple of values.

5条回答
  •  执笔经年
    2021-01-17 18:34

    selectize uses all key events (keydown, keypress, keyup) to provide a great UI, but doesn't seem to provide an easy way to set data from javascript.

    One solution is to use the syn.js library to trigger the right key events. Here's a helper that works:

    def fill_in_selectize_area selector, options
      # Syn appears to require an id, so assign a temporary one
      @selectize_unique_id ||= 0
      unique_id = "temp_selectize_id_#{@selectize_unique_id +=1}"
      with = options.fetch(:with)
      page.execute_script %Q{
        var selectize = $(#{selector.to_json})[0].selectize;
        var type = #{with.to_json}.join(selectize.settings.delimiter) + '\t';
        selectize.$control_input.attr('id', #{unique_id.to_json});
        Syn.click({}, #{unique_id.to_json}).delay().type(type);
      }
      # make sure that it worked *and* that it's finished:
      page.should have_content with.join('×') << '×' 
    end
    
    # example use:
    fill_in_selectize_area '[name="blog[tags]"]', with: ['awesome subject', 'other tag']
    

    Note that the delay is needed because focussing on the input isn't instant.

提交回复
热议问题