|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe "SolidusAdmin::ProductTaxonsController", type: :request do |
| 6 | + let(:admin_user) { create(:admin_user) } |
| 7 | + let!(:product) { create(:product) } |
| 8 | + |
| 9 | + before do |
| 10 | + allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user) |
| 11 | + end |
| 12 | + |
| 13 | + describe "GET /new" do |
| 14 | + it "renders the new template with a 200 OK status" do |
| 15 | + get solidus_admin.new_product_taxon_path(product) |
| 16 | + expect(response).to have_http_status(:ok) |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + describe "POST /create" do |
| 21 | + context "when taxon belongs to a parent" do |
| 22 | + context "with valid parameters" do |
| 23 | + let!(:parent_taxon) { create(:taxonomy).root } |
| 24 | + let(:valid_attributes) { { name: "Accessories", parent_id: parent_taxon.id } } |
| 25 | + |
| 26 | + it "creates new taxon and new classification" do |
| 27 | + expect(Spree::Taxon.count).to eq(1) |
| 28 | + expect(product.classifications.count).to eq(0) |
| 29 | + |
| 30 | + post solidus_admin.product_taxons_path(product), params: { taxon: valid_attributes } |
| 31 | + |
| 32 | + expect(Spree::Taxon.count).to eq(2) |
| 33 | + expect(product.classifications.count).to eq(1) |
| 34 | + end |
| 35 | + |
| 36 | + it "redirects with a 303 See Other status" do |
| 37 | + post solidus_admin.product_taxons_path(product), params: { taxon: valid_attributes } |
| 38 | + expect(response).to redirect_to(solidus_admin.product_path(product)) |
| 39 | + expect(response).to have_http_status(:see_other) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context "with invalid parameters" do |
| 44 | + let(:invalid_attributes) { { name: "" } } |
| 45 | + |
| 46 | + it "does not create a new taxon" do |
| 47 | + expect { |
| 48 | + post solidus_admin.product_taxons_path(product), params: { taxon: invalid_attributes } |
| 49 | + }.not_to change(Spree::Taxon, :count) |
| 50 | + end |
| 51 | + |
| 52 | + it "returns unprocessable_entity status" do |
| 53 | + post solidus_admin.product_taxons_path(product), params: { taxon: invalid_attributes } |
| 54 | + expect(response).to have_http_status(:unprocessable_entity) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context "when taxon is a root" do |
| 60 | + context "with valid parameters" do |
| 61 | + let(:valid_attributes) { { name: "Accessories", parent_id: nil } } |
| 62 | + |
| 63 | + it "creates new taxonomy, new root taxon and new classification" do |
| 64 | + expect(Spree::Taxonomy.count).to eq(0) |
| 65 | + expect(Spree::Taxon.count).to eq(0) |
| 66 | + expect(product.classifications.count).to eq(0) |
| 67 | + |
| 68 | + post solidus_admin.product_taxons_path(product), params: { taxon: valid_attributes } |
| 69 | + |
| 70 | + expect(Spree::Taxonomy.count).to eq(1) |
| 71 | + expect(Spree::Taxon.count).to eq(1) |
| 72 | + expect(product.classifications.count).to eq(1) |
| 73 | + end |
| 74 | + |
| 75 | + it "redirects with a 303 See Other status" do |
| 76 | + post solidus_admin.product_taxons_path(product), params: { taxon: valid_attributes } |
| 77 | + expect(response).to redirect_to(solidus_admin.product_path(product)) |
| 78 | + expect(response).to have_http_status(:see_other) |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + context "with invalid parameters" do |
| 83 | + let!(:another_root_taxon) { create(:taxonomy, name: "Apparel").root } |
| 84 | + let(:invalid_attributes) { { name: another_root_taxon.name, parent_id: nil } } |
| 85 | + |
| 86 | + it "does not create new records" do |
| 87 | + expect(Spree::Taxonomy.count).to eq(1) |
| 88 | + expect(Spree::Taxon.count).to eq(1) |
| 89 | + expect(product.classifications.count).to eq(0) |
| 90 | + |
| 91 | + post solidus_admin.product_taxons_path(product), params: { taxon: invalid_attributes } |
| 92 | + |
| 93 | + expect(Spree::Taxonomy.count).to eq(1) |
| 94 | + expect(Spree::Taxon.count).to eq(1) |
| 95 | + expect(product.classifications.count).to eq(0) |
| 96 | + end |
| 97 | + |
| 98 | + it "returns unprocessable_entity status" do |
| 99 | + post solidus_admin.product_taxons_path(product), params: { taxon: invalid_attributes } |
| 100 | + expect(response).to have_http_status(:unprocessable_entity) |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments