|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.shared_examples_for 'CRUD resource requests' do |resource_name| |
| 4 | + let(:admin_user) { create(:admin_user) } |
| 5 | + let(:resource) { create(factory) } |
| 6 | + |
| 7 | + # Overridables |
| 8 | + let(:factory) { resource_name.to_sym } |
| 9 | + let(:url_helpers) { solidus_admin } |
| 10 | + |
| 11 | + before do |
| 12 | + allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user) |
| 13 | + end |
| 14 | + |
| 15 | + describe "GET /index" do |
| 16 | + it "renders the index template with a 200 OK status" do |
| 17 | + get url_helpers.public_send("#{resource_name.pluralize}_path") |
| 18 | + expect(response).to have_http_status(:ok) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe "GET /new" do |
| 23 | + it "renders the new template with a 200 OK status" do |
| 24 | + get url_helpers.public_send("new_#{resource_name}_path") |
| 25 | + expect(response).to have_http_status(:ok) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + describe "POST /create" do |
| 30 | + context "with valid parameters" do |
| 31 | + it "creates a new #{resource_name.humanize}" do |
| 32 | + expect { |
| 33 | + post url_helpers.public_send("#{resource_name.pluralize}_path"), params: { resource_name => valid_attributes } |
| 34 | + }.to change(resource_class, :count).by(1) |
| 35 | + end |
| 36 | + |
| 37 | + it "redirects to the index page with a 303 See Other status" do |
| 38 | + post url_helpers.public_send("#{resource_name.pluralize}_path"), params: { resource_name => valid_attributes } |
| 39 | + expect(response).to redirect_to(url_helpers.public_send("#{resource_name.pluralize}_path")) |
| 40 | + expect(response).to have_http_status(:see_other) |
| 41 | + end |
| 42 | + |
| 43 | + it "displays a success flash message" do |
| 44 | + post url_helpers.public_send("#{resource_name.pluralize}_path"), params: { resource_name => valid_attributes } |
| 45 | + follow_redirect! |
| 46 | + expect(response.body).to include("#{resource_name.humanize} was successfully created.") |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context "with invalid parameters" do |
| 51 | + let(:invalid_attributes) { { name: "", code: "", active: true } } |
| 52 | + |
| 53 | + it "does not create a new #{resource_name.humanize}" do |
| 54 | + expect { |
| 55 | + post url_helpers.public_send("#{resource_name.pluralize}_path"), params: { resource_name => invalid_attributes } |
| 56 | + }.not_to change(resource_class, :count) |
| 57 | + end |
| 58 | + |
| 59 | + it "renders the new template with unprocessable_entity status" do |
| 60 | + post url_helpers.public_send("#{resource_name.pluralize}_path"), params: { resource_name => invalid_attributes } |
| 61 | + expect(response).to have_http_status(:unprocessable_entity) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe "GET /edit" do |
| 67 | + it "renders the edit template with a 200 OK status" do |
| 68 | + get url_helpers.public_send("edit_#{resource_name}_path", resource) |
| 69 | + expect(response).to have_http_status(:ok) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "PATCH /update" do |
| 74 | + context "with valid parameters" do |
| 75 | + it "updates the #{resource_name.humanize}" do |
| 76 | + patch url_helpers.public_send("#{resource_name}_path", resource), params: { resource_name => valid_attributes } |
| 77 | + resource.reload |
| 78 | + valid_attributes.each do |attr, value| |
| 79 | + expect(resource.public_send(attr)).to eq(value) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + it "redirects to the index page with a 303 See Other status" do |
| 84 | + patch url_helpers.public_send("#{resource_name}_path", resource), params: { resource_name => valid_attributes } |
| 85 | + expect(response).to redirect_to(url_helpers.public_send("#{resource_name.pluralize}_path")) |
| 86 | + expect(response).to have_http_status(:see_other) |
| 87 | + end |
| 88 | + |
| 89 | + it "displays a success flash message" do |
| 90 | + patch url_helpers.public_send("#{resource_name}_path", resource), params: { resource_name => valid_attributes } |
| 91 | + follow_redirect! |
| 92 | + expect(response.body).to include("#{resource_name.humanize} was successfully updated.") |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context "with invalid parameters" do |
| 97 | + it "does not update the #{resource_name.humanize}" do |
| 98 | + expect { |
| 99 | + patch url_helpers.public_send("#{resource_name}_path", resource), params: { resource_name => invalid_attributes } |
| 100 | + }.not_to change { resource.reload } |
| 101 | + end |
| 102 | + |
| 103 | + it "renders the edit template with unprocessable_entity status" do |
| 104 | + patch url_helpers.public_send("#{resource_name}_path", resource), params: { resource_name => invalid_attributes } |
| 105 | + expect(response).to have_http_status(:unprocessable_entity) |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + describe "DELETE /destroy" do |
| 111 | + it "deletes the #{resource_name.humanize} and redirects to the index page with a 303 See Other status" do |
| 112 | + # This ensures resource exists prior to deletion. |
| 113 | + resource |
| 114 | + expect { |
| 115 | + delete url_helpers.public_send("#{resource_name}_path", resource) |
| 116 | + }.to change(resource_class, :count).by(-1) |
| 117 | + |
| 118 | + expect(response).to redirect_to(url_helpers.public_send("#{resource_name.pluralize}_path")) |
| 119 | + expect(response).to have_http_status(:see_other) |
| 120 | + end |
| 121 | + |
| 122 | + it "displays a success flash message after deletion" do |
| 123 | + delete url_helpers.public_send("#{resource_name}_path", resource) |
| 124 | + follow_redirect! |
| 125 | + expect(response.body).to include("#{resource_name.humanize.pluralize} were successfully removed.") |
| 126 | + end |
| 127 | + |
| 128 | + it 'allows to bulk delete resources' do |
| 129 | + ids = [create(factory), create(factory)].map(&:id) |
| 130 | + expect { |
| 131 | + delete url_helpers.public_send("#{resource_name.pluralize}_path", id: ids) |
| 132 | + }.to change { resource_class.count }.by(-ids.size) |
| 133 | + |
| 134 | + expect(response).to redirect_to(url_helpers.public_send("#{resource_name.pluralize}_path")) |
| 135 | + expect(response).to have_http_status(:see_other) |
| 136 | + end |
| 137 | + end |
| 138 | +end |
0 commit comments