|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.shared_examples_for "requests: moveable" do |
| 4 | + let(:admin_user) { create(:admin_user) } |
| 5 | + let(:record) { create(factory, position: 1) } |
| 6 | + let(:request_path) do |
| 7 | + solidus_admin.send("move_#{record.model_name.singular_route_key}_path", record, format: :js) |
| 8 | + end |
| 9 | + |
| 10 | + before do |
| 11 | + allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user) |
| 12 | + end |
| 13 | + |
| 14 | + describe "PATCH /move" do |
| 15 | + it "updates record's position" do |
| 16 | + expect { patch request_path, params: { position: 2 } }.to change { record.reload.position }.from(1).to(2) |
| 17 | + expect(response).to have_http_status(:no_content) |
| 18 | + end |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +RSpec.shared_examples_for "features: sortable" do |
| 23 | + let(:factory_attrs) { {} } |
| 24 | + let(:scope) { "body" } |
| 25 | + |
| 26 | + before do |
| 27 | + create(factory, displayed_attribute => "First", position: 1, **factory_attrs) |
| 28 | + create(factory, displayed_attribute => "Second", position: 2, **factory_attrs) |
| 29 | + visit path |
| 30 | + end |
| 31 | + |
| 32 | + it "allows sorting via drag and drop" do |
| 33 | + within(scope) do |
| 34 | + expect(find("[data-controller='sortable']").all(:xpath, "./*").first).to have_text("First") |
| 35 | + expect(find("[data-controller='sortable']").all(:xpath, "./*").last).to have_text("Second") |
| 36 | + |
| 37 | + rows = find("[data-controller='sortable']").all(:xpath, "./*") |
| 38 | + rows[1].drag_to rows[0] |
| 39 | + |
| 40 | + expect(find("[data-controller='sortable']").all(:xpath, "./*").first).to have_text("Second") |
| 41 | + expect(find("[data-controller='sortable']").all(:xpath, "./*").last).to have_text("First") |
| 42 | + end |
| 43 | + end |
| 44 | +end |
0 commit comments