Skip to content

Commit

Permalink
test_order_and_update_product
Browse files Browse the repository at this point in the history
  • Loading branch information
khungking909 committed Apr 3, 2024
1 parent be583dc commit 4562d16
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .rubocop-rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ RSpec/VerifiedDoubles:

RSpec/VoidExpect:
Enabled: false
RSpec/MultipleMemoizedHelpers:
Enabled: false
2 changes: 1 addition & 1 deletion app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def load_product
return if @product

flash[:admin_error] = t("admin.products.load.not_found")
redirect_to(admin_orders_path)
redirect_to(admin_products_path)
end

def filter_products
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/orders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</table>
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
<div class="d-flex w-75 align-items-center justify-content-end mt-3">
<div class="me-5"><h3> <%= t("orders.total_price") %> : <%= total_price @order%></h3> </div>
<div class="me-5"><h3> <%= t("orders.total_price") %> : <%= total_price @order %></h3> </div>
<div class="d-flex flex-column ps-5 pe-5 bg-light border border-2 border-dark rounded">
<%= form_for(:order, method: :patch, data: { turbo: false }) do |f| %>
<div><%= hidden_field_tag :status, :accept %></div>
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
is_deleted: false
)

file_path = ENV["PC_OF_TRUONG_PATH"]
file_path = "#{ENV['IMAGE_PATH']}image#{n + 1}.jpeg"
File.open(file_path) do |file|
product.image.attach(io: file, filename: "#{product.name}.jpg")
end
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions spec/factories/order_histories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

FactoryBot.define do
factory :order_history do
order_id { 1 }
product_id { 1 }
quantity { Faker::Number.between(from: 1, to: 100) }
current_price { Faker::Number.between(from: 1, to: 100) }
end
end
1 change: 0 additions & 1 deletion spec/factories/order.rb → spec/factories/orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
receiver_name { Faker::Name.name }
receiver_address { Faker::Address.full_address }
receiver_phone_number { Faker::PhoneNumber.phone_number }
status { rand(0..3) }
end
end
139 changes: 139 additions & 0 deletions spec/requests/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# frozen_string_literal: true

require "rails_helper"
require "support/shared_context/login_with_admin_context"
require "support/shared_context/login_fails_context"

RSpec.describe(Admin::OrdersController, type: :controller) do
include SessionsHelper
include OrdersHelper

let(:admin_user) { FactoryBot.create(:account, role: Account.roles[:admin], email: Faker::Internet.email, password: Faker::Internet.password) }
let(:user) { FactoryBot.create(:account, role: Account.roles[:user], email: Faker::Internet.email, password: Faker::Internet.password) }
let(:account) { FactoryBot.create(:account) }
let(:order) { FactoryBot.create(:order, account_id: account.id) }

def perform_action(method, action)
case method
when :get
case action
when :index
get(:index)
when :show
get(:show, params: { id: order.id })
end
when :patch
patch(:update, params: { id: order.id, status: :accept, comment: :accept })
end
end

describe "GET #index" do
let!(:orders) { create_list(:order, 4, account_id: user.id) }

context "when login success and render success" do
include_context "with a logged-in with admin", :get, :index

it "render index" do
expect(response).to(render_template("index"))
expect(assigns(:orders)).to(all(be_a(Order)))
expect(assigns(:orders).count).to(eq(2))
expect(assigns(:pagy).count).to(eq(4))
expect(response).to(have_http_status(:success))
end
end

include_context "login fails", :get, :index
end

describe "GET #show" do
let(:category) { create(:category) }
let(:product) { create(:product, category_id: category.id) }
let!(:order_histories) { create_list(:order_history, 6, order_id: order.id, product_id: product.id, current_price: 500, quantity: 1) }

context "when login success" do
include_context "with a logged-in with admin", :get, :show

context "render show sucess" do
it "render show success" do
expect(response).to(render_template("show"))
expect(assigns(:order_products)).to(all(be_a(OrderHistory)))
expect(assigns(:order_products).count).to(eq(5))
expect(total_price(assigns(:order))).to(eq(3000))
expect(assigns(:pagy).count).to(eq(6))
expect(response).to(have_http_status(:success))
end
end

context "render show fails" do
it "render show fails because order not found" do
get :show, params: { id: -1 }

expect(flash[:admin_error]).to(eq(I18n.t("orders.not_found")))
expect(response).to(have_http_status(:found))
expect(response).to(redirect_to(admin_orders_path))
end
end
end

include_context "login fails", :get, :show
end

describe "PATCH #update" do
let!(:order_pending_status) { create(:order, account_id: account.id) }
let!(:order_pending_approved) { create(:order, account_id: account.id, status: Order.statuses[:approved]) }

context "when login success and render success" do
include_context "with a logged-in with admin", :patch, :update

context "when order.status = 'pending' is " do
it "sets flash[:admin_success] with 'accept_order' message" do
order_pending_status = create(:order, account_id: account.id)
patch :update, params: { id: order_pending_status.id, status: :accept, comment: :accept }

expect(assigns(:order).reload.status_approved?).to(eq(true))
expect(flash[:admin_success]).to(eq(I18n.t("orders.accept_order", name: order_pending_status.receiver_name)))
expect(response).to(redirect_to(admin_orders_path))
end

it "sets flash[:admin_success] with 'refuse_order' message" do
order_pending_status = create(:order, account_id: account.id)
patch :update, params: { id: order_pending_status.id, status: :other_status_difficult_accept, comment: :refuse }

expect(assigns(:order).reload.status_reject?).to(eq(true))
expect(flash[:admin_success]).to(eq(I18n.t("orders.refuse_order", name: order_pending_status.receiver_name)))
expect(response).to(redirect_to(admin_orders_path))
end
end

context "when order.status # 'pending' is " do
it "sets flash[:admin_success] with 'accept_order' message" do
patch :update, params: { id: order_pending_approved.id, status: :accept, comment: :accept }

expect(assigns(:order).reload.status).to(eq(order_pending_approved.status))
expect(flash[:admin_error]).to(eq(I18n.t("orders.status_fails")))
expect(response).to(redirect_to(admin_orders_path))
end
end

context "when update fails" do
it "when update fails because was not found order" do
patch :update, params: { id: -1, status: :reject }

expect(flash[:admin_error]).to(eq(I18n.t("orders.not_found")))
expect(response).to(redirect_to(admin_orders_path))
end

it "when update fails because was activeRecord:Rollback" do
allow_any_instance_of(Order).to(receive(:update_status).and_return(false))

patch :update, params: { id: order_pending_status.id, status: :accept, comment: :accept }

expect(flash[:admin_error]).to(eq(I18n.t("orders.raise_error")))
expect(response).to(redirect_to(admin_orders_path))
end
end
end

include_context "login fails", :patch, :update
end
end
43 changes: 27 additions & 16 deletions spec/requests/admin/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def perform_action(method, action)
patch(:update, params: { id: product.id, product: params })
end
end

describe "GET #index" do
let!(:products) { create_list(:product, 6, category_id: category.id) }

Expand All @@ -50,7 +51,7 @@ def perform_action(method, action)
expect(response).to(render_template("index"))
expect(assigns(:products)).to(all(be_a(Product)))
expect(assigns(:products).count).to(eq(5))
expect(assigns(:pagy).count).to(eq(Product.count))
expect(assigns(:pagy).count).to(eq(6))
expect(response).to(have_http_status(:success))
end
end
Expand All @@ -72,11 +73,11 @@ def perform_action(method, action)

context "render edit fails" do
it "render edit fails" do
get :edit, params: { id: 9_999_999_999_999_999 }
get :edit, params: { id: -1 }

expect(flash[:admin_error]).to(eq(I18n.t("admin.products.load.not_found")))
expect(response).to(have_http_status(:found))
expect(response).to(redirect_to(admin_orders_path))
expect(response).to(redirect_to(admin_products_path))
end
end
end
Expand Down Expand Up @@ -104,9 +105,8 @@ def perform_action(method, action)

context "render update sucess" do
it "render update success" do
@product = create(:product, category_id: category.id)
@params = {
id: @product.id,
params = {
id: product.id,
product: {
name: Faker::Name.name,
price: rand(5..100),
Expand All @@ -116,19 +116,20 @@ def perform_action(method, action)
}
}

patch :update, params: @params
patch :update, params: params

expect(assigns(:product).name).to(eq(@params[:product][:name]))
expect(assigns(:product).name).to(eq(params[:product][:name]))
expect(flash[:admin_success]).to(eq(I18n.t("admin.products.update.success")))
expect(response).to(redirect_to(admin_products_path))
end
end

context "render update fails" do
let!(:product_update) { create(:product, category_id: category.id, name: "Iphone") }

it "render update fails with" do
@product = create(:product, category_id: category.id, name: "Iphone")
params = {
id: @product.id,
id: product_update.id,
product: {
name: nil,
price: nil,
Expand All @@ -140,7 +141,7 @@ def perform_action(method, action)

patch :update, params: params

expect(Product.last.name).to(eq(@product.name))
expect(Product.last.name).to(eq(product_update.name))
expect(response).to(render_template(:edit))
expect(response).to(have_http_status(:unprocessable_entity))
end
Expand Down Expand Up @@ -192,9 +193,7 @@ def perform_action(method, action)

context "delete sucess" do
it "delete success" do
@product = create(:product, category_id: category.id)

delete(:destroy, params: { id: @product.id })
delete(:destroy, params: { id: product.id })

expect(assigns(:product).is_deleted).to(eq(true))
expect(flash[:admin_success]).to(eq(I18n.t("admin.products.delete.success")))
Expand All @@ -204,12 +203,24 @@ def perform_action(method, action)
end

context "delete fails" do
let!(:product_delete) { create(:product, category_id: category.id) }
let!(:order_delete) { create(:order, account_id: current_account.id) }
let!(:order_history_delete) { create(:order_history, order_id: order_delete.id, product_id: product_delete.id) }

it "delete fails with" do
delete(:destroy, params: { id: 99_999_999_999_999_999 })
delete(:destroy, params: { id: -1 })

expect(flash[:admin_error]).to(eq(I18n.t("admin.products.load.not_found")))
expect(response).to(have_http_status(:found))
expect(response).to(redirect_to(admin_orders_path))
expect(response).to(redirect_to(admin_products_path))
end

it "delete fails with product of order status pending" do
delete(:destroy, params: { id: product_delete.id })

expect(flash[:admin_error]).to(eq(I18n.t("admin.products.delete.fail")))
expect(response).to(have_http_status(:found))
expect(response).to(redirect_to(admin_products_path))
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions spec/support/shared_context/login_fails_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.shared_examples("login fails") do |method, action|
let!(:user_login) { create(:account, role: Account.roles[:user]) }

it "login fails" do
perform_action(method, action)

Expand All @@ -10,8 +12,7 @@
end

it "not admin" do
@user = create(:account, role: Account.roles[:user])
log_in @user
log_in user_login

perform_action(method, action)

Expand Down
5 changes: 3 additions & 2 deletions spec/support/shared_context/login_with_admin_context.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

RSpec.shared_context("with a logged-in with admin") do |method, action|
let!(:admin_login) { create(:account, role: Account.roles[:admin]) }

before do
@admin = create(:account, role: Account.roles[:admin])
log_in(@admin)
log_in(admin_login)
@initial_count = Product.count
perform_action(method, action)
end
Expand Down

0 comments on commit 4562d16

Please sign in to comment.