2

I want to test sorting made with jQuery sortable from within my integration tests. In order to do so, the testing-framework (capybara, selenium) allows me to call page.execute_script("activeAdminSortable().sortable().update()").

By triggering the update event with the proper parameters, in I can simulate a drag-and-drop to test if it works. The relevant parts of the JavaScript code is:

(function($) {
  $(document).ready(function() {
    $('tbody .grabber').parents('tbody').activeAdminSortable();
  });

  $.fn.activeAdminSortable = function() {
    this.sortable({
      update: function(event, ui) {
        $.ajax({
          axis: 'y',
          cursor: 'move',
          url: ui.item.find('[data-sort-url]').data('sort-url'),
          type: 'post',
          data: { position: ui.item.index() + 1 },
          success: activeAdminSortableFlashFinished
        });
      }
    });

    this.disableSelection();
  }
})(jQuery);

I need to find a way to trigger the update event and pass it the correct parameters. Is there any easy way to craft such an event, and ui parameter? And to trigger the update.

Alternatively, I could write some JavaScript that actually grabs, drags and drops the items, but that seems rather brittle to me. Or is that trivial?

2 Answers 2

2

Add .trigger("update") where you have .update()

Sign up to request clarification or add additional context in comments.

1 Comment

The dot after trigger is not correct. You want `.trigger("update");
1

I am using $('#sortable').trigger('sortupdate') successfully for this same purpose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.