Skip to content

Commit a25b831

Browse files
authored
Merge pull request solidusio#6101 from chaimann/admin-promotion-categories-add-edit
Admin promotion categories add/edit
2 parents ad4f5be + c87a012 commit a25b831

File tree

37 files changed

+488
-95
lines changed

37 files changed

+488
-95
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Lint/AmbiguousBlockAssociation:
134134
Exclude:
135135
- "*/spec/**/*"
136136
- "spec/**/*" # For the benefit of apps that inherit from this config
137+
- "**/shared_examples/**/*"
137138

138139
# We use eval to add common_spree_dependencies into the Gemfiles of each of our gems
139140
Security/Eval:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_examples_for 'promotion categories features' do
4+
before { sign_in create(:admin_user, email: "[email protected]") }
5+
6+
it "lists promotion categories" do
7+
create(factory_name, name: "test1", code: "code1")
8+
create(factory_name, name: "test2", code: "code2")
9+
10+
visit index_path
11+
expect(page).to have_content("test1")
12+
expect(page).to have_content("test2")
13+
14+
expect(page).to be_axe_clean
15+
end
16+
17+
it 'allows to create new promo category' do
18+
visit index_path
19+
20+
click_on "Add new"
21+
expect(turbo_frame_modal).to have_content("New Promotion Category")
22+
23+
fill_in "Code", with: "ste.1"
24+
click_on "Add Promotion Category"
25+
26+
expect(turbo_frame_modal).to have_content("can't be blank")
27+
28+
fill_in "Name", with: "Soon to expire"
29+
click_on "Add Promotion Category"
30+
31+
expect(page).to have_content("Promotion Category was successfully created.")
32+
expect(page).to have_content("Soon to expire")
33+
expect(page).to have_content("ste.1")
34+
expect(model_class.count).to eq(1)
35+
end
36+
37+
it 'allows to update promo category' do
38+
create(factory_name, name: "Soon to expire", code: "ste.1")
39+
40+
visit index_path
41+
42+
click_on "Soon to expire"
43+
expect(turbo_frame_modal).to have_content("Edit Promotion Category")
44+
45+
fill_in "Name", with: "Expired"
46+
fill_in "Code", with: "exp.2"
47+
click_on "Update Promotion Category"
48+
49+
expect(page).to have_content("Promotion Category was successfully updated.")
50+
expect(page).to have_content("Expired")
51+
expect(page).to have_content("exp.2")
52+
end
53+
54+
it 'allows to delete promo category' do
55+
create(factory_name, name: "Soon to expire", code: "ste.1")
56+
create(factory_name, name: "Expired", code: "exp.2")
57+
58+
visit index_path
59+
60+
select_row("Expired")
61+
click_on "Delete"
62+
expect(page).to have_content("Promotion Categories were successfully removed.")
63+
expect(page).not_to have_content("Expired")
64+
expect(model_class.count).to eq(1)
65+
end
66+
end
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

admin/spec/spec_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
end
99
SimpleCov.command_name('solidus:admin')
1010
SimpleCov.merge_timeout(3600)
11-
SimpleCov.start('rails')
11+
SimpleCov.start('rails') do
12+
add_filter '/shared_examples/'
13+
end
1214
end
1315

1416
require 'solidus_admin'

bin/build

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ echo "* Testing Solidus Sample *"
4141
echo "**************************"
4242
bin/rspec sample/spec
4343

44+
echo "**************************"
45+
echo "* Testing Legacy Promotions *"
46+
echo "**************************"
47+
bin/rspec legacy_promotions/spec
48+
49+
echo "**************************"
50+
echo "* Testing Solidus Promotions *"
51+
echo "**************************"
52+
bin/rspec promotions/spec
53+
4454
if [ -n "$COVERAGE" ]; then
4555
# Generate coverage report
4656
echo "******************************"

bin/rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LIBS = %w[
99
core
1010
sample
1111
legacy_promotions
12+
promotions
1213
]
1314

1415
# Ignore line info, e.g. foo/bar.rb:123 would become foo/bar.rb

core/lib/spree/testing_support/capybara_ext.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ def find_label_by_text(text)
110110
# find the original.
111111
find('label:not(.select2-offscreen)', text: /#{Regexp.escape(text)}/i, match: :one)
112112
end
113+
114+
def dialog(parent: 'body', **options)
115+
within(parent) do
116+
find('dialog', visible: :all, **options)
117+
end
118+
end
119+
120+
def turbo_frame_modal
121+
dialog(parent: find('turbo-frame', visible: :all))
122+
end
113123
end
114124
end
115125
end

legacy_promotions/config/locales/promotion_categories.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ en:
22
solidus_admin:
33
promotion_categories:
44
title: "Promotion Categories"
5+
create:
6+
success: "Promotion Category was successfully created."
7+
update:
8+
success: "Promotion Category was successfully updated."
59
destroy:
610
success: "Promotion Categories were successfully removed."

legacy_promotions/config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
extend SolidusAdmin::AdminResources
2424

2525
admin_resources :promotions, only: [:index, :destroy]
26-
admin_resources :promotion_categories, only: [:index, :destroy]
26+
admin_resources :promotion_categories, except: [:show]
2727
end
2828
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @promotion_category, url: form_url, html: { id: form_id } do |f| %>
4+
<div class="flex flex-col gap-6 pb-4">
5+
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
6+
<%= render component("ui/forms/field").text_field(f, :code, class: "required") %>
7+
</div>
8+
<% modal.with_actions do %>
9+
<form method="dialog">
10+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
11+
</form>
12+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<% end %>

0 commit comments

Comments
 (0)