diff --git a/spec/factories.rb b/spec/factories.rb index 24c0767d4..15599f30f 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -435,6 +435,11 @@ FactoryGirl.define do starts_at { 1.month.ago } ends_at { 1.month.from_now } + trait :current do + starts_at { 2.days.ago } + ends_at { 2.days.from_now } + end + trait :incoming do starts_at { 2.days.from_now } ends_at { 1.month.from_now } @@ -478,6 +483,12 @@ FactoryGirl.define do end end + factory :poll_shift, class: 'Poll::Shift' do + association :booth, factory: :poll_booth + association :officer, factory: :poll_officer + date Date.current + end + factory :poll_final_recount, class: 'Poll::FinalRecount' do association :officer_assignment, factory: [:poll_officer_assignment, :final] association :booth_assignment, factory: :poll_booth_assignment diff --git a/spec/features/admin/poll/shifts_spec.rb b/spec/features/admin/poll/shifts_spec.rb new file mode 100644 index 000000000..fd9b9f4ca --- /dev/null +++ b/spec/features/admin/poll/shifts_spec.rb @@ -0,0 +1,88 @@ +require 'rails_helper' + +feature 'Admin shifts' do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + scenario "Show" do + poll = create(:poll) + officer = create(:poll_officer) + + booth1 = create(:poll_booth) + booth2 = create(:poll_booth) + + shift1 = create(:poll_shift, officer: officer, booth: booth1, date: Date.today) + shift2 = create(:poll_shift, officer: officer, booth: booth2, date: Date.tomorrow) + + visit new_admin_booth_shift_path(booth1) + + expect(page).to have_css(".shift", count: 1) + expect(page).to have_content I18n.l(Date.today, format: :long) + expect(page).to have_content officer.name + + visit new_admin_booth_shift_path(booth2) + + expect(page).to have_css(".shift", count: 1) + expect(page).to have_content I18n.l(Date.tomorrow, format: :long) + expect(page).to have_content officer.name + end + + scenario "Create" do + poll = create(:poll) + booth = create(:poll_booth) + officer = create(:poll_officer) + + visit admin_booths_path + + within("#booth_#{booth.id}") do + click_link "Manage shifts" + end + + select I18n.l(poll.starts_at.to_date, format: :long), from: 'shift_date' + select officer.name, from: 'shift_officer_id' + click_button "Add shift" + + expect(page).to have_content "Shift added" + + within("#shifts") do + expect(page).to have_css(".shift", count: 1) + expect(page).to have_content(I18n.l(poll.starts_at.to_date, format: :long)) + expect(page).to have_content(officer.name) + end + end + + scenario "Destroy" do + poll = create(:poll) + booth = create(:poll_booth) + officer = create(:poll_officer) + + shift = create(:poll_shift, officer: officer, booth: booth) + + visit admin_booths_path + + within("#booth_#{booth.id}") do + click_link "Manage shifts" + end + + expect(page).to have_css(".shift", count: 1) + within("#shift_#{shift.id}") do + click_link "Remove" + end + + expect(page).to have_content "Shift removed" + expect(page).to have_css(".shift", count: 0) + end + + scenario "Empty" do + poll = create(:poll) + booth = create(:poll_booth) + + visit new_admin_booth_shift_path(booth) + + expect(page).to have_content "This booth has no shifts" + end + +end diff --git a/spec/models/poll/poll_spec.rb b/spec/models/poll/poll_spec.rb index 61f9fce52..2eb78841b 100644 --- a/spec/models/poll/poll_spec.rb +++ b/spec/models/poll/poll_spec.rb @@ -62,6 +62,20 @@ describe :poll do end end + describe "#current_or_incoming" do + it "returns current or incoming polls" do + current = create(:poll, :current) + incoming = create(:poll, :incoming) + expired = create(:poll, :expired) + + current_or_incoming = Poll.current_or_incoming + + expect(current_or_incoming).to include(current) + expect(current_or_incoming).to include(incoming) + expect(current_or_incoming).to_not include(expired) + end + end + describe "#document_has_voted?" do it "returns true if Poll::Voter with document exists" do poll = create(:poll) diff --git a/spec/models/poll/shift_spec.rb b/spec/models/poll/shift_spec.rb new file mode 100644 index 000000000..e70b5f9a0 --- /dev/null +++ b/spec/models/poll/shift_spec.rb @@ -0,0 +1,61 @@ +require 'rails_helper' + +describe :shift do + let(:shift) { build(:poll_shift) } + + describe "validations" do + + it "should be valid" do + expect(shift).to be_valid + end + + it "should not be valid without a booth" do + shift.booth = nil + expect(shift).to_not be_valid + end + + it "should not be valid without an officer" do + shift.officer = nil + expect(shift).to_not be_valid + end + + it "should not be valid without a date" do + shift.date = nil + expect(shift).to_not be_valid + end + + end + + describe "officer_assignments" do + + it "should create corresponding officer_assignments" do + poll1 = create(:poll) + poll2 = create(:poll) + poll3 = create(:poll) + + booth = create(:poll_booth) + officer = create(:poll_officer) + + booth_assignment1 = create(:poll_booth_assignment, poll: poll1, booth: booth) + booth_assignment2 = create(:poll_booth_assignment, poll: poll2, booth: booth) + + shift = create(:poll_shift, booth: booth, officer: officer, date: Date.current) + + officer_assignments = Poll::OfficerAssignment.all + expect(officer_assignments.count).to eq(2) + + oa1 = officer_assignments.first + oa2 = officer_assignments.second + + expect(oa1.officer).to eq(officer) + expect(oa1.date).to eq(Date.current) + expect(oa1.booth_assignment).to eq(booth_assignment1) + + expect(oa2.officer).to eq(officer) + expect(oa2.date).to eq(Date.current) + expect(oa2.booth_assignment).to eq(booth_assignment2) + end + + end + +end