-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5821 from MadelineCollier/admin-update-store-cred…
…it-reason [Admin] Store Credit Reasons edit/update & New request specs to appease Codecov
- Loading branch information
Showing
10 changed files
with
259 additions
and
8 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
admin/app/components/solidus_admin/store_credit_reasons/edit/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<%= turbo_frame_tag :edit_store_credit_reason_modal do %> | ||
<%= render component("ui/modal").new(title: t(".title")) do |modal| %> | ||
<%= form_for @store_credit_reason, url: solidus_admin.store_credit_reason_path(@store_credit_reason), html: { id: form_id } do |f| %> | ||
<div class="flex flex-col gap-6 pb-4"> | ||
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %> | ||
<label class="flex gap-2 items-center"> | ||
<%= hidden_field_tag "#{f.object_name}[active]", "0" %> | ||
<%= render component("ui/forms/checkbox").new( | ||
name: "#{f.object_name}[active]", | ||
value: "1", | ||
checked: f.object.active | ||
) %> | ||
<span class="font-semibold text-xs ml-2"><%= Spree::StoreCreditReason.human_attribute_name :active %></span> | ||
<%= render component("ui/toggletip").new(text: t(".hints.active")) %> | ||
</label> | ||
</div> | ||
<% modal.with_actions do %> | ||
<form method="dialog"> | ||
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> | ||
</form> | ||
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<%= render component("store_credit_reasons/index").new(page: @page) %> |
12 changes: 12 additions & 0 deletions
12
admin/app/components/solidus_admin/store_credit_reasons/edit/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::StoreCreditReasons::Edit::Component < SolidusAdmin::BaseComponent | ||
def initialize(page:, store_credit_reason:) | ||
@page = page | ||
@store_credit_reason = store_credit_reason | ||
end | ||
|
||
def form_id | ||
dom_id(@store_credit_reason, "#{stimulus_id}_edit_store_credit_reason_form") | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
admin/app/components/solidus_admin/store_credit_reasons/edit/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Add your component translations here. | ||
# Use the translation in the example in your template with `t(".hello")`. | ||
en: | ||
title: "Edit Store Credit Reason" | ||
cancel: "Cancel" | ||
submit: "Update Store Credit Reason" | ||
hints: | ||
active: "When checked, this store credit reason will be available for selection when adding store credit to orders." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
describe "Store Credit Reasons", :js, type: :feature do | ||
before { sign_in create(:admin_user, email: '[email protected]') } | ||
|
||
it "lists Store Credit Reasons and allows deleting them" do | ||
it "lists store credit reasons and allows deleting them" do | ||
create(:store_credit_reason, name: "Default-store-credit-reason") | ||
|
||
visit "/admin/store_credit_reasons" | ||
|
@@ -14,7 +14,7 @@ | |
|
||
select_row("Default-store-credit-reason") | ||
click_on "Delete" | ||
expect(page).to have_content("Store Credit Reasons were successfully removed.") | ||
expect(page).to have_content("Store credit reasons were successfully removed.") | ||
expect(page).not_to have_content("Default-store-credit-reason") | ||
expect(Spree::StoreCreditReason.count).to eq(0) | ||
expect(page).to be_axe_clean | ||
|
@@ -43,7 +43,7 @@ | |
|
||
click_on "Add Store Credit Reason" | ||
|
||
expect(page).to have_content("Store Credit Reason was successfully created.") | ||
expect(page).to have_content("Store credit reason was successfully created.") | ||
expect(Spree::StoreCreditReason.find_by(name: "New Reason")).to be_present | ||
expect(page.current_url).to include(query) | ||
end | ||
|
@@ -58,4 +58,34 @@ | |
end | ||
end | ||
end | ||
|
||
context "when editing an existing store credit reason" do | ||
let(:query) { "?page=1&q%5Bname_cont%5D=customer" } | ||
|
||
before do | ||
Spree::StoreCreditReason.create(name: "New Customer Reward") | ||
visit "/admin/store_credit_reasons#{query}" | ||
find_row("New Customer Reward").click | ||
expect(page).to have_content("Edit Store Credit Reason") | ||
expect(page).to be_axe_clean | ||
end | ||
|
||
it "opens a modal" do | ||
expect(page).to have_selector("dialog") | ||
within("dialog") { click_on "Cancel" } | ||
expect(page).not_to have_selector("dialog") | ||
expect(page.current_url).to include(query) | ||
end | ||
|
||
it "successfully updates the existing store credit reason" do | ||
fill_in "Name", with: "Customer complaint" | ||
|
||
click_on "Update Store Credit Reason" | ||
expect(page).to have_content("Store credit reason was successfully updated.") | ||
expect(page).to have_content("Customer complaint") | ||
expect(page).not_to have_content("New Customer Reward") | ||
expect(Spree::StoreCreditReason.find_by(name: "Customer complaint")).to be_present | ||
expect(page.current_url).to include(query) | ||
end | ||
end | ||
end |
133 changes: 133 additions & 0 deletions
133
admin/spec/requests/solidus_admin/store_credit_reasons_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe "SolidusAdmin::StoreCreditReasonsController", type: :request do | ||
let(:admin_user) { create(:admin_user) } | ||
let(:store_credit_reason) { create(:store_credit_reason) } | ||
|
||
before do | ||
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user) | ||
end | ||
|
||
describe "GET /index" do | ||
it "renders the index template with a 200 OK status" do | ||
get solidus_admin.store_credit_reasons_path | ||
expect(response).to have_http_status(:ok) | ||
end | ||
end | ||
|
||
describe "GET /new" do | ||
it "renders the new template with a 200 OK status" do | ||
get solidus_admin.new_store_credit_reason_path | ||
expect(response).to have_http_status(:ok) | ||
end | ||
end | ||
|
||
describe "POST /create" do | ||
context "with valid parameters" do | ||
let(:valid_attributes) { { name: "Customer Loyalty", active: true } } | ||
|
||
it "creates a new StoreCreditReason" do | ||
expect { | ||
post solidus_admin.store_credit_reasons_path, params: { store_credit_reason: valid_attributes } | ||
}.to change(Spree::StoreCreditReason, :count).by(1) | ||
end | ||
|
||
it "redirects to the index page with a 303 See Other status" do | ||
post solidus_admin.store_credit_reasons_path, params: { store_credit_reason: valid_attributes } | ||
expect(response).to redirect_to(solidus_admin.store_credit_reasons_path) | ||
expect(response).to have_http_status(:see_other) | ||
end | ||
|
||
it "displays a success flash message" do | ||
post solidus_admin.store_credit_reasons_path, params: { store_credit_reason: valid_attributes } | ||
follow_redirect! | ||
expect(response.body).to include("Store credit reason was successfully created.") | ||
end | ||
end | ||
|
||
context "with invalid parameters" do | ||
let(:invalid_attributes) { { name: "", active: true } } | ||
|
||
it "does not create a new StoreCreditReason" do | ||
expect { | ||
post solidus_admin.store_credit_reasons_path, params: { store_credit_reason: invalid_attributes } | ||
}.not_to change(Spree::StoreCreditReason, :count) | ||
end | ||
|
||
it "renders the new template with unprocessable_entity status" do | ||
post solidus_admin.store_credit_reasons_path, params: { store_credit_reason: invalid_attributes } | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
end | ||
end | ||
end | ||
|
||
describe "GET /edit" do | ||
it "renders the edit template with a 200 OK status" do | ||
get solidus_admin.edit_store_credit_reason_path(store_credit_reason) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
end | ||
|
||
describe "PATCH /update" do | ||
context "with valid parameters" do | ||
let(:valid_attributes) { { name: "Updated Reason", active: false } } | ||
|
||
it "updates the store credit reason" do | ||
patch solidus_admin.store_credit_reason_path(store_credit_reason), params: { store_credit_reason: valid_attributes } | ||
store_credit_reason.reload | ||
expect(store_credit_reason.name).to eq("Updated Reason") | ||
expect(store_credit_reason.active).to be(false) | ||
end | ||
|
||
it "redirects to the index page with a 303 See Other status" do | ||
patch solidus_admin.store_credit_reason_path(store_credit_reason), params: { store_credit_reason: valid_attributes } | ||
expect(response).to redirect_to(solidus_admin.store_credit_reasons_path) | ||
expect(response).to have_http_status(:see_other) | ||
end | ||
|
||
it "displays a success flash message" do | ||
patch solidus_admin.store_credit_reason_path(store_credit_reason), params: { store_credit_reason: valid_attributes } | ||
follow_redirect! | ||
expect(response.body).to include("Store credit reason was successfully updated.") | ||
end | ||
end | ||
|
||
context "with invalid parameters" do | ||
let(:invalid_attributes) { { name: "", active: false } } | ||
|
||
it "does not update the store credit reason" do | ||
original_name = store_credit_reason.name | ||
patch solidus_admin.store_credit_reason_path(store_credit_reason), params: { store_credit_reason: invalid_attributes } | ||
store_credit_reason.reload | ||
expect(store_credit_reason.name).to eq(original_name) | ||
end | ||
|
||
it "renders the edit template with unprocessable_entity status" do | ||
patch solidus_admin.store_credit_reason_path(store_credit_reason), params: { store_credit_reason: invalid_attributes } | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE /destroy" do | ||
it "deletes the store credit reason and redirects to the index page with a 303 See Other status" do | ||
# This ensures the store_credit_reason exists prior to deletion. | ||
store_credit_reason | ||
|
||
expect { | ||
delete solidus_admin.store_credit_reason_path(store_credit_reason) | ||
}.to change(Spree::StoreCreditReason, :count).by(-1) | ||
|
||
expect(response).to redirect_to(solidus_admin.store_credit_reasons_path) | ||
expect(response).to have_http_status(:see_other) | ||
end | ||
|
||
it "displays a success flash message after deletion" do | ||
delete solidus_admin.store_credit_reason_path(store_credit_reason) | ||
follow_redirect! | ||
expect(response.body).to include("Store credit reasons were successfully removed.") | ||
end | ||
end | ||
end |