require 'rails_helper' describe Admin::MenuItemsController, :type => :controller do before(:each) do activate_authlogic u = SpudUser.new(:login => "testuser",:email => "test@testuser.com",:password => "test",:password_confirmation => "test") u.super_admin = true u.save @user = SpudUserSession.create(u) @menu = FactoryGirl.create(:spud_menu) end describe 'index' do it "should return an array of menus" do 2.times {|x| s = FactoryGirl.create(:spud_menu_item,:spud_menu_id => @menu.id,:parent_id => @menu.id)} get :index,:menu_id =>@menu.id expect(assigns(:menu_items).count).to be > 1 end it "should redirect if the menu is not found" do get :index,:menu_id => @menu.id + 1 expect(response).to redirect_to admin_menus_url end end describe 'new' do it "should assign a new menu" do get :new,:menu_id =>@menu.id expect(assigns(:menu_item)).to_not be_blank expect(response).to be_success end end describe 'create' do it "should create a new menu item with a valid form submission" do expect { post :create,:menu_id => @menu.id, :spud_menu_item => FactoryGirl.attributes_for(:spud_menu_item,:parent_id => nil).reject{|k,v| k == 'id' || k == :spud_menu_id} }.to change(SpudMenuItem,:count).by(1) expect(response).to be_redirect end it "should not create a menu item with an invalid form entry" do yamldata = FactoryGirl.attributes_for(:spud_menu_item,:name => nil).reject{|k,v| k == 'id' || k == :spud_menu_id} expect { post :create,:menu_id => @menu.id, :spud_menu_item => FactoryGirl.attributes_for(:spud_menu_item,:name => nil,:parent_id => nil).reject{|k,v| k == 'id' || k == :spud_menu_id} }.to_not change(SpudMenuItem,:count) end it "should autoset order if not specified" do menuitem = FactoryGirl.create(:spud_menu_item,:parent_id => @menu.id) post :create,:menu_id => @menu.id, :spud_menu_item => FactoryGirl.attributes_for(:spud_menu_item,:menu_order => nil,:parent_id => nil).reject{|k,v| k == 'id' || k == :spud_menu_id} expect(assigns(:menu_item).menu_order).to eq(1) end it "should assign the menu name based on page name if left blank" do page = FactoryGirl.create(:spud_page) post :create,:menu_id => @menu.id, :spud_menu_item => FactoryGirl.attributes_for(:spud_menu_item,:name => nil,:spud_page_id => page.id).reject{|k,v| k == 'id' || k == :spud_menu_id} expect(assigns(:menu_item).name).to eq(page.name) end it "should set the parent to a submenu if specified" do menuitem = FactoryGirl.create(:spud_menu_item,:parent_id => @menu.id) post :create,:menu_id => @menu.id, :spud_menu_item => FactoryGirl.attributes_for(:spud_menu_item,:parent_id => menuitem.id).reject{|k,v| k == 'id' || k == :spud_menu_id} expect(assigns(:menu_item).parent).to eq(menuitem) end end describe 'edit' do it "should respond with menu item by id" do menu1 = FactoryGirl.create(:spud_menu_item) menu2 = FactoryGirl.create(:spud_menu_item) get :edit, :menu_id => @menu.id,:id => menu2.id expect(assigns(:menu_item)).to eq(menu2) end it "should redirect_to index if menu item not found" do get :edit,:menu_id => @menu.id,:id => "345" expect(response).to redirect_to admin_menu_menu_items_url(:menu_id => @menu.id) end end describe 'update' do it "should update the name when the name attribute is changed" do menu_item = FactoryGirl.create(:spud_menu_item) new_name = 'MyMenu' expect { put :update,:menu_id => @menu.id,:id => menu_item.id, :spud_menu_item => menu_item.attributes.merge!(:name => new_name,:parent_id => nil).reject{|k,v| k.to_sym == :spud_menu_id || k == 'id' || k.to_sym == :updated_at || k.to_sym == :created_at} menu_item.reload }.to change(menu_item,:name).to(new_name) end it "should assign a menu to a submenu if parentid is defined" do menu_item1 = FactoryGirl.create(:spud_menu_item) menu_item = FactoryGirl.create(:spud_menu_item) expect { put :update,:menu_id => @menu.id,:id => menu_item.id, :spud_menu_item => menu_item.attributes.merge!(:parent_id => menu_item1.id).reject{|k,v| k.to_sym == :spud_menu_id || k == 'id' || k.to_sym == :updated_at || k.to_sym == :created_at} menu_item.reload }.to change(menu_item,:parent).to(menu_item1) end end describe 'destroy' do it "should destroy the menu item" do menu_item = FactoryGirl.create(:spud_menu_item,:parent_id => @menu.id) expect { delete :destroy,:menu_id => @menu.id,:id => menu_item.id }.to change(SpudMenuItem,:count).by(-1) end end describe 'update_sort' do it "should update all menu items menu_order fields" do ordered_arr = Array.new(5) ordered_arr.each_with_index do |item, index| item = FactoryGirl.create(:spud_menu_item,:parent_id => @menu.id, :menu_order => index) ordered_arr[index] = item.id.to_s end new_order_arr = ordered_arr.shuffle put :update_sort, :menu_id => @menu.id,:id => ordered_arr[0], :order => new_order_arr new_order_arr.each_with_index do |item, index| expect(@menu.spud_menu_items.find_by_id(item.to_i).menu_order).to be(index) end end end end