|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.shared_examples_for 'promotion categories requests' do |
| 4 | + let(:admin_user) { create(:admin_user) } |
| 5 | + |
| 6 | + before do |
| 7 | + allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user) |
| 8 | + end |
| 9 | + |
| 10 | + describe "GET /index" do |
| 11 | + it "renders the index template with a 200 OK status" do |
| 12 | + get url_helpers.promotion_categories_path |
| 13 | + expect(response).to have_http_status(:ok) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + describe "GET /new" do |
| 18 | + it "renders the new template with a 200 OK status" do |
| 19 | + get url_helpers.new_promotion_category_path |
| 20 | + expect(response).to have_http_status(:ok) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe "POST /create" do |
| 25 | + context "with valid parameters" do |
| 26 | + let(:valid_attributes) { { name: "Expired", code: "exp.1" } } |
| 27 | + let(:run_request) { post url_helpers.promotion_categories_path, params: { promotion_category: valid_attributes } } |
| 28 | + |
| 29 | + it "creates a new promotion category" do |
| 30 | + expect { run_request }.to change(model_class, :count).by(1) |
| 31 | + end |
| 32 | + |
| 33 | + it "redirects to the index page with a 303 See Other status" do |
| 34 | + run_request |
| 35 | + expect(response).to redirect_to(url_helpers.promotion_categories_path) |
| 36 | + expect(response).to have_http_status(:see_other) |
| 37 | + end |
| 38 | + |
| 39 | + it "displays a success flash message" do |
| 40 | + run_request |
| 41 | + follow_redirect! |
| 42 | + expect(response.body).to include("Promotion Category was successfully created.") |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context "with invalid parameters" do |
| 47 | + let(:invalid_attributes) { { name: "", code: "" } } |
| 48 | + let(:run_request) { post url_helpers.promotion_categories_path, params: { promotion_category: invalid_attributes } } |
| 49 | + |
| 50 | + it "does not create a new promotion category" do |
| 51 | + expect { run_request }.not_to change(model_class, :count) |
| 52 | + end |
| 53 | + |
| 54 | + it "renders the new template with unprocessable_entity status" do |
| 55 | + run_request |
| 56 | + expect(response).to have_http_status(:unprocessable_entity) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + describe "GET /edit" do |
| 62 | + it "renders the edit template with a 200 OK status" do |
| 63 | + get url_helpers.edit_promotion_category_path(promotion_category) |
| 64 | + expect(response).to have_http_status(:ok) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe "PATCH /update" do |
| 69 | + context "with valid parameters" do |
| 70 | + let(:valid_attributes) { { name: "Updated", code: "upd.1" } } |
| 71 | + let(:run_request) { patch url_helpers.promotion_category_path(promotion_category), params: { promotion_category: valid_attributes } } |
| 72 | + |
| 73 | + it "updates the promotion category" do |
| 74 | + run_request |
| 75 | + promotion_category.reload |
| 76 | + expect(promotion_category.name).to eq("Updated") |
| 77 | + expect(promotion_category.code).to eq("upd.1") |
| 78 | + end |
| 79 | + |
| 80 | + it "redirects to the index page with a 303 See Other status" do |
| 81 | + run_request |
| 82 | + expect(response).to redirect_to(url_helpers.promotion_categories_path) |
| 83 | + expect(response).to have_http_status(:see_other) |
| 84 | + end |
| 85 | + |
| 86 | + it "displays a success flash message" do |
| 87 | + run_request |
| 88 | + follow_redirect! |
| 89 | + expect(response.body).to include("Promotion Category was successfully updated.") |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context "with invalid parameters" do |
| 94 | + let(:invalid_attributes) { { name: "", code: "" } } |
| 95 | + let(:run_request) { patch url_helpers.promotion_category_path(promotion_category), params: { promotion_category: invalid_attributes } } |
| 96 | + |
| 97 | + it "does not update the promotion category" do |
| 98 | + expect { run_request }.not_to change { promotion_category.reload.name } |
| 99 | + end |
| 100 | + |
| 101 | + it "renders the edit template with unprocessable_entity status" do |
| 102 | + run_request |
| 103 | + expect(response).to have_http_status(:unprocessable_entity) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe "DELETE /destroy" do |
| 109 | + before { promotion_category } |
| 110 | + |
| 111 | + let(:run_request) { delete url_helpers.promotion_category_path(promotion_category) } |
| 112 | + |
| 113 | + it "deletes the promotion category and redirects to the index page with a 303 See Other status" do |
| 114 | + expect { run_request }.to change(model_class, :count).by(-1) |
| 115 | + |
| 116 | + expect(response).to redirect_to(url_helpers.promotion_categories_path) |
| 117 | + expect(response).to have_http_status(:see_other) |
| 118 | + end |
| 119 | + |
| 120 | + it "displays a success flash message after deletion" do |
| 121 | + run_request |
| 122 | + follow_redirect! |
| 123 | + expect(response.body).to include("Promotion Categories were successfully removed.") |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments