|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | describe "Surveys" do |
4 | | - let(:survey) { FactoryGirl.create(:survey, name: "Question Set", introduction: "Some introduction") } |
5 | | - let(:question1) { FactoryGirl.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) } |
6 | | - let(:question2) { FactoryGirl.create(:q_short, survey: survey, question_text: "Short Question") } |
| 4 | + let!(:survey) { FactoryGirl.create(:survey, name: "Question Set", introduction: "Some introduction") } |
| 5 | + let!(:question1) { FactoryGirl.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) } |
| 6 | + let!(:question2) { FactoryGirl.create(:q_short, survey: survey, question_text: "Short Question") } |
| 7 | + let!(:question3) { FactoryGirl.create(:q_checkbox, survey: survey, question_text: "Checkbox question") } |
| 8 | + let!(:question4) { FactoryGirl.create(:q_checkbox, survey: survey, question_text: "Checkbox question", validation_rules: { presence: "1" }) } |
| 9 | + |
7 | 10 | before do |
8 | | - [question1, question2] |
9 | 11 | visit rapidfire.new_survey_attempt_path(survey) |
10 | 12 | end |
11 | 13 |
|
|
18 | 20 | before do |
19 | 21 | fill_in "attempt_#{question1.id}_answer_text", with: "Long Answer" |
20 | 22 | fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer" |
| 23 | + check "attempt_#{question3.id}_answer_text_1" |
| 24 | + check "attempt_#{question4.id}_answer_text_0" |
21 | 25 | click_button "Save" |
22 | 26 | end |
23 | 27 |
|
24 | | - it "persists 2 answers" do |
25 | | - expect(Rapidfire::Answer.count).to eq(2) |
| 28 | + it "persists 4 answers" do |
| 29 | + expect(Rapidfire::Answer.count).to eq(4) |
26 | 30 | end |
27 | 31 |
|
28 | | - it "persists 2 answers with answer values" do |
29 | | - expected_answers = ["Long Answer", "Short Answer"] |
| 32 | + it "persists 4 answers with answer values" do |
| 33 | + expected_answers = ["Long Answer", "Short Answer", "telugu", "hindi"] |
30 | 34 | expect(Rapidfire::Answer.all.map(&:answer_text)).to match(expected_answers) |
31 | 35 | end |
32 | 36 |
|
|
36 | 40 | end |
37 | 41 |
|
38 | 42 | context "when all questions are not answered" do |
39 | | - before do |
40 | | - fill_in "attempt_#{question1.id}_answer_text", with: "" |
41 | | - fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer" |
42 | | - click_button "Save" |
43 | | - end |
| 43 | + context "when validation fails" do |
| 44 | + before do |
| 45 | + fill_in "attempt_#{question1.id}_answer_text", with: "" |
| 46 | + fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer" |
| 47 | + check "attempt_#{question3.id}_answer_text_1" |
| 48 | + click_button "Save" |
| 49 | + end |
44 | 50 |
|
45 | | - it "fails to persits answers" do |
46 | | - expect(Rapidfire::Answer.count).to eq(0) |
47 | | - end |
| 51 | + it "fails to persits answers" do |
| 52 | + expect(Rapidfire::Answer.count).to eq(0) |
| 53 | + end |
| 54 | + |
| 55 | + it "shows error for missing answers" do |
| 56 | + expect(page).to have_content("can't be blank", count: 2) |
| 57 | + end |
48 | 58 |
|
49 | | - it "shows error for missing answers" do |
50 | | - expect(page).to have_content "can't be blank" |
| 59 | + it "shows already populated answers" do |
| 60 | + short_answer = page.find("#attempt_#{question2.id}_answer_text").value |
| 61 | + expect(page).to have_checked_field("attempt_#{question3.id}_answer_text_1") |
| 62 | + expect(short_answer).to have_content "Short Answer" |
| 63 | + end |
51 | 64 | end |
52 | 65 |
|
53 | | - it "shows already populated answers" do |
54 | | - short_answer = page.find("#attempt_#{question2.id}_answer_text").value |
55 | | - expect(short_answer).to have_content "Short Answer" |
| 66 | + context "when validation passes" do |
| 67 | + before do |
| 68 | + fill_in "attempt_#{question1.id}_answer_text", with: "Long Answer" |
| 69 | + check "attempt_#{question4.id}_answer_text_0" |
| 70 | + click_button "Save" |
| 71 | + end |
| 72 | + |
| 73 | + it "persists 4 answers" do |
| 74 | + expect(Rapidfire::Answer.count).to eq(4) |
| 75 | + end |
| 76 | + |
| 77 | + it "persists 4 answers with 2 empty answers" do |
| 78 | + expected_answers = ["Long Answer", "", "", "hindi"] |
| 79 | + expect(Rapidfire::Answer.all.map(&:answer_text)).to match(expected_answers) |
| 80 | + end |
| 81 | + |
| 82 | + it "redirects to question groups path" do |
| 83 | + expect(current_path).to eq(rapidfire.surveys_path) |
| 84 | + end |
56 | 85 | end |
57 | 86 | end |
58 | 87 | end |
|
0 commit comments