|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require "rails_helper" |
| 4 | +require "faker" |
2 | 5 |
|
3 | 6 | RSpec.describe "judges/new", type: :system do |
4 | 7 | let(:organization) { create(:casa_org) } |
5 | 8 | let(:admin) { create(:casa_admin, casa_org_id: organization.id) } |
| 9 | + let(:active_name) { Faker::Name.unique.name } |
| 10 | + let(:inactive_name) { Faker::Name.unique.name } |
6 | 11 |
|
7 | 12 | before do |
8 | 13 | sign_in admin |
9 | | - |
10 | 14 | visit new_judge_path |
11 | 15 | end |
12 | 16 |
|
13 | | - it "adds new judge" do |
14 | | - fill_in "Name", with: "" |
15 | | - click_on "Submit" |
| 17 | + # rubocop:disable RSpec/ExampleLength |
| 18 | + it "creates an active judge with valid name", :aggregate_failures do |
| 19 | + submit_judge_form(name: active_name, active: true) |
| 20 | + expect(page).to have_text("Judge was successfully created.") |
| 21 | + expect(page).to have_text(active_name) |
16 | 22 |
|
17 | | - expect(page).to have_text("Name can't be blank") |
| 23 | + judge = Judge.find_by(name: active_name) |
| 24 | + expect(judge).not_to be_nil |
| 25 | + expect(judge.active).to be true |
| 26 | + end |
| 27 | + # rubocop:enable RSpec/ExampleLength |
18 | 28 |
|
19 | | - fill_in "Name", with: "Joey Shmoey" |
20 | | - click_on "Submit" |
| 29 | + # rubocop:disable RSpec/ExampleLength |
| 30 | + it "creates an inactive judge with valid name", :aggregate_failures do |
| 31 | + submit_judge_form(name: inactive_name, active: false) |
| 32 | + expect(page).to have_text("Judge was successfully created.") |
| 33 | + expect(page).to have_text(inactive_name) |
| 34 | + |
| 35 | + judge = Judge.find_by(name: inactive_name) |
| 36 | + expect(judge).not_to be_nil |
| 37 | + expect(judge.active).to be false |
| 38 | + end |
| 39 | + # rubocop:enable RSpec/ExampleLength |
21 | 40 |
|
| 41 | + # rubocop:disable RSpec/ExampleLength |
| 42 | + it "creates a judge with a very long name", :aggregate_failures do |
| 43 | + long_name = Faker::Lorem.characters(number: 255) |
| 44 | + submit_judge_form(name: long_name) |
22 | 45 | expect(page).to have_text("Judge was successfully created.") |
23 | | - expect(page).to have_text("Joey Shmoey") |
| 46 | + expect(page).to have_text(long_name) |
| 47 | + |
| 48 | + judge = Judge.find_by(name: long_name) |
| 49 | + expect(judge).not_to be_nil |
| 50 | + end |
| 51 | + # rubocop:enable RSpec/ExampleLength |
| 52 | + |
| 53 | + # rubocop:disable RSpec/ExampleLength |
| 54 | + it "creates a judge with special characters in the name", :aggregate_failures do |
| 55 | + special_name = "#{Faker::Lorem.characters(number: 30, min_alpha: 10, min_numeric: 5)}!@#$%^&*()" |
| 56 | + submit_judge_form(name: special_name) |
| 57 | + expect(page).to have_text("Judge was successfully created.") |
| 58 | + expect(page).to have_text(special_name) |
| 59 | + |
| 60 | + judge = Judge.find_by(name: special_name) |
| 61 | + expect(judge).not_to be_nil |
| 62 | + end |
| 63 | + # rubocop:enable RSpec/ExampleLength |
| 64 | + |
| 65 | + context "when validations fail" do |
| 66 | + it "shows validation error when name is blank" do |
| 67 | + submit_judge_form(name: "") |
| 68 | + expect(page).to have_text("Name can't be blank") |
| 69 | + end |
| 70 | + |
| 71 | + it "does not allow duplicate judge names in the same organization", :aggregate_failures do |
| 72 | + duplicate_name = Faker::Name.unique.name |
| 73 | + create(:judge, name: duplicate_name, casa_org: organization) |
| 74 | + submit_judge_form(name: duplicate_name) |
| 75 | + expect(page).to have_text("Name has already been taken") |
| 76 | + expect(Judge.where(name: duplicate_name, casa_org: organization).count).to eq 1 |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + private |
| 81 | + |
| 82 | + def submit_judge_form(name:, active: true) |
| 83 | + fill_in "Name", with: name |
| 84 | + active ? check("Active?") : uncheck("Active?") |
| 85 | + click_on "Submit" |
24 | 86 | end |
25 | 87 | end |
0 commit comments