spec/controllers/fe/admin/elements_controller_spec.rb in fe-1.0.0 vs spec/controllers/fe/admin/elements_controller_spec.rb in fe-2.0.0

- old
+ new

@@ -8,59 +8,69 @@ question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element) create(:page_element, element: element, page: page) - xhr :get, :edit, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + get :edit, params: {question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(assigns(:element)).to eq(element) end end context '#new' do it 'should work' do answer_sheet = create(:answer_sheet) page = create(:page) question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) - element = create(:text_field_element, style: 'style') - create(:page_element, element: element, page: page) + element1 = create(:text_field_element, style: 'style', share: true) + element2 = create(:text_field_element, style: 'style', share: false) + create(:page_element, element: element1, page: page) + create(:page_element, element: element2, page: page) - xhr :get, :new, element_type: 'Fe::TextField', element: { style: 'style' }, question_sheet_id: question_sheet.id, page_id: page.id - expect(assigns(:questions)).to eq([element]) + get :new, params: {element_type: 'Fe::TextField', element: { style: 'style' }, question_sheet_id: question_sheet.id, page_id: page.id}, xhr: true + expect(assigns(:questions)).to eq([element1]) end end context '#use_existing' do it 'should work' do answer_sheet = create(:answer_sheet) page = create(:page) question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element, style: 'style') - xhr :get, :use_existing, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + get :use_existing, params: {question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(assigns(:page_element)).to_not be_nil + expect(assigns(:page_element)).to_not be_nil + expect(assigns(:page)).to eq(page) + expect(assigns(:page_element).element).to eq(element) end - it 'should not put the same question on a questionnaire twice' do + end + context '#copy_existing' do + it 'should work' do answer_sheet = create(:answer_sheet) page = create(:page) question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) - element = create(:text_field_element, style: 'style') - create(:page_element, element: element, page: page) + element = create(:text_field_element, style: 'style', share: true) - xhr :get, :use_existing, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id - expect(assigns(:page_element)).to be_nil + get :copy_existing, params: {question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true + expect(assigns(:page_element)).to_not be_nil + expect(assigns(:page_element)).to_not be_nil + expect(assigns(:page)).to eq(page) + expect(assigns(:page_element).element).to_not eq(element) + expect(assigns(:page_element).element.share).to be false end end context '#create' do it 'should work' do answer_sheet = create(:answer_sheet) page = create(:page) question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) expect { - xhr :post, :create, element_type: 'Fe::TextField', element: { style: 'style' }, question_sheet_id: question_sheet.id, page_id: page.id + post :create, params: {element_type: 'Fe::TextField', element: { style: 'style' }, question_sheet_id: question_sheet.id, page_id: page.id}, xhr: true }.to change{Fe::Element.count}.by(1) expect(assigns(:page_element)).to_not be_nil new_element = Fe::Element.last expect(assigns(:page_element).element).to eq(new_element) @@ -71,11 +81,11 @@ page = create(:page) question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) expect { - xhr :post, :create, element_type: 'Fe::TextField', element: { slug: "Illegal Chars: #@$!" }, question_sheet_id: question_sheet.id, page_id: page.id + post :create, params: {element_type: 'Fe::TextField', element: { slug: "Illegal Chars: #@$!" }, question_sheet_id: question_sheet.id, page_id: page.id}, xhr: true }.to change{Fe::Element.count}.by(0) expect(assigns(:page_element)).to be_nil expect(response).to render_template('error.js.erb') end @@ -87,11 +97,11 @@ question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element, style: 'style') create(:page_element, element: element, page: page) - xhr :put, :update, element: { style: 'style' }, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + put :update, params: {element: { style: 'style' }, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(assigns(:element)).to eq(element) expect(assigns(:element).style).to eq('style') end it 'should handle error saving element' do @@ -100,11 +110,11 @@ question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element, style: 'style') create(:page_element, element: element, page: page) - xhr :put, :update, element: { slug: "Illegal Chars: #@$!" }, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + put :update, params: {element: { slug: "Illegal Chars: #@$!" }, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(assigns(:element)).to eq(element) expect(response).to render_template('error.js.erb') end end context '#destroy' do @@ -114,11 +124,11 @@ question_sheet = page.question_sheet create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element, style: 'style') create(:page_element, element: element, page: page) - xhr :delete, :destroy, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + delete :destroy, params: {question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(Fe::PageElement.find_by(page_id: page.id, element_id: element.id)).to be_nil expect(Fe::Element.find_by(id: element.id)).to be_nil end it 'should not destroy the element when it is not used in any other pages, but it has answers' do @@ -128,11 +138,11 @@ create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element, style: 'style') create(:page_element, element: element, page: page) create(:answer, question: element, value: 'answer here', answer_sheet: answer_sheet) - xhr :delete, :destroy, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + delete :destroy, params: {question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(Fe::PageElement.find_by(page_id: page.id, element_id: element.id)).to be_nil expect(Fe::Element.find_by(id: element.id)).to_not be_nil end it 'should not destroy the element when it has no answers, but is being used in another page' do @@ -143,11 +153,11 @@ create(:answer_sheet_question_sheet, answer_sheet: answer_sheet, question_sheet: question_sheet) element = create(:text_field_element, style: 'style') create(:page_element, element: element, page: page) create(:page_element, element: element, page: page2) - xhr :delete, :destroy, question_sheet_id: question_sheet.id, page_id: page.id, id: element.id + delete :destroy, params: {question_sheet_id: question_sheet.id, page_id: page.id, id: element.id}, xhr: true expect(Fe::PageElement.find_by(page_id: page.id, element_id: element.id)).to be_nil expect(Fe::Element.find_by(id: element.id)).to_not be_nil end end @@ -167,11 +177,11 @@ create(:page_element, element: element2, page: page) create(:page_element, element: element3, page: page) page_element_positions_before = Fe::PageElement.all.pluck(:position) - xhr :post, :reorder, question_sheet_id: question_sheet.id, page_id: page.id, "questions_list_#{element3.id}" => [ element5.id, element4.id ] + post :reorder, params: {question_sheet_id: question_sheet.id, page_id: page.id, "questions_list_#{element3.id}" => [ element5.id, element4.id ]}, xhr: true # it shouldn't touch the page elements expect(Fe::PageElement.all.pluck(:position)).to eq(page_element_positions_before) # it should put a new order on the question grid elements expect(element5.reload.position).to eq(1) expect(element4.reload.position).to eq(2) @@ -190,10 +200,10 @@ pe2 = create(:page_element, element: element2, page: page) pe3 = create(:page_element, element: element3, page: page) question_grid_elements_positions_before = element3.reload.elements.pluck(:position) - xhr :post, :reorder, question_sheet_id: question_sheet.id, page_id: page.id, "questions_list" => [ element3.id, element.id, element2.id ] + post :reorder, params: {question_sheet_id: question_sheet.id, page_id: page.id, "questions_list" => [ element3.id, element.id, element2.id ]}, xhr: true # it shouldn't touch the question grid positions expect(element3.reload.elements.pluck(:position)).to eq(question_grid_elements_positions_before) # it should put a new order on the page elements expect(pe3.reload.position).to eq(1) expect(pe1.reload.position).to eq(2)