|
12 | 12 |
|
13 | 13 | it_behaves_like "a question model" |
14 | 14 |
|
| 15 | + shared_examples "title length validation" do |
| 16 | + it "is valid when the title is less than 100 characters" do |
| 17 | + question.title = "a" * 99 |
| 18 | + expect(question).to be_valid |
| 19 | + expect(question.errors).to be_empty |
| 20 | + end |
| 21 | + |
| 22 | + it "is invalid when the title is more than 99 characters" do |
| 23 | + question.title = "a" * 100 |
| 24 | + expect(question).not_to be_valid |
| 25 | + expect(question.errors[:title]).to include(I18n.t("activemodel.errors.models.question/name.attributes.title.too_long")) |
| 26 | + end |
| 27 | + end |
| 28 | + |
15 | 29 | context "when the name question is in full name format" do |
16 | 30 | context "when the answer is empty" do |
17 | 31 | it "returns invalid with blank full_name field" do |
|
58 | 72 | expect(question.show_answer_in_csv).to eq({ "#{question_text} - Full name" => name }) |
59 | 73 | end |
60 | 74 | end |
| 75 | + |
| 76 | + describe "length validation" do |
| 77 | + it "is valid with length under 500 characters" do |
| 78 | + question.full_name = "a" * 499 |
| 79 | + expect(question).to be_valid |
| 80 | + expect(question.errors[:full_name]).to be_empty |
| 81 | + end |
| 82 | + |
| 83 | + it "is invalid with length over 499 characters" do |
| 84 | + question.full_name = "a" * 500 |
| 85 | + expect(question).not_to be_valid |
| 86 | + expect(question.errors[:full_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.full_name.too_long")) |
| 87 | + end |
| 88 | + end |
61 | 89 | end |
62 | 90 |
|
63 | 91 | context "when the name question is in first and last name format" do |
|
147 | 175 | }) |
148 | 176 | end |
149 | 177 | end |
| 178 | + |
| 179 | + describe "length validation" do |
| 180 | + before do |
| 181 | + question.first_name = "a" * 499 |
| 182 | + question.last_name = "a" * 499 |
| 183 | + end |
| 184 | + |
| 185 | + it "is valid when all fields have the maximum allowed length" do |
| 186 | + expect(question).to be_valid |
| 187 | + expect(question.errors).to be_empty |
| 188 | + end |
| 189 | + |
| 190 | + it "is invalid when the first name is over 499 characters" do |
| 191 | + question.first_name = "a" * 500 |
| 192 | + expect(question).not_to be_valid |
| 193 | + expect(question.errors[:first_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.first_name.too_long")) |
| 194 | + end |
| 195 | + |
| 196 | + it "is invalid when the last name is over 499 characters" do |
| 197 | + question.last_name = "a" * 500 |
| 198 | + expect(question).not_to be_valid |
| 199 | + expect(question.errors[:last_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.last_name.too_long")) |
| 200 | + end |
| 201 | + end |
150 | 202 | end |
151 | 203 |
|
152 | 204 | context "when the name question is in first, middle and last name format" do |
|
215 | 267 | }) |
216 | 268 | end |
217 | 269 | end |
| 270 | + |
| 271 | + describe "length validation" do |
| 272 | + before do |
| 273 | + question.first_name = "a" * 499 |
| 274 | + question.middle_names = "a" * 499 |
| 275 | + question.last_name = "a" * 499 |
| 276 | + end |
| 277 | + |
| 278 | + it "is valid when all fields have the maximum allowed length" do |
| 279 | + expect(question).to be_valid |
| 280 | + expect(question.errors).to be_empty |
| 281 | + end |
| 282 | + |
| 283 | + it "is invalid when the first name is over 499 characters" do |
| 284 | + question.first_name = "a" * 500 |
| 285 | + expect(question).not_to be_valid |
| 286 | + expect(question.errors[:first_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.first_name.too_long")) |
| 287 | + end |
| 288 | + |
| 289 | + it "is invalid when the middle name is over 499 characters" do |
| 290 | + question.middle_names = "a" * 500 |
| 291 | + expect(question).not_to be_valid |
| 292 | + expect(question.errors[:middle_names]).to include(I18n.t("activemodel.errors.models.question/name.attributes.middle_names.too_long")) |
| 293 | + end |
| 294 | + |
| 295 | + it "is invalid when the last name is over 499 characters" do |
| 296 | + question.last_name = "a" * 500 |
| 297 | + expect(question).not_to be_valid |
| 298 | + expect(question.errors[:last_name]).to include(I18n.t("activemodel.errors.models.question/name.attributes.last_name.too_long")) |
| 299 | + end |
| 300 | + end |
218 | 301 | end |
219 | 302 |
|
220 | 303 | context "when title_needed is set to true" do |
|
333 | 416 | "#{question_text} - Title" => title, |
334 | 417 | }) |
335 | 418 | end |
| 419 | + |
| 420 | + include_examples "title length validation" |
336 | 421 | end |
337 | 422 | end |
338 | 423 |
|
|
400 | 485 | "#{question_text} - Title" => title, |
401 | 486 | }) |
402 | 487 | end |
| 488 | + |
| 489 | + include_examples "title length validation" |
403 | 490 | end |
404 | 491 | end |
405 | 492 | end |
|
0 commit comments