From 893693325188736d6ae6fe877aba2bced4c98124 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 17 Aug 2019 14:10:48 +1200 Subject: [PATCH] Shared context for signing in (and then out) in feature specs --- spec/features/admin/admin_spec.rb | 8 +- spec/features/admin/forums_spec.rb | 85 ++-- spec/features/cms_spec.rb | 24 +- .../comments/commenting_a_comment_spec.rb | 47 +- spec/features/crops/alternate_name_spec.rb | 7 +- spec/features/crops/browse_crops_spec.rb | 10 +- spec/features/crops/crop_detail_page_spec.rb | 29 +- spec/features/crops/crop_photos_spec.rb | 33 +- spec/features/crops/crop_wranglers_spec.rb | 129 +++-- .../crops/crop_wrangling_button_spec.rb | 30 +- spec/features/crops/delete_crop_spec.rb | 31 +- spec/features/crops/request_new_crop_spec.rb | 58 +-- spec/features/crops/requested_crops_spec.rb | 14 +- spec/features/crops/scientific_name_spec.rb | 114 ++--- spec/features/gardens/actions_spec.rb | 107 ++-- spec/features/gardens/adding_gardens_spec.rb | 53 +- spec/features/gardens/gardens_spec.rb | 218 ++++---- spec/features/gardens/index_spec.rb | 104 ++-- .../features/harvests/browse_harvests_spec.rb | 35 +- .../harvests/harvesting_a_crop_spec.rb | 212 ++++---- spec/features/home/home_spec.rb | 18 +- spec/features/likeable_spec.rb | 4 +- spec/features/members/ban_spec.rb | 21 +- spec/features/members/following_spec.rb | 99 ++-- spec/features/members/profile_spec.rb | 28 +- spec/features/notifications_spec.rb | 95 ++-- spec/features/percy/percy_spec.rb | 254 ++++----- spec/features/photos/new_photo_spec.rb | 74 +-- spec/features/photos/show_photo_spec.rb | 94 ++-- .../features/places/searching_a_place_spec.rb | 45 +- .../plantings/planting_a_crop_spec.rb | 481 +++++++++--------- spec/features/posts/posting_a_post_spec.rb | 45 +- spec/features/seeds/adding_seeds_spec.rb | 116 +++-- spec/features/seeds/misc_seeds_spec.rb | 124 ++--- spec/features/seeds/seed_photos.rb | 65 ++- spec/support/devise.rb | 1 + spec/support/feature_helpers.rb | 19 +- 37 files changed, 1443 insertions(+), 1488 deletions(-) diff --git a/spec/features/admin/admin_spec.rb b/spec/features/admin/admin_spec.rb index 6ee586ee7..57c8953b1 100644 --- a/spec/features/admin/admin_spec.rb +++ b/spec/features/admin/admin_spec.rb @@ -1,13 +1,7 @@ require 'rails_helper' describe "forums", js: true do - context "as an admin user" do - let(:member) { create :admin_member } - - before do - login_as member - end - + include_context 'signed in admin' do it "navigating to forum admin with js" do visit admin_path Percy.snapshot(page, name: 'Admin page') diff --git a/spec/features/admin/forums_spec.rb b/spec/features/admin/forums_spec.rb index d13cc13fb..d22ace435 100644 --- a/spec/features/admin/forums_spec.rb +++ b/spec/features/admin/forums_spec.rb @@ -1,52 +1,55 @@ require 'rails_helper' describe "forums", js: true do - context "as an admin user" do - let(:member) { create :admin_member } - let(:forum) { create :forum } + include_context 'signed in admin' do + let(:forum) { create :forum } - before do - login_as member - end - - it "navigating to forum admin with js" do - visit admin_path - within 'ul#site_admin' do - click_link "Forums" + describe "navigating to forum admin with js" do + before do + visit admin_path + within 'ul#site_admin' do + click_link "Forums" + end end - expect(current_path).to eq forums_path - expect(page).to have_link "New forum" + it { expect(current_path).to eq forums_path } + it { expect(page).to have_link "New forum" } end - it "adding a forum" do - visit forums_path - click_link "New forum" - expect(current_path).to eq new_forum_path - fill_in 'Name', with: 'Discussion' - fill_in 'Description', with: "this is a new forum" - click_button 'Save' - expect(current_path).to eq forum_path(Forum.last) - expect(page).to have_content 'Forum was successfully created' - end - - it 'editing forum' do - visit forum_path forum - click_link 'Edit' - fill_in 'Name', with: 'Something else' - click_button 'Save' - forum.reload - expect(current_path).to eq forum_path(forum) - expect(page).to have_content 'Forum was successfully updated' - expect(page).to have_content 'Something else' - end - - it 'deleting forum' do - visit forum_path forum - accept_confirm do - click_link 'Delete' + describe "adding a forum" do + before do + visit forums_path + click_link "New forum" + expect(current_path).to eq new_forum_path + fill_in 'Name', with: 'Discussion' + fill_in 'Description', with: "this is a new forum" + click_button 'Save' end - expect(current_path).to eq forums_path - expect(page).to have_content 'Forum was successfully deleted' + it { expect(current_path).to eq forum_path(Forum.last) } + it { expect(page).to have_content 'Forum was successfully created' } + end + + describe 'editing forum' do + before do + visit forum_path forum + click_link 'Edit' + fill_in 'Name', with: 'Something else' + click_button 'Save' + forum.reload + end + it { expect(current_path).to eq forum_path(forum) } + it { expect(page).to have_content 'Forum was successfully updated' } + it { expect(page).to have_content 'Something else' } + end + + describe 'deleting forum' do + before do + visit forum_path forum + accept_confirm do + click_link 'Delete' + end + end + it { expect(current_path).to eq forums_path } + it { expect(page).to have_content 'Forum was successfully deleted' } end end end diff --git a/spec/features/cms_spec.rb b/spec/features/cms_spec.rb index 4abe7d6d4..d07f3c935 100644 --- a/spec/features/cms_spec.rb +++ b/spec/features/cms_spec.rb @@ -1,26 +1,24 @@ require 'rails_helper' describe "cms admin" do - let(:member) { create :member } - let(:admin_member) { create :admin_member } - it "can't view CMS admin if not signed in" do visit comfy_admin_cms_path expect(current_path).to eq root_path expect(page).to have_content "Please sign in as an admin user" end - it "can't view CMS admin if not an admin member" do - # sign in as an ordinary member - login_as member - visit comfy_admin_cms_path - expect(current_path).to eq root_path - expect(page).to have_content "Please sign in as an admin user" + include_context 'signed in member' do + it "can't view CMS admin if not an admin member" do + visit comfy_admin_cms_path + expect(current_path).to eq root_path + expect(page).to have_content "Please sign in as an admin user" + end end - it "admin members can view CMS admin area" do - login_as admin_member - visit comfy_admin_cms_path - expect(current_path).to match(/#{comfy_admin_cms_path}/) # match any CMS admin page + include_context 'signed in admin' do + it "admin members can view CMS admin area" do + visit comfy_admin_cms_path + expect(current_path).to match(/#{comfy_admin_cms_path}/) # match any CMS admin page + end end end diff --git a/spec/features/comments/commenting_a_comment_spec.rb b/spec/features/comments/commenting_a_comment_spec.rb index f2136e98e..34907080b 100644 --- a/spec/features/comments/commenting_a_comment_spec.rb +++ b/spec/features/comments/commenting_a_comment_spec.rb @@ -1,34 +1,33 @@ require 'rails_helper' describe 'Commenting on a post' do - let(:member) { create :member } - let(:post) { create :post, author: member } + include_context 'signed in member' do + let(:member) { create :member } + let(:post) { create :post, author: member } - before do - login_as member - visit new_comment_path post_id: post.id - end + before { visit new_comment_path post_id: post.id } - it "creating a comment" do - fill_in "comment_body", with: "This is a sample test for comment" - click_button "Post comment" - expect(page).to have_content "comment was successfully created." - expect(page).to have_content "Posted by" - Percy.snapshot(page, name: 'Posting a comment') - end - - context "editing a comment" do - let(:existing_comment) { create :comment, post: post, author: member } - - before do - visit edit_comment_path existing_comment + it "creating a comment" do + fill_in "comment_body", with: "This is a sample test for comment" + click_button "Post comment" + expect(page).to have_content "comment was successfully created." + expect(page).to have_content "Posted by" + Percy.snapshot(page, name: 'Posting a comment') end - it "saving edit" do - fill_in "comment_body", with: "Testing edit for comment" - click_button "Post comment" - expect(page).to have_content "comment was successfully updated." - expect(page).to have_content "edited at" + context "editing a comment" do + let(:existing_comment) { create :comment, post: post, author: member } + + before do + visit edit_comment_path existing_comment + end + + it "saving edit" do + fill_in "comment_body", with: "Testing edit for comment" + click_button "Post comment" + expect(page).to have_content "comment was successfully updated." + expect(page).to have_content "edited at" + end end end end diff --git a/spec/features/crops/alternate_name_spec.rb b/spec/features/crops/alternate_name_spec.rb index 903b6d3a7..acd79bb36 100644 --- a/spec/features/crops/alternate_name_spec.rb +++ b/spec/features/crops/alternate_name_spec.rb @@ -15,13 +15,8 @@ describe "Alternate names", js: true do expect(page).to have_content alternate_eggplant.name end - context "User is a crop wrangler" do + include_context 'signed in crop wrangler' do let!(:crop_wranglers) { create_list :crop_wrangling_member, 3 } - let(:member) { crop_wranglers.first } - - before do - login_as member - end it "Crop wranglers can edit alternate names" do visit crop_path(crop) diff --git a/spec/features/crops/browse_crops_spec.rb b/spec/features/crops/browse_crops_spec.rb index 821ae01c4..9979cfb8e 100644 --- a/spec/features/crops/browse_crops_spec.rb +++ b/spec/features/crops/browse_crops_spec.rb @@ -12,9 +12,8 @@ describe "browse crops" do end it "shows a list of crops" do - crop1 = tomato visit crops_path - expect(page).to have_content crop1.name + expect(page).to have_content tomato.name end it "pending crops are not listed" do @@ -27,11 +26,8 @@ describe "browse crops" do expect(page).not_to have_content rejected_crop.name end - context "logged in and crop wrangler" do - before do - login_as FactoryBot.create(:crop_wrangling_member) - visit crops_path - end + include_context 'signed in crop wrangler' do + before { visit crops_path } it "shows a new crop link" do expect(page).to have_link "Add New Crop" diff --git a/spec/features/crops/crop_detail_page_spec.rb b/spec/features/crops/crop_detail_page_spec.rb index 2e1fec723..3da5f8828 100644 --- a/spec/features/crops/crop_detail_page_spec.rb +++ b/spec/features/crops/crop_detail_page_spec.rb @@ -20,13 +20,7 @@ describe "crop detail page", js: true do end end - context "signed in member" do - let(:member) { create :member } - - before do - login_as(member) - end - + include_context "signed in member" do context "action buttons" do before { subject } @@ -86,17 +80,16 @@ describe "crop detail page", js: true do expect(page).not_to have_content "You have 20 seeds" end - it "User signed in" do - login_as(member) - visit crop_path(seed.crop) - expect(page).to have_link "You have 20 seeds of this crop." - end - - it "click link to your owned seeds" do - login_as(member) - visit crop_path(seed.crop) - click_link "You have 20 seeds of this crop." - expect(current_path).to eq member_seeds_path(member_slug: member.slug) + include_context 'signed in member' do + it "User signed in" do + visit crop_path(seed.crop) + expect(page).to have_link "You have 20 seeds of this crop." + end + it "click link to your owned seeds" do + visit crop_path(seed.crop) + click_link "You have 20 seeds of this crop." + expect(current_path).to eq member_seeds_path(member_slug: member.slug) + end end end diff --git a/spec/features/crops/crop_photos_spec.rb b/spec/features/crops/crop_photos_spec.rb index e66d645d3..39654a6bd 100644 --- a/spec/features/crops/crop_photos_spec.rb +++ b/spec/features/crops/crop_photos_spec.rb @@ -3,20 +3,20 @@ require 'rails_helper' describe "crop detail page", js: true do subject { page } - let!(:member) { FactoryBot.create :member } + let!(:owner_member) { FactoryBot.create :member } let!(:crop) { FactoryBot.create :crop } - let!(:planting) { FactoryBot.create :planting, crop: crop, owner: member } - let!(:harvest) { FactoryBot.create :harvest, crop: crop, owner: member } - let!(:seed) { FactoryBot.create :seed, crop: crop, owner: member } + let!(:planting) { FactoryBot.create :planting, crop: crop, owner: owner_member } + let!(:harvest) { FactoryBot.create :harvest, crop: crop, owner: owner_member } + let!(:seed) { FactoryBot.create :seed, crop: crop, owner: owner_member } - let!(:photo1) { FactoryBot.create(:photo, owner: member) } - let!(:photo2) { FactoryBot.create(:photo, owner: member) } - let!(:photo3) { FactoryBot.create(:photo, owner: member) } - let!(:photo4) { FactoryBot.create(:photo, owner: member) } - let!(:photo5) { FactoryBot.create(:photo, owner: member) } - let!(:photo6) { FactoryBot.create(:photo, owner: member) } + let!(:photo1) { FactoryBot.create(:photo, owner: owner_member) } + let!(:photo2) { FactoryBot.create(:photo, owner: owner_member) } + let!(:photo3) { FactoryBot.create(:photo, owner: owner_member) } + let!(:photo4) { FactoryBot.create(:photo, owner: owner_member) } + let!(:photo5) { FactoryBot.create(:photo, owner: owner_member) } + let!(:photo6) { FactoryBot.create(:photo, owner: owner_member) } before do planting.photos << photo1 @@ -50,15 +50,16 @@ describe "crop detail page", js: true do end context "when signed in" do - before { login_as(FactoryBot.create(:member)) } - - include_examples "shows photos" + include_context 'signed in member' do + include_examples "shows photos" + end end context "when signed in as photos owner" do - before { login_as(member) } - - include_examples "shows photos" + include_context 'signed in member' do + let(:member) { owner_member } + include_examples "shows photos" + end end context "when not signed in " do diff --git a/spec/features/crops/crop_wranglers_spec.rb b/spec/features/crops/crop_wranglers_spec.rb index 77571c0d4..3f52757ce 100644 --- a/spec/features/crops/crop_wranglers_spec.rb +++ b/spec/features/crops/crop_wranglers_spec.rb @@ -2,87 +2,86 @@ require 'rails_helper' describe "crop wranglers", js: true do context "signed in wrangler" do - let!(:crop_wranglers) { create_list :crop_wrangling_member, 3 } - let(:wrangler) { crop_wranglers.first } - let!(:crops) { create_list :crop, 2 } - let!(:requested_crop) { create :crop_request } - let!(:rejected_crop) { create :rejected_crop } + include_context 'signed in crop wrangler' do + let!(:crop_wranglers) { create_list :crop_wrangling_member, 3 } + let!(:crops) { create_list :crop, 2 } + let!(:requested_crop) { create :crop_request } + let!(:rejected_crop) { create :rejected_crop } - before { login_as wrangler } + it "sees crop wranglers listed on the crop wrangler page" do + visit root_path + click_link 'Admin' + click_link 'Crop Wrangling' - it "sees crop wranglers listed on the crop wrangler page" do - visit root_path - click_link 'Admin' - click_link 'Crop Wrangling' - - within '.crop_wranglers' do - expect(page).to have_content 'Crop Wranglers:' - crop_wranglers.each do |crop_wrangler| - expect(page).to have_link crop_wrangler.login_name, href: member_path(crop_wrangler) + within '.crop_wranglers' do + expect(page).to have_content 'Crop Wranglers:' + crop_wranglers.each do |crop_wrangler| + expect(page).to have_link crop_wrangler.login_name, href: member_path(crop_wrangler) + end end end - end - it "can see list of crops with extra detail of who created a crop" do - visit root_path - click_link 'Admin' - click_link 'Crop Wrangling' - within '#recently-added-crops' do - expect(page).to have_content crops.first.creator.login_name.to_s - end - end - - describe "visiting a crop can see wrangler links" do - before do - visit crop_path(crops.first) - click_link 'Actions' + it "can see list of crops with extra detail of who created a crop" do + visit root_path + click_link 'Admin' + click_link 'Crop Wrangling' + within '#recently-added-crops' do + expect(page).to have_content crops.first.creator.login_name.to_s + end end - it { expect(page).to have_content 'You are a CROP WRANGLER' } - it { expect(page).to have_link 'Edit' } - it { expect(page).to have_link 'Delete' } - end + describe "visiting a crop can see wrangler links" do + before do + visit crop_path(crops.first) + click_link 'Actions' + end - it "can create a new crop" do - visit root_path - click_link 'Admin' - click_link 'Crop Wrangling' - click_link 'Add Crop' - fill_in 'Name', with: "aubergine" - fill_in 'en_wikipedia_url', with: "http://en.wikipedia.org/wiki/Maize" - fill_in 'sci_name[1]', with: "planticus maximus" - click_on 'Save' - expect(page).to have_content 'crop was successfully created.' - expect(page).to have_content 'planticus maximus' - end + it { expect(page).to have_content 'You are a CROP WRANGLER' } + it { expect(page).to have_link 'Edit' } + it { expect(page).to have_link 'Delete' } + end - it "View pending crops" do - visit crop_path(requested_crop) - expect(page).to have_content "This crop is currently pending approval." - expect(page).to have_content "Please approve this even though it's fake." - end + it "can create a new crop" do + visit root_path + click_link 'Admin' + click_link 'Crop Wrangling' + click_link 'Add Crop' + fill_in 'Name', with: "aubergine" + fill_in 'en_wikipedia_url', with: "http://en.wikipedia.org/wiki/Maize" + fill_in 'sci_name[1]', with: "planticus maximus" + click_on 'Save' + expect(page).to have_content 'crop was successfully created.' + expect(page).to have_content 'planticus maximus' + end - it "View rejected crops" do - visit crop_path(rejected_crop) - expect(page).to have_content "This crop was rejected for the following reason: Totally fake" + it "View pending crops" do + visit crop_path(requested_crop) + expect(page).to have_content "This crop is currently pending approval." + expect(page).to have_content "Please approve this even though it's fake." + end + + it "View rejected crops" do + visit crop_path(rejected_crop) + expect(page).to have_content "This crop was rejected for the following reason: Totally fake" + end end end context "signed in non-wrangler" do - let!(:crop_wranglers) { create_list :crop_wrangling_member, 3 } - let(:member) { create :member } + include_context 'signed in member' do + let!(:crop_wranglers) { create_list :crop_wrangling_member, 3 } + let(:member) { create :member } - before { login_as member } + it "can't see wrangling page without js", js: false do + visit root_path + expect(page).not_to have_link "Crop Wrangling" + end - it "can't see wrangling page without js", js: false do - visit root_path - expect(page).not_to have_link "Crop Wrangling" - end - - it "can't see wrangling page with js" do - visit root_path - click_link member.login_name - expect(page).not_to have_link "Crop Wrangling" + it "can't see wrangling page with js" do + visit root_path + click_link member.login_name + expect(page).not_to have_link "Crop Wrangling" + end end end end diff --git a/spec/features/crops/crop_wrangling_button_spec.rb b/spec/features/crops/crop_wrangling_button_spec.rb index 7e9c3d8d1..2dfdcda6d 100644 --- a/spec/features/crops/crop_wrangling_button_spec.rb +++ b/spec/features/crops/crop_wrangling_button_spec.rb @@ -1,28 +1,22 @@ require 'rails_helper' describe "crop wrangling button" do - let(:crop_wrangler) { create :crop_wrangling_member } - let(:member) { create :member } + context 'not signed in' do + before { visit crops_path } + it { expect(page).to have_no_link "Wrangle Crops", href: wrangle_crops_path } + end - context "crop wrangling button" do - before do - login_as crop_wrangler - visit crops_path - end - - it "has a link to crop wrangling page" do - expect(page).to have_link "Wrangle Crops", href: wrangle_crops_path + context "signed in, but not a crop wrangler" do + include_context 'signed in member' do + before { visit crops_path } + it { expect(page).to have_no_link "Wrangle Crops", href: wrangle_crops_path } end end - context "crop wrangling button" do - before do - login_as member - visit crops_path - end - - it "has no link to crop wrangling page" do - expect(page).to have_no_link "Wrangle Crops", href: wrangle_crops_path + context "signed in crop wrangler" do + include_context 'signed in crop wrangler' do + before { visit crops_path } + it { expect(page).to have_link "Wrangle Crops", href: wrangle_crops_path } end end end diff --git a/spec/features/crops/delete_crop_spec.rb b/spec/features/crops/delete_crop_spec.rb index b3c108ef9..00a8e7cec 100644 --- a/spec/features/crops/delete_crop_spec.rb +++ b/spec/features/crops/delete_crop_spec.rb @@ -2,28 +2,27 @@ require 'rails_helper' describe "Delete crop spec" do context "As a crop wrangler" do - let(:wrangler) { FactoryBot.create :crop_wrangling_member } let!(:pending_crop) { FactoryBot.create :crop_request } let!(:approved_crop) { FactoryBot.create :crop } - before { login_as wrangler } - - it "Delete approved crop" do - visit crop_path(approved_crop) - click_link 'Actions' - accept_confirm do - click_link 'Delete' + include_context 'signed in crop wrangler' do + it "deletes approved crop" do + visit crop_path(approved_crop) + click_link 'Actions' + accept_confirm do + click_link 'Delete' + end + expect(page).to have_content "crop was successfully destroyed" end - expect(page).to have_content "crop was successfully destroyed" - end - it "Delete pending crop" do - visit crop_path(pending_crop) - click_link 'Actions' - accept_confirm do - click_link 'Delete' + it "deletes pending crop" do + visit crop_path(pending_crop) + click_link 'Actions' + accept_confirm do + click_link 'Delete' + end + expect(page).to have_content "crop was successfully destroyed" end - expect(page).to have_content "crop was successfully destroyed" end end end diff --git a/spec/features/crops/request_new_crop_spec.rb b/spec/features/crops/request_new_crop_spec.rb index cd5cfb984..a35480b66 100644 --- a/spec/features/crops/request_new_crop_spec.rb +++ b/spec/features/crops/request_new_crop_spec.rb @@ -2,46 +2,40 @@ require 'rails_helper' describe "Requesting a new crop" do context "As a regular member" do - let(:member) { create :member } - let!(:wrangler) { create :crop_wrangling_member } - - before do - login_as member - end - - it "Submit request" do - visit new_crop_path - fill_in "Name", with: "Couch potato" - fill_in "request_notes", with: "Couch potatoes are real for real." - click_button "Save" - expect(page).to have_content 'crop was successfully created.' - expect(page).to have_content "This crop is currently pending approval." + include_context 'signed in member' do + it "Submit request" do + visit new_crop_path + fill_in "Name", with: "Couch potato" + fill_in "request_notes", with: "Couch potatoes are real for real." + click_button "Save" + expect(page).to have_content 'crop was successfully created.' + expect(page).to have_content "This crop is currently pending approval." + end end end context "As a crop wrangler" do - let(:wrangler) { create :crop_wrangling_member } let!(:crop) { create :crop_request } let!(:already_approved) { create :crop } - before { login_as wrangler } + include_context 'signed in crop wrangler' do + it "Approve a request" do + visit edit_crop_path(crop) + select "approved", from: "Approval status" + click_button "Save" + expect(page).to have_content "En wikipedia url is not a valid English Wikipedia URL" + fill_in "en_wikipedia_url", with: "http://en.wikipedia.org/wiki/Aung_San_Suu_Kyi" + click_button "Save" + expect(page).to have_content "crop was successfully updated." + end - it "Approve a request" do - visit edit_crop_path(crop) - select "approved", from: "Approval status" - click_button "Save" - expect(page).to have_content "En wikipedia url is not a valid English Wikipedia URL" - fill_in "en_wikipedia_url", with: "http://en.wikipedia.org/wiki/Aung_San_Suu_Kyi" - click_button "Save" - expect(page).to have_content "crop was successfully updated." - end - - it "Rejecting a crop" do - visit edit_crop_path(crop) - select "rejected", from: "Approval status" - select "not edible", from: "Reason for rejection" - click_button "Save" - expect(page).to have_content "crop was successfully updated." + it "Rejecting a crop" do + visit edit_crop_path(crop) + select "rejected", from: "Approval status" + select "not edible", from: "Reason for rejection" + click_button "Save" + expect(page).to have_content "crop was successfully updated." + end end end end diff --git a/spec/features/crops/requested_crops_spec.rb b/spec/features/crops/requested_crops_spec.rb index a4e406b63..5692e71fc 100644 --- a/spec/features/crops/requested_crops_spec.rb +++ b/spec/features/crops/requested_crops_spec.rb @@ -1,15 +1,13 @@ require 'rails_helper' -describe "Crop - " do - let(:member) { create :member } +describe "Requesting Crops" do let!(:requested_crop) { create :crop, requester: member, approval_status: 'pending', name: 'puha for dinner' } - before do - login_as member - visit requested_crops_path - end + include_context 'signed in member' do + before { visit requested_crops_path } - it "creating a crop with multiple scientific and alternate name", :js do - expect(page).to have_content "puha for dinner" + it "creating a crop with multiple scientific and alternate name", :js do + expect(page).to have_content "puha for dinner" + end end end diff --git a/spec/features/crops/scientific_name_spec.rb b/spec/features/crops/scientific_name_spec.rb index be428f7b0..3b525c5c3 100644 --- a/spec/features/crops/scientific_name_spec.rb +++ b/spec/features/crops/scientific_name_spec.rb @@ -20,69 +20,67 @@ describe "Scientific names", js: true do let!(:crop_wranglers) { create_list :crop_wrangling_member, 3 } let(:member) { crop_wranglers.first } - before do - login_as(member) - end - - it "Crop wranglers can edit scientific names" do - visit crop_path(crop) - # expect(page.status_code).to equal 200 - expect(page).to have_content "CROP WRANGLER" - expect(page).to have_content zea_mays.name - click_link zea_mays.name - expect(page).to have_link "Edit", href: edit_scientific_name_path(zea_mays) - within('.scientific_names') { click_on "Edit" } - # expect(page.status_code).to equal 200 - expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" - fill_in 'Name', with: "Zea mirabila" - click_on "Save" - expect(page).to have_content "Zea mirabila" - expect(page).to have_content 'crop was successfully updated' - end - - it "Crop wranglers can delete scientific names" do - visit crop_path(zea_mays.crop) - click_link zea_mays.name - expect(page).to have_link "Delete", - href: scientific_name_path(zea_mays) - within('.scientific_names') do - accept_confirm do - click_link 'Delete' - end + include_context 'signed in crop wrangler' do + it "Crop wranglers can edit scientific names" do + visit crop_path(crop) + # expect(page.status_code).to equal 200 + expect(page).to have_content "CROP WRANGLER" + expect(page).to have_content zea_mays.name + click_link zea_mays.name + expect(page).to have_link "Edit", href: edit_scientific_name_path(zea_mays) + within('.scientific_names') { click_on "Edit" } + # expect(page.status_code).to equal 200 + expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" + fill_in 'Name', with: "Zea mirabila" + click_on "Save" + expect(page).to have_content "Zea mirabila" + expect(page).to have_content 'crop was successfully updated' end - # expect(page.status_code).to equal 200 - expect(page).not_to have_content zea_mays.name - expect(page).to have_content 'Scientific name was successfully deleted.' - end - it "Crop wranglers can add scientific names" do - visit crop_path(crop) - expect(page).to have_link "Add", - href: new_scientific_name_path(crop_id: crop.id) - within('.scientific_names') { click_on "Add" } - # expect(page.status_code).to equal 200 - expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" - fill_in 'Name', with: "Zea mirabila" - click_on "Save" - # expect(page.status_code).to equal 200 - expect(page).to have_content "Zea mirabila" - expect(page).to have_content 'crop was successfully created.' - end + it "Crop wranglers can delete scientific names" do + visit crop_path(zea_mays.crop) + click_link zea_mays.name + expect(page).to have_link "Delete", + href: scientific_name_path(zea_mays) + within('.scientific_names') do + accept_confirm do + click_link 'Delete' + end + end + # expect(page.status_code).to equal 200 + expect(page).not_to have_content zea_mays.name + expect(page).to have_content 'Scientific name was successfully deleted.' + end - it "The show-scientific-name page works" do - visit scientific_name_path(zea_mays) - # expect(page.status_code).to equal 200 - expect(page).to have_link zea_mays.crop.name, - href: crop_path(zea_mays.crop) - end + it "Crop wranglers can add scientific names" do + visit crop_path(crop) + expect(page).to have_link "Add", + href: new_scientific_name_path(crop_id: crop.id) + within('.scientific_names') { click_on "Add" } + # expect(page.status_code).to equal 200 + expect(page).to have_css "option[value='#{crop.id}'][selected=selected]" + fill_in 'Name', with: "Zea mirabila" + click_on "Save" + # expect(page.status_code).to equal 200 + expect(page).to have_content "Zea mirabila" + expect(page).to have_content 'crop was successfully created.' + end - context "When scientific name is pending" do - let(:pending_crop) { create :crop_request } - let(:pending_sci_name) { create :scientific_name, crop: pending_crop } + it "The show-scientific-name page works" do + visit scientific_name_path(zea_mays) + # expect(page.status_code).to equal 200 + expect(page).to have_link zea_mays.crop.name, + href: crop_path(zea_mays.crop) + end - it "Displays crop pending message" do - visit scientific_name_path(pending_sci_name) - expect(page).to have_content "This crop is currently pending approval" + context "When scientific name is pending" do + let(:pending_crop) { create :crop_request } + let(:pending_sci_name) { create :scientific_name, crop: pending_crop } + + it "Displays crop pending message" do + visit scientific_name_path(pending_sci_name) + expect(page).to have_content "This crop is currently pending approval" + end end end end diff --git a/spec/features/gardens/actions_spec.rb b/spec/features/gardens/actions_spec.rb index 0e53e178b..98f37adcd 100644 --- a/spec/features/gardens/actions_spec.rb +++ b/spec/features/gardens/actions_spec.rb @@ -5,73 +5,72 @@ describe "Gardens" do context 'logged in' do subject { page } - let(:member) { FactoryBot.create :member } - before { login_as member } + include_context 'signed in member' do + let(:garden) { member.gardens.first } + let(:other_member_garden) { FactoryBot.create :garden } - let(:garden) { member.gardens.first } - let(:other_member_garden) { FactoryBot.create :garden } + describe '#index' do + shared_examples "has buttons bar at top" do + it "has buttons bar at top" do + within '.layout-actions' do + expect(subject).to have_link 'Add a garden' + expect(subject).to have_link 'My gardens' + expect(subject).to have_link "Everyone's gardens" + end + end + end - describe '#index' do - shared_examples "has buttons bar at top" do - it "has buttons bar at top" do - within '.layout-actions' do - expect(subject).to have_link 'Add a garden' - expect(subject).to have_link 'My gardens' - expect(subject).to have_link "Everyone's gardens" + context 'my gardens' do + before { visit gardens_path(member_slug: member.slug) } + + include_examples "has buttons bar at top" + + context 'with actions menu expanded' do + before { click_link 'Actions' } + it "has actions on garden" do + expect(subject).to have_link 'Plant something here' + expect(subject).to have_link 'Mark as inactive' + expect(subject).to have_link 'Edit' + expect(subject).to have_link 'Add photo' + expect(subject).to have_link 'Delete' + end + end + end + + context 'all gardens' do + before { visit gardens_path } + + include_examples "has buttons bar at top" + end + + context "other member's garden" do + before { visit gardens_path(member_slug: FactoryBot.create(:member).slug) } + + include_examples "has buttons bar at top" + describe 'does not show actions on other member garden' do + it { is_expected.not_to have_link 'Actions' } end end end - context 'my gardens' do - before { visit gardens_path(member_slug: member.slug) } + describe '#show' do + describe 'my garden' do + before { visit garden_path(garden) } - include_examples "has buttons bar at top" - - context 'with actions menu expanded' do - before { click_link 'Actions' } - it "has actions on garden" do - expect(subject).to have_link 'Plant something here' - expect(subject).to have_link 'Mark as inactive' - expect(subject).to have_link 'Edit' - expect(subject).to have_link 'Add photo' - expect(subject).to have_link 'Delete' + context 'with actions menu expanded' do + before { click_link 'Actions' } + it { is_expected.to have_link 'Edit' } + it { is_expected.to have_link 'Delete' } + it { is_expected.to have_content "Plant something here" } + it { is_expected.to have_content "Add photo" } end end - end - context 'all gardens' do - before { visit gardens_path } - - include_examples "has buttons bar at top" - end - - context "other member's garden" do - before { visit gardens_path(member_slug: FactoryBot.create(:member).slug) } - - include_examples "has buttons bar at top" - describe 'does not show actions on other member garden' do + describe "someone else's garden" do + before { visit garden_path(other_member_garden) } it { is_expected.not_to have_link 'Actions' } end end end - - describe '#show' do - describe 'my garden' do - before { visit garden_path(garden) } - - context 'with actions menu expanded' do - before { click_link 'Actions' } - it { is_expected.to have_link 'Edit' } - it { is_expected.to have_link 'Delete' } - it { is_expected.to have_content "Plant something here" } - it { is_expected.to have_content "Add photo" } - end - end - - describe "someone else's garden" do - before { visit garden_path(other_member_garden) } - it { is_expected.not_to have_link 'Actions' } - end - end end end diff --git a/spec/features/gardens/adding_gardens_spec.rb b/spec/features/gardens/adding_gardens_spec.rb index bb161e4fd..47f521fd1 100644 --- a/spec/features/gardens/adding_gardens_spec.rb +++ b/spec/features/gardens/adding_gardens_spec.rb @@ -2,37 +2,34 @@ require 'rails_helper' require 'custom_matchers' describe "Gardens", :js do - let(:member) { FactoryBot.create :member } + include_context 'signed in member' do + before { visit new_garden_path } - before do - login_as member - visit new_garden_path - end + it "has the required fields help text" do + expect(page).to have_content "* denotes a required field" + end - it "has the required fields help text" do - expect(page).to have_content "* denotes a required field" - end + it "displays required and optional fields properly" do + expect(page).to have_selector ".required", text: "Name" + expect(page).to have_selector 'textarea#garden_description' + expect(page).to have_selector 'input#garden_location' + expect(page).to have_selector 'input#garden_area' + end - it "displays required and optional fields properly" do - expect(page).to have_selector ".required", text: "Name" - expect(page).to have_selector 'textarea#garden_description' - expect(page).to have_selector 'input#garden_location' - expect(page).to have_selector 'input#garden_area' - end + it "Create new garden" do + fill_in "Name", with: "New garden" + click_button "Save" + expect(page).to have_content "Garden was successfully created" + expect(page).to have_content "New garden" + end - it "Create new garden" do - fill_in "Name", with: "New garden" - click_button "Save" - expect(page).to have_content "Garden was successfully created" - expect(page).to have_content "New garden" - end - - it "Refuse to create new garden with negative area" do - visit new_garden_path - fill_in "Name", with: "Negative Garden" - fill_in "Area", with: -5 - click_button "Save" - expect(page).not_to have_content "Garden was successfully created" - expect(page).to have_content "Area must be greater than or equal to 0" + it "Refuse to create new garden with negative area" do + visit new_garden_path + fill_in "Name", with: "Negative Garden" + fill_in "Area", with: -5 + click_button "Save" + expect(page).not_to have_content "Garden was successfully created" + expect(page).to have_content "Area must be greater than or equal to 0" + end end end diff --git a/spec/features/gardens/gardens_spec.rb b/spec/features/gardens/gardens_spec.rb index f39c210f6..fdb517b08 100644 --- a/spec/features/gardens/gardens_spec.rb +++ b/spec/features/gardens/gardens_spec.rb @@ -1,121 +1,119 @@ require 'rails_helper' describe "Planting a crop", js: true do - # name is aaa to ensure it is ordered first - let!(:garden) { create :garden, name: 'aaa' } - let!(:planting) { create :planting, garden: garden, owner: garden.owner, planted_at: Date.parse("2013-3-10") } - let!(:tomato) { create :tomato } - let!(:finished_planting) { create :finished_planting, owner: garden.owner, garden: garden, crop: tomato } + include_context 'signed in member' do + # name is aaa to ensure it is ordered first + let!(:garden) { create :garden, name: 'aaa', owner: member } + let!(:planting) { create :planting, garden: garden, owner: garden.owner, planted_at: Date.parse("2013-3-10") } + let!(:tomato) { create :tomato } + let!(:finished_planting) { create :finished_planting, owner: garden.owner, garden: garden, crop: tomato } - before do - login_as garden.owner - end - - it "View gardens" do - visit gardens_path - expect(page).to have_content "Everyone's gardens" - within '.layout-actions' do - click_link "My gardens" - end - expect(page).to have_content "#{garden.owner.login_name}'s gardens" - within '.layout-actions' do - click_link "Everyone's gardens" - end - expect(page).to have_content "Everyone's gardens" - end - - it "Marking a garden as inactive" do - visit garden_path(garden) - click_link 'Actions' - accept_confirm do - click_link "Mark as inactive" - end - expect(page).to have_content "Garden was successfully updated" - expect(page).to have_content "This garden is inactive" - - click_link 'Actions' - expect(page).to have_content "Mark as active" - expect(page).not_to have_content "Mark as inactive" - end - - it "List only active gardens" do - visit garden_path(garden) - click_link 'Actions' - accept_confirm do - click_link "Mark as inactive" - end - visit gardens_path - expect(page).not_to have_link garden_path(garden) - end - - it "Create new garden" do - visit new_garden_path - fill_in "Name", with: "New garden" - click_button "Save" - expect(page).to have_content "Garden was successfully created" - expect(page).to have_content "New garden" - end - - it "Refuse to create new garden with negative area" do - visit new_garden_path - fill_in "Name", with: "Negative Garden" - fill_in "Area", with: -5 - click_button "Save" - expect(page).not_to have_content "Garden was successfully created" - expect(page).to have_content "Area must be greater than or equal to 0" - end - - context "Clicking edit from the index page" do - before do + it "View gardens" do visit gardens_path + expect(page).to have_content "Everyone's gardens" + within '.layout-actions' do + click_link "My gardens" + end + expect(page).to have_content "#{garden.owner.login_name}'s gardens" + within '.layout-actions' do + click_link "Everyone's gardens" + end + expect(page).to have_content "Everyone's gardens" end - it "button on index to edit garden" do - first('a#garden-actions-button').click - click_link href: edit_garden_path(garden) - expect(page).to have_content 'Edit garden' - end - end - - it "Edit garden" do - visit new_garden_path - fill_in "Name", with: "New garden" - click_button "Save" - click_link 'Actions' - within '.garden-actions' do - click_link 'Edit' - end - fill_in "Name", with: "Different name" - click_button "Save" - expect(page).to have_content "Garden was successfully updated" - expect(page).to have_content "Different name" - end - - it "Delete garden" do - visit new_garden_path - fill_in "Name", with: "New garden" - click_button "Save" - visit garden_path(Garden.last) - click_link 'Actions' - accept_confirm do - click_link 'Delete' - end - expect(page).to have_content "Garden was successfully deleted" - expect(page).to have_content "#{garden.owner}'s gardens" - end - - describe "Making a planting inactive from garden show" do - it do + it "Marking a garden as inactive" do visit garden_path(garden) - click_link(class: 'planting-menu') - click_link "Mark as finished" - find(".datepicker-days td.day", text: "21").click - expect(page).to have_content 'Finished' - end - end + click_link 'Actions' + accept_confirm do + click_link "Mark as inactive" + end + expect(page).to have_content "Garden was successfully updated" + expect(page).to have_content "This garden is inactive" - it "List only active plantings on a garden" do - visit gardens_path - expect(page).not_to have_content finished_planting.crop_name + click_link 'Actions' + expect(page).to have_content "Mark as active" + expect(page).not_to have_content "Mark as inactive" + end + + it "List only active gardens" do + visit garden_path(garden) + click_link 'Actions' + accept_confirm do + click_link "Mark as inactive" + end + visit gardens_path + expect(page).not_to have_link garden_path(garden) + end + + it "Create new garden" do + visit new_garden_path + fill_in "Name", with: "New garden" + click_button "Save" + expect(page).to have_content "Garden was successfully created" + expect(page).to have_content "New garden" + end + + it "Refuse to create new garden with negative area" do + visit new_garden_path + fill_in "Name", with: "Negative Garden" + fill_in "Area", with: -5 + click_button "Save" + expect(page).not_to have_content "Garden was successfully created" + expect(page).to have_content "Area must be greater than or equal to 0" + end + + context "Clicking edit from the index page" do + before do + visit gardens_path + end + + it "button on index to edit garden" do + first('a#garden-actions-button').click + click_link href: edit_garden_path(garden) + expect(page).to have_content 'Edit garden' + end + end + + it "Edit garden" do + visit new_garden_path + fill_in "Name", with: "New garden" + click_button "Save" + click_link 'Actions' + within '.garden-actions' do + click_link 'Edit' + end + fill_in "Name", with: "Different name" + click_button "Save" + expect(page).to have_content "Garden was successfully updated" + expect(page).to have_content "Different name" + end + + it "Delete garden" do + visit new_garden_path + fill_in "Name", with: "New garden" + click_button "Save" + visit garden_path(Garden.last) + click_link 'Actions' + accept_confirm do + click_link 'Delete' + end + expect(page).to have_content "Garden was successfully deleted" + expect(page).to have_content "#{garden.owner}'s gardens" + end + + describe "Making a planting inactive from garden show" do + it do + visit garden_path(garden) + click_link(class: 'planting-menu') + click_link "Mark as finished" + find(".datepicker-days td.day", text: "21").click + expect(page).to have_content 'Finished' + end + end + + it "List only active plantings on a garden" do + visit gardens_path + expect(page).not_to have_content finished_planting.crop_name + end end end diff --git a/spec/features/gardens/index_spec.rb b/spec/features/gardens/index_spec.rb index e14bbbc5d..39b422d25 100644 --- a/spec/features/gardens/index_spec.rb +++ b/spec/features/gardens/index_spec.rb @@ -3,68 +3,68 @@ require 'custom_matchers' describe "Gardens#index", :js do context "Logged in as member" do - let(:member) { FactoryBot.create :member, login_name: 'shadow' } + include_context 'signed in member' do + let(:member) { FactoryBot.create :member, login_name: 'shadow' } - before { login_as member } + context "with 10 gardens" do + before do + FactoryBot.create_list :garden, 10, owner: member + visit member_gardens_path(member_slug: member.slug) + end - context "with 10 gardens" do - before do - FactoryBot.create_list :garden, 10, owner: member - visit member_gardens_path(member_slug: member.slug) - end - - it "displays each of the gardens" do - member.gardens.each do |garden| - expect(page).to have_text garden.name + it "displays each of the gardens" do + member.gardens.each do |garden| + expect(page).to have_text garden.name + end + end + it "links to each garden" do + member.gardens.each do |garden| + expect(page).to have_link(garden.name, href: garden_path(garden)) + end end end - it "links to each garden" do - member.gardens.each do |garden| - expect(page).to have_link(garden.name, href: garden_path(garden)) + + context "with inactive gardens" do + let!(:active_garden) { FactoryBot.create :garden, name: "My active garden", owner: member } + let!(:inactive_garden) { FactoryBot.create :inactive_garden, name: "retired garden", owner: member } + + before { visit member_gardens_path(member_slug: member.slug) } + + it "show active garden" do + expect(page).to have_text active_garden.name + end + it "does not show inactive garden" do + expect(page).not_to have_text inactive_garden.name + end + it "links to active garden" do + expect(page).to have_link(active_garden.name, href: garden_path(active_garden)) + end + it "does not link to inactive gardens" do + expect(page).not_to have_link(inactive_garden.name, href: garden_path(inactive_garden)) end end - end - context "with inactive gardens" do - let!(:active_garden) { FactoryBot.create :garden, name: "My active garden", owner: member } - let!(:inactive_garden) { FactoryBot.create :inactive_garden, name: "retired garden", owner: member } + context 'with plantings' do + let(:maize) { FactoryBot.create(:maize) } + let(:tomato) { FactoryBot.create(:tomato) } - before { visit member_gardens_path(member_slug: member.slug) } + let!(:planting) do + FactoryBot.create :planting, owner: member, crop: maize, garden: member.gardens.first + end + let!(:finished_planting) do + FactoryBot.create :finished_planting, owner: member, crop: tomato, garden: member.gardens.first + end - it "show active garden" do - expect(page).to have_text active_garden.name - end - it "does not show inactive garden" do - expect(page).not_to have_text inactive_garden.name - end - it "links to active garden" do - expect(page).to have_link(active_garden.name, href: garden_path(active_garden)) - end - it "does not link to inactive gardens" do - expect(page).not_to have_link(inactive_garden.name, href: garden_path(inactive_garden)) - end - end + before do + visit member_gardens_path(member_slug: member.slug) + end - context 'with plantings' do - let(:maize) { FactoryBot.create(:maize) } - let(:tomato) { FactoryBot.create(:tomato) } - - let!(:planting) do - FactoryBot.create :planting, owner: member, crop: maize, garden: member.gardens.first - end - let!(:finished_planting) do - FactoryBot.create :finished_planting, owner: member, crop: tomato, garden: member.gardens.first - end - - before do - visit member_gardens_path(member_slug: member.slug) - end - - it "shows planting in garden" do - expect(page).to have_link(planting.crop.name, href: planting_path(planting)) - end - it "does not show finished planting" do - expect(page).not_to have_text(finished_planting.crop.name) + it "shows planting in garden" do + expect(page).to have_link(planting.crop.name, href: planting_path(planting)) + end + it "does not show finished planting" do + expect(page).not_to have_text(finished_planting.crop.name) + end end end end diff --git a/spec/features/harvests/browse_harvests_spec.rb b/spec/features/harvests/browse_harvests_spec.rb index 67dc75e55..bc6b29078 100644 --- a/spec/features/harvests/browse_harvests_spec.rb +++ b/spec/features/harvests/browse_harvests_spec.rb @@ -3,30 +3,29 @@ require 'rails_helper' describe "browse harvests" do subject { page } - let!(:member) { create :member } let!(:harvest) { create :harvest, owner: member } - before { login_as member } + include_context 'signed in member' do + describe 'blank optional fields' do + let!(:harvest) { create :harvest, :no_description } - describe 'blank optional fields' do - let!(:harvest) { create :harvest, :no_description } + before { visit harvests_path } - before { visit harvests_path } - - it 'read more' do - expect(subject).not_to have_link "Read more" - end - end - - describe "filled in optional fields" do - let!(:harvest) { create :harvest, :long_description } - - before do - visit harvests_path + it 'read more' do + expect(subject).not_to have_link "Read more" + end end - it 'links to #show' do - expect(subject).to have_link harvest.crop.name, href: harvest_path(harvest) + describe "filled in optional fields" do + let!(:harvest) { create :harvest, :long_description } + + before do + visit harvests_path + end + + it 'links to #show' do + expect(subject).to have_link harvest.crop.name, href: harvest_path(harvest) + end end end end diff --git a/spec/features/harvests/harvesting_a_crop_spec.rb b/spec/features/harvests/harvesting_a_crop_spec.rb index c425dfa4f..d3ded52e1 100644 --- a/spec/features/harvests/harvesting_a_crop_spec.rb +++ b/spec/features/harvests/harvesting_a_crop_spec.rb @@ -2,128 +2,126 @@ require 'rails_helper' require 'custom_matchers' describe "Harvesting a crop", :js, :elasticsearch do - let(:member) { create :member } - let!(:maize) { create :maize } - let!(:plant_part) { create :plant_part } - let(:planting) { create :planting, crop: maize, owner: member } + include_context 'signed in member' do + let!(:maize) { create :maize } + let!(:plant_part) { create :plant_part } + let(:planting) { create :planting, crop: maize, owner: member } - before do - login_as member - visit new_harvest_path - end + before { visit new_harvest_path } - it_behaves_like "crop suggest", "harvest", "crop" + it_behaves_like "crop suggest", "harvest", "crop" - it "has the required fields help text" do - expect(page).to have_content "* denotes a required field" - end - - describe "displays required and optional fields properly" do - it { expect(page).to have_selector ".required", text: "What did you harvest?" } - it { expect(page).to have_selector 'input#harvest_quantity' } - it { expect(page).to have_selector 'input#harvest_weight_quantity' } - it { expect(page).to have_selector 'textarea#harvest_description' } - end - - it "Creating a new harvest", :js do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - - within "form#new_harvest" do - choose plant_part.name - fill_in "When?", with: "2014-06-15" - fill_in "How many?", with: 42 - fill_in "Weighing (in total)", with: 42 - fill_in "Notes", with: "It's killer." - click_button "Save" + it "has the required fields help text" do + expect(page).to have_content "* denotes a required field" end - expect(page).to have_content "harvest was successfully created." - end + describe "displays required and optional fields properly" do + it { expect(page).to have_selector ".required", text: "What did you harvest?" } + it { expect(page).to have_selector 'input#harvest_quantity' } + it { expect(page).to have_selector 'input#harvest_weight_quantity' } + it { expect(page).to have_selector 'textarea#harvest_description' } + end - it "Clicking link to owner's profile" do - visit member_harvests_path(member) - click_link "View #{member}'s profile >>" - expect(current_path).to eq member_path member - end + it "Creating a new harvest", :js do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" - describe "Harvesting from crop page" do - before do - visit crop_path(maize) - within '.crop-actions' do - click_link "Harvest #{maize.name}" - end within "form#new_harvest" do choose plant_part.name - expect(page).to have_selector "input[value='maize']" + fill_in "When?", with: "2014-06-15" + fill_in "How many?", with: 42 + fill_in "Weighing (in total)", with: 42 + fill_in "Notes", with: "It's killer." click_button "Save" end + + expect(page).to have_content "harvest was successfully created." + end + + it "Clicking link to owner's profile" do + visit member_harvests_path(member) + click_link "View #{member}'s profile >>" + expect(current_path).to eq member_path member + end + + describe "Harvesting from crop page" do + before do + visit crop_path(maize) + within '.crop-actions' do + click_link "Harvest #{maize.name}" + end + within "form#new_harvest" do + choose plant_part.name + expect(page).to have_selector "input[value='maize']" + click_button "Save" + end + end + + it { expect(page).to have_content "harvest was successfully created." } + it { expect(page).to have_content "maize" } + end + + describe "Harvesting from planting page" do + let!(:planting) { create :planting, crop: maize, owner: member, garden: member.gardens.first } + before do + visit planting_path(planting) + click_link "Record Harvest" + + choose plant_part.name + click_button "Save" + end + + it { expect(page).to have_content "harvest was successfully created." } + it { expect(page).to have_content planting.garden.name } + it { expect(page).to have_content "maize" } + end + + context "Editing a harvest" do + let(:existing_harvest) { create :harvest, crop: maize, owner: member } + let!(:other_plant_part) { create :plant_part, name: 'chocolate' } + + before do + visit harvest_path(existing_harvest) + click_link 'Actions' + click_link "Edit" + end + + it "Saving without edits" do + # Check that the autosuggest helper properly fills inputs with + # existing resource's data + click_button "Save" + expect(page).to have_content "harvest was successfully updated." + expect(page).to have_content "maize" + end + + it "change plant part" do + choose other_plant_part.name + click_button "Save" + expect(page).to have_content "harvest was successfully updated." + expect(page).to have_content other_plant_part.name + end end - it { expect(page).to have_content "harvest was successfully created." } - it { expect(page).to have_content "maize" } - end + context "Viewing a harvest" do + let(:existing_harvest) do + create :harvest, crop: maize, owner: member, + harvested_at: Time.zone.today + end + let!(:existing_planting) do + create :planting, crop: maize, owner: member, + planted_at: Time.zone.yesterday + end - describe "Harvesting from planting page" do - let!(:planting) { create :planting, crop: maize, owner: member, garden: member.gardens.first } - before do - visit planting_path(planting) - click_link "Record Harvest" + before do + visit harvest_path(existing_harvest) + end - choose plant_part.name - click_button "Save" - end - - it { expect(page).to have_content "harvest was successfully created." } - it { expect(page).to have_content planting.garden.name } - it { expect(page).to have_content "maize" } - end - - context "Editing a harvest" do - let(:existing_harvest) { create :harvest, crop: maize, owner: member } - let!(:other_plant_part) { create :plant_part, name: 'chocolate' } - - before do - visit harvest_path(existing_harvest) - click_link 'Actions' - click_link "Edit" - end - - it "Saving without edits" do - # Check that the autosuggest helper properly fills inputs with - # existing resource's data - click_button "Save" - expect(page).to have_content "harvest was successfully updated." - expect(page).to have_content "maize" - end - - it "change plant part" do - choose other_plant_part.name - click_button "Save" - expect(page).to have_content "harvest was successfully updated." - expect(page).to have_content other_plant_part.name - end - end - - context "Viewing a harvest" do - let(:existing_harvest) do - create :harvest, crop: maize, owner: member, - harvested_at: Time.zone.today - end - let!(:existing_planting) do - create :planting, crop: maize, owner: member, - planted_at: Time.zone.yesterday - end - - before do - visit harvest_path(existing_harvest) - end - - it "linking to a planting" do - expect(page).to have_content existing_planting.to_s - choose("harvest_planting_id_#{existing_planting.id}") - click_button "save" - expect(page).to have_link(href: planting_path(existing_planting)) + it "linking to a planting" do + expect(page).to have_content existing_planting.to_s + choose("harvest_planting_id_#{existing_planting.id}") + click_button "save" + expect(page).to have_link(href: planting_path(existing_planting)) + end end end end diff --git a/spec/features/home/home_spec.rb b/spec/features/home/home_spec.rb index 89dbacdc5..42d354f14 100644 --- a/spec/features/home/home_spec.rb +++ b/spec/features/home/home_spec.rb @@ -79,17 +79,17 @@ describe "home page" do end context "when signed in" do - before { login_as member } + include_context 'signed in member' do + include_examples 'show crops' + include_examples 'show plantings' + include_examples 'show harvests' + include_examples 'shows seeds' - include_examples 'show crops' - include_examples 'show plantings' - include_examples 'show harvests' - include_examples 'shows seeds' + describe 'should say welcome' do + before { visit root_path } - describe 'should say welcome' do - before { visit root_path } - - it { expect(page).to have_content "Welcome to #{ENV['GROWSTUFF_SITE_NAME']}, #{member.login_name}" } + it { expect(page).to have_content "Welcome to #{ENV['GROWSTUFF_SITE_NAME']}, #{member.login_name}" } + end end end end diff --git a/spec/features/likeable_spec.rb b/spec/features/likeable_spec.rb index 42aa03c6b..068fcba77 100644 --- a/spec/features/likeable_spec.rb +++ b/spec/features/likeable_spec.rb @@ -6,9 +6,7 @@ describe 'Likeable', js: true do let!(:post) { FactoryBot.create(:post, author: member) } let!(:photo) { FactoryBot.create(:photo, owner: member) } - context 'logged in member' do - before { login_as member } - + include_context 'signed in member' do describe 'photos' do let(:like_count_class) { "#photo-#{photo.id} .like-count" } diff --git a/spec/features/members/ban_spec.rb b/spec/features/members/ban_spec.rb index 8eb2888e7..256083a3e 100644 --- a/spec/features/members/ban_spec.rb +++ b/spec/features/members/ban_spec.rb @@ -4,18 +4,17 @@ describe "members list" do let!(:spammer) { FactoryBot.create :member } let!(:admin) { FactoryBot.create :admin_member } - context 'logged in as admin' do - before do - login_as admin - visit member_path(spammer) - end - it { expect(page).to have_link "Ban member" } - describe 'bans the user' do - before do - accept_confirm { click_link 'Ban member' } + context 'logged in as admin' do + include_context 'signed in admin' do + before { visit member_path(spammer) } + it { expect(page).to have_link "Ban member" } + describe 'bans the user' do + before do + accept_confirm { click_link 'Ban member' } + end + it { expect(page).to have_link admin.login_name } + it { expect(page).not_to have_link spammer.login_name } end - it { expect(page).to have_link admin.login_name } - it { expect(page).not_to have_link spammer.login_name } end end end diff --git a/spec/features/members/following_spec.rb b/spec/features/members/following_spec.rb index 762320671..fea8b5a7f 100644 --- a/spec/features/members/following_spec.rb +++ b/spec/features/members/following_spec.rb @@ -12,65 +12,62 @@ describe "follows", :js do end context "when signed in" do - let(:member) { create :member } - let(:other_member) { create :member } + include_context 'signed in member' do + let(:other_member) { create :member } - before do - login_as(member) - end - - it "your profile doesn't have a follow button" do - visit member_path(member) - expect(page).not_to have_link "Follow" - expect(page).not_to have_link "Unfollow" - end - - context "following another member" do - before { visit member_path(other_member) } - - it "has a follow button" do - expect(page).to have_link "Follow", href: follows_path(followed: other_member.slug) + it "your profile doesn't have a follow button" do + visit member_path(member) + expect(page).not_to have_link "Follow" + expect(page).not_to have_link "Unfollow" end - it "has correct message and unfollow button" do - click_link 'Follow' - expect(page).to have_content "Followed #{other_member.login_name}" - expect(page).to have_link "Unfollow", href: follow_path(member.get_follow(other_member)) - end + context "following another member" do + before { visit member_path(other_member) } - it "has a followed member listed in the following page" do - click_link 'Follow' - visit member_follows_path(member) - expect(page).to have_content other_member.login_name - end + it "has a follow button" do + expect(page).to have_link "Follow", href: follows_path(followed: other_member.slug) + end - it "has correct message and follow button after unfollow" do - click_link 'Follow' - click_link 'Unfollow' - expect(page).to have_content "Unfollowed #{other_member.login_name}" - visit member_path(other_member) # unfollowing redirects to root - expect(page).to have_link "Follow", href: follows_path(followed: other_member.slug) - end + it "has correct message and unfollow button" do + click_link 'Follow' + expect(page).to have_content "Followed #{other_member.login_name}" + expect(page).to have_link "Unfollow", href: follow_path(member.get_follow(other_member)) + end - it "has member in following list" do - click_link 'Follow' - visit member_follows_path(member) - expect(page).to have_content other_member.login_name - end + it "has a followed member listed in the following page" do + click_link 'Follow' + visit member_follows_path(member) + expect(page).to have_content other_member.login_name + end - it "appears in in followed member's followers list" do - click_link 'Follow' - visit member_followers_path(other_member) - expect(page).to have_content member.login_name - end + it "has correct message and follow button after unfollow" do + click_link 'Follow' + click_link 'Unfollow' + expect(page).to have_content "Unfollowed #{other_member.login_name}" + visit member_path(other_member) # unfollowing redirects to root + expect(page).to have_link "Follow", href: follows_path(followed: other_member.slug) + end - it "removes members from following and followers lists after unfollow" do - click_link 'Follow' - click_link 'Unfollow' - visit member_follows_path(member) - expect(page).not_to have_content other_member.login_name - visit member_followers_path(other_member) - expect(page).to have_content member.login_name + it "has member in following list" do + click_link 'Follow' + visit member_follows_path(member) + expect(page).to have_content other_member.login_name + end + + it "appears in in followed member's followers list" do + click_link 'Follow' + visit member_followers_path(other_member) + expect(page).to have_content member.login_name + end + + it "removes members from following and followers lists after unfollow" do + click_link 'Follow' + click_link 'Unfollow' + visit member_follows_path(member) + expect(page).not_to have_content other_member.login_name + visit member_followers_path(other_member) + expect(page).to have_content member.login_name + end end end end diff --git a/spec/features/members/profile_spec.rb b/spec/features/members/profile_spec.rb index 6b6ab0f94..a2296b247 100644 --- a/spec/features/members/profile_spec.rb +++ b/spec/features/members/profile_spec.rb @@ -162,26 +162,26 @@ describe "member profile", js: true do end context "signed in member" do - before { login_as(member) } + include_context 'signed in member' do + include_examples 'member details' + include_examples 'member activity' - include_examples 'member details' - include_examples 'member activity' + context "your own profile page" do + before { visit member_path(member) } - context "your own profile page" do - before { visit member_path(member) } - - it "has a button to edit profile" do - expect(page).to have_link "Edit profile", href: edit_member_registration_path + it "has a button to edit profile" do + expect(page).to have_link "Edit profile", href: edit_member_registration_path + end end - end - context "someone else's profile page" do - before { visit member_path(other_member) } + context "someone else's profile page" do + before { visit member_path(other_member) } - it "has a private message button" do - expect(page).to have_link "Send message", href: new_notification_path(recipient_id: other_member.id) + it "has a private message button" do + expect(page).to have_link "Send message", href: new_notification_path(recipient_id: other_member.id) + end + it { expect(page).not_to have_link "Edit profile", href: edit_member_registration_path } end - it { expect(page).not_to have_link "Edit profile", href: edit_member_registration_path } end end end diff --git a/spec/features/notifications_spec.rb b/spec/features/notifications_spec.rb index dc149c7d9..1ab31dc30 100644 --- a/spec/features/notifications_spec.rb +++ b/spec/features/notifications_spec.rb @@ -4,59 +4,60 @@ describe "Notifications", :js do let(:sender) { create :member } let(:recipient) { create :member, login_name: 'beyonce' } - context "On existing notification" do - let!(:notification) do - create :notification, - sender: sender, - recipient: recipient, - body: "Notification body", - post_id: nil + include_context 'signed in member' do + let(:member) { recipient } + + context "On existing notification" do + let!(:notification) do + create :notification, + sender: sender, + recipient: recipient, + body: "Notification body", + post_id: nil + end + before do + visit root_path + click_link 'Your Stuff' + Percy.snapshot(page, name: "notification menu") + visit notification_path(notification) + Percy.snapshot(page, name: "notifications#show") + end + + it "Replying to the notification" do + click_link "Reply" + expect(page).to have_content "Notification body" + Percy.snapshot(page, name: 'Replying to notification') + + fill_in 'notification_body', with: "Response body" + Percy.snapshot(page, name: "notifications#new") + click_button "Send" + + expect(page).to have_content "Message was successfully sent" + end end - before do - login_as recipient - visit root_path - click_link 'Your Stuff' - Percy.snapshot(page, name: "notification menu") - visit notification_path(notification) - Percy.snapshot(page, name: "notifications#show") - end + describe 'pagination' do + before do + FactoryBot.create_list :notification, 34, recipient: recipient + visit notifications_path + end - it "Replying to the notification" do - click_link "Reply" - expect(page).to have_content "Notification body" - Percy.snapshot(page, name: 'Replying to notification') + it do + Percy.snapshot(page, name: "notifications#index") + end - fill_in 'notification_body', with: "Response body" - Percy.snapshot(page, name: "notifications#new") - click_button "Send" + it 'has page navigation' do + expect(page).to have_selector 'a[rel="next"]' + end - expect(page).to have_content "Message was successfully sent" - end - end + it 'paginates at 30 notifications per page' do + expect(page).to have_selector '.message', count: 30 + end - describe 'pagination' do - before do - FactoryBot.create_list :notification, 34, recipient: recipient - login_as recipient - visit notifications_path - end - - it do - Percy.snapshot(page, name: "notifications#index") - end - - it 'has page navigation' do - expect(page).to have_selector 'a[rel="next"]' - end - - it 'paginates at 30 notifications per page' do - expect(page).to have_selector '.message', count: 30 - end - - it 'navigates pages' do - first('a[rel="next"]').click - expect(page).to have_selector '.message', count: 4 + it 'navigates pages' do + first('a[rel="next"]').click + expect(page).to have_selector '.message', count: 4 + end end end end diff --git a/spec/features/percy/percy_spec.rb b/spec/features/percy/percy_spec.rb index 7b614d169..a5968742f 100644 --- a/spec/features/percy/percy_spec.rb +++ b/spec/features/percy/percy_spec.rb @@ -235,152 +235,154 @@ rest of the garden. context 'when signed in' do let(:prefix) { 'signed-in' } - before { login_as member } - include_examples 'visit pages' + include_context 'signed in member' do + include_examples 'visit pages' - it 'load my plantings#show' do - planting = FactoryBot.create :planting, crop: tomato, owner: member, garden: member.gardens.first - visit planting_path(planting) - Percy.snapshot(page, name: "#{prefix}/self/plantings#show") - end - - it 'load my members#show' do - visit member_path(member) - Percy.snapshot(page, name: "#{prefix}/self/members#show") - end - - it 'load my gardens#show' do - garden = FactoryBot.create :garden, name: 'paradise', owner: member - visit garden_path(garden) - Percy.snapshot(page, name: "#{prefix}/self/gardens#show") - end - - describe '#new' do - it 'crops#new' do - visit new_crop_path - Percy.snapshot(page, name: "#{prefix}/crops#new") + it 'load my plantings#show' do + planting = FactoryBot.create :planting, crop: tomato, owner: member, garden: member.gardens.first + visit planting_path(planting) + Percy.snapshot(page, name: "#{prefix}/self/plantings#show") end - it 'gardens#new' do - visit new_garden_path - Percy.snapshot(page, name: "#{prefix}/gardens#new") + it 'load my members#show' do + visit member_path(member) + Percy.snapshot(page, name: "#{prefix}/self/members#show") end - it 'harvests#new' do - visit new_harvest_path - Percy.snapshot(page, name: "#{prefix}/harvests#new") - fill_in(id: 'crop', with: 'tom') - Percy.snapshot(page, name: "#{prefix}/harvests#new-autosuggest") + it 'load my gardens#show' do + garden = FactoryBot.create :garden, name: 'paradise', owner: member + visit garden_path(garden) + Percy.snapshot(page, name: "#{prefix}/self/gardens#show") end - it 'plantings#new' do - visit new_planting_path - Percy.snapshot(page, name: "#{prefix}/plantings#new") - fill_in(id: 'crop', with: 'tom') - Percy.snapshot(page, name: "#{prefix}/plantings#new-autosuggest") + describe '#new' do + it 'crops#new' do + visit new_crop_path + Percy.snapshot(page, name: "#{prefix}/crops#new") + end + + it 'gardens#new' do + visit new_garden_path + Percy.snapshot(page, name: "#{prefix}/gardens#new") + end + + it 'harvests#new' do + visit new_harvest_path + Percy.snapshot(page, name: "#{prefix}/harvests#new") + fill_in(id: 'crop', with: 'tom') + Percy.snapshot(page, name: "#{prefix}/harvests#new-autosuggest") + end + + it 'plantings#new' do + visit new_planting_path + Percy.snapshot(page, name: "#{prefix}/plantings#new") + fill_in(id: 'crop', with: 'tom') + Percy.snapshot(page, name: "#{prefix}/plantings#new-autosuggest") + end + + it 'seeds#new' do + visit new_seed_path + Percy.snapshot(page, name: "#{prefix}/seeds#new") + fill_in(id: 'crop', with: 'tom') + Percy.snapshot(page, name: "#{prefix}/seeds#new-autosuggest") + end + + it 'posts#new' do + visit new_post_path + Percy.snapshot(page, name: "#{prefix}/posts#new") + end end - it 'seeds#new' do - visit new_seed_path - Percy.snapshot(page, name: "#{prefix}/seeds#new") - fill_in(id: 'crop', with: 'tom') - Percy.snapshot(page, name: "#{prefix}/seeds#new-autosuggest") + describe '#edit' do + it 'loads gardens#edit' do + garden = FactoryBot.create :garden, owner: member + visit edit_garden_path(garden) + Percy.snapshot(page, name: "#{prefix}/gardens#edit") + end + + it 'loads harvests#edit' do + harvest = FactoryBot.create :harvest, owner: member + visit edit_harvest_path(harvest) + Percy.snapshot(page, name: "#{prefix}/harvests#edit") + end + + it 'loads planting#edit' do + planting = FactoryBot.create :planting, owner: member + visit edit_planting_path(planting) + Percy.snapshot(page, name: "#{prefix}/plantings#edit") + end + + it 'loads posts#edit' do + visit edit_post_path(post) + Percy.snapshot(page, name: "#{prefix}/posts#edit") + end + + it 'comments#new' do + visit new_comment_path(post_id: post.id) + Percy.snapshot(page, name: "comments#new") + end end - it 'posts#new' do - visit new_post_path - Percy.snapshot(page, name: "#{prefix}/posts#new") - end - end - - describe '#edit' do - it 'loads gardens#edit' do - garden = FactoryBot.create :garden, owner: member - visit edit_garden_path(garden) - Percy.snapshot(page, name: "#{prefix}/gardens#edit") - end - - it 'loads harvests#edit' do - harvest = FactoryBot.create :harvest, owner: member - visit edit_harvest_path(harvest) - Percy.snapshot(page, name: "#{prefix}/harvests#edit") - end - - it 'loads planting#edit' do - planting = FactoryBot.create :planting, owner: member - visit edit_planting_path(planting) - Percy.snapshot(page, name: "#{prefix}/plantings#edit") - end - - it 'loads posts#edit' do - visit edit_post_path(post) - Percy.snapshot(page, name: "#{prefix}/posts#edit") - end - - it 'comments#new' do - visit new_comment_path(post_id: post.id) - Percy.snapshot(page, name: "comments#new") - end - end - - describe 'expand menus' do - it 'expands crop menu' do - visit root_path - click_on 'Crops' - Percy.snapshot(page, name: "#{prefix}/crops-menu") - click_on 'Community' - Percy.snapshot(page, name: "#{prefix}/community-menu") - click_on 'percy' - Percy.snapshot(page, name: "#{prefix}/member-menu") + describe 'expand menus' do + it 'expands crop menu' do + visit root_path + click_on 'Crops' + Percy.snapshot(page, name: "#{prefix}/crops-menu") + click_on 'Community' + Percy.snapshot(page, name: "#{prefix}/community-menu") + click_on 'percy' + Percy.snapshot(page, name: "#{prefix}/member-menu") + end end end end context 'wrangling crops' do - let(:prefix) { 'crop-wrangler' } - before { login_as crop_wrangler } - let!(:candy) { FactoryBot.create :crop_request, name: 'candy' } + include_context 'signed in crop wrangler' do + let(:prefix) { 'crop-wrangler' } + let!(:candy) { FactoryBot.create :crop_request, name: 'candy' } - it 'crop wrangling page' do - visit wrangle_crops_path - Percy.snapshot(page, name: 'crops wrangle') - click_link 'Pending approval' - Percy.snapshot(page, name: 'crops pending approval') - click_link 'candy' - Percy.snapshot(page, name: 'editing pending crop') + it 'crop wrangling page' do + visit wrangle_crops_path + Percy.snapshot(page, name: 'crops wrangle') + click_link 'Pending approval' + Percy.snapshot(page, name: 'crops pending approval') + click_link 'candy' + Percy.snapshot(page, name: 'editing pending crop') + end end end + context 'admin' do - before do - login_as admin_user - visit admin_path - end - it 'admin page' do - Percy.snapshot(page, name: 'Admin') - end - it 'Roles' do - click_link 'Roles' - Percy.snapshot(page, name: 'Admin Roles') - end - it 'CMS' do - click_link 'CMS' - Percy.snapshot(page, name: 'CMS') - end - it 'Garden Types' do - click_link 'Garden Types' - Percy.snapshot(page, name: 'Admin Garden type') - end - it 'Alternate names' do - click_link 'Alternate names' - Percy.snapshot(page, name: 'Admin Alternate names') - end - it 'Scientific names' do - click_link 'Scientific names' - Percy.snapshot(page, name: 'Admin Scientific names') - end - it 'Members' do - click_link 'Members' - Percy.snapshot(page, name: 'Admin Members') + include_context 'signed in admin' do + before { visit admin_path } + it 'admin page' do + Percy.snapshot(page, name: 'Admin') + end + it 'Roles' do + click_link 'Roles' + Percy.snapshot(page, name: 'Admin Roles') + end + it 'CMS' do + click_link 'CMS' + Percy.snapshot(page, name: 'CMS') + end + it 'Garden Types' do + click_link 'Garden Types' + Percy.snapshot(page, name: 'Admin Garden type') + end + it 'Alternate names' do + click_link 'Alternate names' + Percy.snapshot(page, name: 'Admin Alternate names') + end + it 'Scientific names' do + click_link 'Scientific names' + Percy.snapshot(page, name: 'Admin Scientific names') + end + it 'Members' do + click_link 'Members' + Percy.snapshot(page, name: 'Admin Members') + end end end end diff --git a/spec/features/photos/new_photo_spec.rb b/spec/features/photos/new_photo_spec.rb index de0ae5664..4bf64065e 100644 --- a/spec/features/photos/new_photo_spec.rb +++ b/spec/features/photos/new_photo_spec.rb @@ -6,56 +6,56 @@ describe "new photo page" do context "signed in member" do let(:member) { FactoryBot.create :member } - before { login_as member } + include_context 'signed in member' do + context "viewing a planting" do + let(:planting) { FactoryBot.create :planting, owner: member } - context "viewing a planting" do - let(:planting) { FactoryBot.create :planting, owner: member } - - it "add photo" do - visit planting_path(planting) - click_link 'Actions' - within '.planting-actions' do - click_link('Add photo') + it "add photo" do + visit planting_path(planting) + click_link 'Actions' + within '.planting-actions' do + click_link('Add photo') + end + expect(page).to have_text planting.crop.name + Percy.snapshot(page, name: 'Add photo to planting') end - expect(page).to have_text planting.crop.name - Percy.snapshot(page, name: 'Add photo to planting') end - end - context "viewing a harvest" do - let(:harvest) { FactoryBot.create :harvest, owner: member } + context "viewing a harvest" do + let(:harvest) { FactoryBot.create :harvest, owner: member } - it "add photo" do - visit harvest_path(harvest) - click_link 'Actions' - within '.harvest-actions' do - click_link "Add photo" + it "add photo" do + visit harvest_path(harvest) + click_link 'Actions' + within '.harvest-actions' do + click_link "Add photo" + end + expect(page).to have_text harvest.crop.name end - expect(page).to have_text harvest.crop.name end - end - context "viewing a garden" do - let(:garden) { FactoryBot.create :garden, owner: member } + context "viewing a garden" do + let(:garden) { FactoryBot.create :garden, owner: member } - it "add photo" do - visit garden_path(garden) - click_link 'Actions' - within '.garden-actions' do - click_link "Add photo" + it "add photo" do + visit garden_path(garden) + click_link 'Actions' + within '.garden-actions' do + click_link "Add photo" + end + expect(page).to have_text garden.name end - expect(page).to have_text garden.name end - end - describe "viewing a seed" do - let(:seed) { FactoryBot.create :seed, owner: member } + describe "viewing a seed" do + let(:seed) { FactoryBot.create :seed, owner: member } - it "add photo" do - visit seed_path(seed) - click_link 'Actions' - first('.seed-actions').click_link('Add photo') - expect(page).to have_text seed.to_s + it "add photo" do + visit seed_path(seed) + click_link 'Actions' + first('.seed-actions').click_link('Add photo') + expect(page).to have_text seed.to_s + end end end end diff --git a/spec/features/photos/show_photo_spec.rb b/spec/features/photos/show_photo_spec.rb index 2c027d57d..664b24aee 100644 --- a/spec/features/photos/show_photo_spec.rb +++ b/spec/features/photos/show_photo_spec.rb @@ -4,68 +4,68 @@ describe "show photo page" do context "signed in member" do let(:member) { create :member } - before { login_as member } + include_context 'signed in member' do + context "linked to planting" do + let(:planting) { create :planting } + let(:photo) { create :photo, owner: planting.owner } - context "linked to planting" do - let(:planting) { create :planting } - let(:photo) { create :photo, owner: planting.owner } + context "shows linkback to planting" do + before do + planting.photos << photo + visit photo_path(photo) + end - context "shows linkback to planting" do - before do - planting.photos << photo - visit photo_path(photo) + it { + expect(page).to have_link "#{planting.crop.name} planting in #{planting.garden.name} by #{planting.owner}", + href: planting_path(planting) + } + it { expect(page).to have_link planting.crop.name } end - - it { - expect(page).to have_link "#{planting.crop.name} planting in #{planting.garden.name} by #{planting.owner}", - href: planting_path(planting) - } - it { expect(page).to have_link planting.crop.name } end - end - context "linked to harvest" do - let(:photo) { create :photo, owner: harvest.owner } - let(:harvest) { create :harvest } + context "linked to harvest" do + let(:photo) { create :photo, owner: harvest.owner } + let(:harvest) { create :harvest } - context "shows linkback to harvest" do - before do - harvest.photos << photo - visit photo_path(photo) + context "shows linkback to harvest" do + before do + harvest.photos << photo + visit photo_path(photo) + end + + it { expect(page).to have_link "#{harvest.crop.name} harvest by #{harvest.owner}", href: harvest_path(harvest) } + it { expect(page).to have_link harvest.crop.name } end - - it { expect(page).to have_link "#{harvest.crop.name} harvest by #{harvest.owner}", href: harvest_path(harvest) } - it { expect(page).to have_link harvest.crop.name } end - end - context "linked to garden" do - let(:photo) { create :photo, owner: garden.owner } - let(:garden) { create :garden } + context "linked to garden" do + let(:photo) { create :photo, owner: garden.owner } + let(:garden) { create :garden } - context "shows linkback to garden" do - before do - garden.photos << photo - visit photo_path(photo) - Percy.snapshot(page, name: 'Show photo of a garden') + context "shows linkback to garden" do + before do + garden.photos << photo + visit photo_path(photo) + Percy.snapshot(page, name: 'Show photo of a garden') + end + + it { expect(page).to have_link "garden named \"#{garden.name}\" by #{garden.owner}", href: garden_path(garden) } end - - it { expect(page).to have_link "garden named \"#{garden.name}\" by #{garden.owner}", href: garden_path(garden) } end - end - context "linked to seed" do - let(:photo) { create :photo, owner: seed.owner } - let(:seed) { create :seed } + context "linked to seed" do + let(:photo) { create :photo, owner: seed.owner } + let(:seed) { create :seed } - context "shows linkback to seed" do - before do - seed.photos << photo - visit photo_path(photo) + context "shows linkback to seed" do + before do + seed.photos << photo + visit photo_path(photo) + end + + it { expect(page).to have_link "#{seed.crop.name} seeds belonging to #{seed.owner}", href: seed_path(seed) } + it { expect(page).to have_link seed.crop.name } end - - it { expect(page).to have_link "#{seed.crop.name} seeds belonging to #{seed.owner}", href: seed_path(seed) } - it { expect(page).to have_link seed.crop.name } end end end diff --git a/spec/features/places/searching_a_place_spec.rb b/spec/features/places/searching_a_place_spec.rb index fa64ea568..0a5ba110a 100644 --- a/spec/features/places/searching_a_place_spec.rb +++ b/spec/features/places/searching_a_place_spec.rb @@ -26,32 +26,33 @@ describe "User searches" do end describe "Nearby plantings, seed, and members" do - before do - login_as member - visit places_path - search_with "Philippines" - end + include_context 'signed in member' do + before do + visit places_path + search_with "Philippines" + end - it "shows that there are nearby seeds, plantings, and members" do - expect(page).to have_content "Nearby members" - expect(page).to have_content "Seeds available for trade near Philippines" - expect(page).to have_content "Recent plantings near Philippines" - Percy.snapshot(page, name: 'places map') - end + it "shows that there are nearby seeds, plantings, and members" do + expect(page).to have_content "Nearby members" + expect(page).to have_content "Seeds available for trade near Philippines" + expect(page).to have_content "Recent plantings near Philippines" + Percy.snapshot(page, name: 'places map') + end - it "goes to members' index page" do - click_link 'View all members >>' - expect(current_path).to eq members_path - end + it "goes to members' index page" do + click_link 'View all members >>' + expect(current_path).to eq members_path + end - it "goes to plantings' index page" do - click_link 'View all plantings >>' - expect(current_path).to eq plantings_path - end + it "goes to plantings' index page" do + click_link 'View all plantings >>' + expect(current_path).to eq plantings_path + end - it "goes to seeds' index page" do - click_link 'View all seeds >>' - expect(current_path).to eq seeds_path + it "goes to seeds' index page" do + click_link 'View all seeds >>' + expect(current_path).to eq seeds_path + end end end diff --git a/spec/features/plantings/planting_a_crop_spec.rb b/spec/features/plantings/planting_a_crop_spec.rb index 24925b073..8ff4b2d96 100644 --- a/spec/features/plantings/planting_a_crop_spec.rb +++ b/spec/features/plantings/planting_a_crop_spec.rb @@ -8,273 +8,270 @@ describe "Planting a crop", :js, :elasticsearch do let!(:planting) do FactoryBot.create :planting, garden: garden, owner: member, planted_at: Date.parse("2013-03-10") end + include_context 'signed in member' do + before { visit new_planting_path } - before do - login_as member - visit new_planting_path - end + it_behaves_like "crop suggest", "planting" - it_behaves_like "crop suggest", "planting" + it "has the required fields help text" do + expect(page).to have_content "* denotes a required field" + end - it "has the required fields help text" do - expect(page).to have_content "* denotes a required field" - end + describe "displays required and optional fields properly" do + it { expect(page).to have_selector ".required", text: "What did you plant?" } + it { expect(page).to have_selector ".required", text: "Where did you plant it?" } + it { expect(page).to have_selector 'input#planting_planted_at' } + it { expect(page).to have_selector 'input#planting_quantity' } + it { expect(page).to have_selector 'select#planting_planted_from' } + it { expect(page).to have_selector 'select#planting_sunniness' } + it { expect(page).to have_selector 'textarea#planting_description' } + it { expect(page).to have_selector 'input#planting_finished_at' } + end - describe "displays required and optional fields properly" do - it { expect(page).to have_selector ".required", text: "What did you plant?" } - it { expect(page).to have_selector ".required", text: "Where did you plant it?" } - it { expect(page).to have_selector 'input#planting_planted_at' } - it { expect(page).to have_selector 'input#planting_quantity' } - it { expect(page).to have_selector 'select#planting_planted_from' } - it { expect(page).to have_selector 'select#planting_sunniness' } - it { expect(page).to have_selector 'textarea#planting_description' } - it { expect(page).to have_selector 'input#planting_finished_at' } - end + describe "Creating a new planting" do + before do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "semi-shade", from: "Sun or shade?" + fill_in "Tell us more about it", with: "It's rad." + choose 'Garden' + fill_in "When", with: "2014-06-15" + click_button "Save" + end + end - describe "Creating a new planting" do - before do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "semi-shade", from: "Sun or shade?" - fill_in "Tell us more about it", with: "It's rad." - choose 'Garden' - fill_in "When", with: "2014-06-15" - click_button "Save" + it { expect(page).to have_content "planting was successfully created" } + end + + describe "Clicking link to owner's profile" do + before do + visit member_plantings_path(member) + click_link "View #{member}'s profile >>" + end + it { expect(current_path).to eq member_path(member) } + end + + describe "Progress bar status on planting creation" do + before do + visit new_planting_path + + @a_past_date = 15.days.ago.strftime("%Y-%m-%d") + @right_now = Time.zone.today.strftime("%Y-%m-%d") + @a_future_date = 1.year.from_now.strftime("%Y-%m-%d") + end + + it "shows that it is not planted yet" do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + choose 'Garden' + fill_in "When", with: @a_future_date + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "semi-shade", from: "Sun or shade?" + fill_in "Tell us more about it", with: "It's rad." + click_button "Save" + end + + expect(page).to have_content "planting was successfully created" + expect(page).to have_content "0%" + end + + it "shows that days before maturity is unknown" do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + choose 'Garden' + fill_in "When", with: @a_past_date + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "semi-shade", from: "Sun or shade?" + fill_in "Tell us more about it", with: "It's rad." + click_button "Save" + end + + expect(page).to have_content "planting was successfully created" + expect(page).not_to have_content "Finished" + expect(page).not_to have_content "Finishes" + end + + it "shows that planting is in progress" do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + choose 'Garden' + fill_in "When", with: @right_now + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "semi-shade", from: "Sun or shade?" + fill_in "When?", with: '2013-03-10' + fill_in "Tell us more about it", with: "It's rad." + fill_in "Finished date", with: @a_future_date + click_button "Save" + end + + expect(page).to have_content "planting was successfully created" + expect(page).not_to have_content "0%" + expect(page).not_to have_content "Finished" + expect(page).not_to have_content "Finishes" + end + + it "shows that planting is 100% complete (no date specified)" do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + choose 'Garden' + fill_in "When", with: @right_now + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "semi-shade", from: "Sun or shade?" + fill_in "Tell us more about it", with: "It's rad." + check "Mark as finished" + click_button "Save" + end + + expect(page).to have_content "planting was successfully created" + expect(page).to have_content "Finished" + end + + it "shows that planting is 100% complete (date specified)" do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + choose 'Garden' + fill_in "When", with: @a_past_date + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "semi-shade", from: "Sun or shade?" + choose 'Garden' + fill_in "Tell us more about it", with: "It's rad." + fill_in "Finished date", with: @right_now + click_button "Save" + end + + expect(page).to have_content "planting was successfully created" + expect(page).to have_content "Finished" end end - it { expect(page).to have_content "planting was successfully created" } - end - - describe "Clicking link to owner's profile" do - before do - visit member_plantings_path(member) - click_link "View #{member}'s profile >>" - end - it { expect(current_path).to eq member_path(member) } - end - - describe "Progress bar status on planting creation" do - before do - login_as member - visit new_planting_path - - @a_past_date = 15.days.ago.strftime("%Y-%m-%d") - @right_now = Time.zone.today.strftime("%Y-%m-%d") - @a_future_date = 1.year.from_now.strftime("%Y-%m-%d") - end - - it "shows that it is not planted yet" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" + it "Planting from crop page" do + visit crop_path(maize) + within '.crop-actions' do + click_link "Plant maize" + end within "form#new_planting" do - choose 'Garden' - fill_in "When", with: @a_future_date - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "semi-shade", from: "Sun or shade?" - fill_in "Tell us more about it", with: "It's rad." - click_button "Save" + expect(page).to have_selector "input[value='maize']" end - expect(page).to have_content "planting was successfully created" - expect(page).to have_content "0%" - end - - it "shows that days before maturity is unknown" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - choose 'Garden' - fill_in "When", with: @a_past_date - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "semi-shade", from: "Sun or shade?" - fill_in "Tell us more about it", with: "It's rad." - click_button "Save" - end - - expect(page).to have_content "planting was successfully created" - expect(page).not_to have_content "Finished" - expect(page).not_to have_content "Finishes" - end - - it "shows that planting is in progress" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - choose 'Garden' - fill_in "When", with: @right_now - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "semi-shade", from: "Sun or shade?" - fill_in "When?", with: '2013-03-10' - fill_in "Tell us more about it", with: "It's rad." - fill_in "Finished date", with: @a_future_date - click_button "Save" - end - - expect(page).to have_content "planting was successfully created" - expect(page).not_to have_content "0%" - expect(page).not_to have_content "Finished" - expect(page).not_to have_content "Finishes" - end - - it "shows that planting is 100% complete (no date specified)" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - choose 'Garden' - fill_in "When", with: @right_now - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "semi-shade", from: "Sun or shade?" - fill_in "Tell us more about it", with: "It's rad." - check "Mark as finished" - click_button "Save" - end - - expect(page).to have_content "planting was successfully created" - expect(page).to have_content "Finished" - end - - it "shows that planting is 100% complete (date specified)" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - choose 'Garden' - fill_in "When", with: @a_past_date - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "semi-shade", from: "Sun or shade?" - choose 'Garden' - fill_in "Tell us more about it", with: "It's rad." - fill_in "Finished date", with: @right_now - click_button "Save" - end - - expect(page).to have_content "planting was successfully created" - expect(page).to have_content "Finished" - end - end - - it "Planting from crop page" do - visit crop_path(maize) - within '.crop-actions' do - click_link "Plant maize" - end - within "form#new_planting" do - expect(page).to have_selector "input[value='maize']" - end - - choose(member.gardens.first.name) - click_button "Save" - - expect(page).to have_content "planting was successfully created" - expect(page).to have_content "maize" - end - - it "Editing a planting to add details" do - visit planting_path(planting) - click_link 'Actions' - click_link "Edit" - fill_in "Tell us more about it", with: "Some extra notes" - click_button "Save" - expect(page).to have_content "planting was successfully updated" - end - - it "Editing a planting to fill in the finished date" do - visit planting_path(planting) - expect(page).not_to have_content "Finishes" - # click_link(id: 'planting-actions-button') - click_link 'Actions' - click_link "Edit" - check "finished" - fill_in "Finished date", with: "2015-06-25" - click_button "Save" - expect(page).to have_content "planting was successfully updated" - expect(page).to have_content "Finished" - end - - it "Marking a planting as finished" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - choose(member.gardens.first.name) - within "form#new_planting" do - fill_in "When?", with: "2014-07-01" - check "Mark as finished" - fill_in "Finished date", with: "2014-08-30" - uncheck 'Mark as finished' - end - - # Javascript removes the finished at date when the - # planting is marked unfinished. - expect(find("#planting_finished_at").value).to eq("") - - within "form#new_planting" do - check 'Mark as finished' - end - - # The finished at date was cached in Javascript in - # case the user clicks unfinished accidentally. - expect(find("#planting_finished_at").value).to eq("2014-08-30") - - within "form#new_planting" do + choose(member.gardens.first.name) click_button "Save" + + expect(page).to have_content "planting was successfully created" + expect(page).to have_content "maize" end - expect(page).to have_content "planting was successfully created" - expect(page).to have_content "Finished" - expect(page).to have_content "30 Aug" - # shouldn't be on the page - visit plantings_path - expect(page).not_to have_content "maize" + it "Editing a planting to add details" do + visit planting_path(planting) + click_link 'Actions' + click_link "Edit" + fill_in "Tell us more about it", with: "Some extra notes" + click_button "Save" + expect(page).to have_content "planting was successfully updated" + end - # show all plantings to see this finished planting - visit plantings_path(all: 1) - expect(page).to have_content "maize" - end + it "Editing a planting to fill in the finished date" do + visit planting_path(planting) + expect(page).not_to have_content "Finishes" + # click_link(id: 'planting-actions-button') + click_link 'Actions' + click_link "Edit" + check "finished" + fill_in "Finished date", with: "2015-06-25" + click_button "Save" + expect(page).to have_content "planting was successfully updated" + expect(page).to have_content "Finished" + end - describe "Marking a planting as finished without a date" do - before do + it "Marking a planting as finished" do fill_autocomplete "crop", with: "mai" select_from_autocomplete "maize" + choose(member.gardens.first.name) within "form#new_planting" do - choose member.gardens.first.name + fill_in "When?", with: "2014-07-01" check "Mark as finished" + fill_in "Finished date", with: "2014-08-30" + uncheck 'Mark as finished' + end + + # Javascript removes the finished at date when the + # planting is marked unfinished. + expect(find("#planting_finished_at").value).to eq("") + + within "form#new_planting" do + check 'Mark as finished' + end + + # The finished at date was cached in Javascript in + # case the user clicks unfinished accidentally. + expect(find("#planting_finished_at").value).to eq("2014-08-30") + + within "form#new_planting" do click_button "Save" end + expect(page).to have_content "planting was successfully created" + expect(page).to have_content "Finished" + expect(page).to have_content "30 Aug" + + # shouldn't be on the page + visit plantings_path + expect(page).not_to have_content "maize" + + # show all plantings to see this finished planting + visit plantings_path(all: 1) + expect(page).to have_content "maize" + end + + describe "Marking a planting as finished without a date" do + before do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + choose member.gardens.first.name + check "Mark as finished" + click_button "Save" + end + end + + it { expect(page).to have_content "planting was successfully created" } + it { expect(page).to have_content "Finished" } + end + + describe "Planting sunniness" do + before "shows the a sunny image" do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", with: "2015-10-15" + fill_in "How many?", with: 42 + select "cutting", from: "Planted from" + select "sun", from: "Sun or shade?" + fill_in "Tell us more about it", with: "It's rad." + check "Mark as finished" + click_button "Save" + end + + it { expect(page).to have_css("img[alt='sun']") } + end end - it { expect(page).to have_content "planting was successfully created" } - it { expect(page).to have_content "Finished" } - end + describe "Marking a planting as finished from the show page" do + let(:path) { planting_path(planting) } + let(:link_text) { "Mark as finished" } - describe "Planting sunniness" do - before "shows the a sunny image" do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - fill_in "When", with: "2015-10-15" - fill_in "How many?", with: 42 - select "cutting", from: "Planted from" - select "sun", from: "Sun or shade?" - fill_in "Tell us more about it", with: "It's rad." - check "Mark as finished" - click_button "Save" - end - - it { expect(page).to have_css("img[alt='sun']") } + it_behaves_like "append date" end end - - describe "Marking a planting as finished from the show page" do - let(:path) { planting_path(planting) } - let(:link_text) { "Mark as finished" } - - it_behaves_like "append date" - end end diff --git a/spec/features/posts/posting_a_post_spec.rb b/spec/features/posts/posting_a_post_spec.rb index 7467517c5..cde48b467 100644 --- a/spec/features/posts/posting_a_post_spec.rb +++ b/spec/features/posts/posting_a_post_spec.rb @@ -1,33 +1,30 @@ require 'rails_helper' describe 'Post a post' do - let(:member) { create :member } + include_context 'signed in member' do + before { visit new_post_path } - before do - login_as member - visit new_post_path - end - - it "creating a post" do - fill_in "post_subject", with: "Testing" - fill_in "post_body", with: "This is a sample test" - click_button "Post" - expect(page).to have_content "Post was successfully created" - expect(page).to have_content "Posted by" - end - - context "editing a post" do - let(:existing_post) { create :post, author: member } - - before do - visit edit_post_path(existing_post) + it "creating a post" do + fill_in "post_subject", with: "Testing" + fill_in "post_body", with: "This is a sample test" + click_button "Post" + expect(page).to have_content "Post was successfully created" + expect(page).to have_content "Posted by" end - it "saving edit" do - fill_in "post_subject", with: "Testing Edit" - click_button "Post" - expect(page).to have_content "Post was successfully updated" - expect(page).to have_content "edited at" + context "editing a post" do + let(:existing_post) { create :post, author: member } + + before do + visit edit_post_path(existing_post) + end + + it "saving edit" do + fill_in "post_subject", with: "Testing Edit" + click_button "Post" + expect(page).to have_content "Post was successfully updated" + expect(page).to have_content "edited at" + end end end end diff --git a/spec/features/seeds/adding_seeds_spec.rb b/spec/features/seeds/adding_seeds_spec.rb index ce5ab31b4..cc29ef027 100644 --- a/spec/features/seeds/adding_seeds_spec.rb +++ b/spec/features/seeds/adding_seeds_spec.rb @@ -2,71 +2,69 @@ require 'rails_helper' require 'custom_matchers' describe "Seeds", :js, :elasticsearch do - let(:member) { create :member } - let!(:maize) { create :maize } + include_context 'signed in member' do + let!(:maize) { create :maize } - before do - login_as member - visit new_seed_path - end + before { visit new_seed_path } - it_behaves_like "crop suggest", "seed", "crop" + it_behaves_like "crop suggest", "seed", "crop" - it "has the required fields help text" do - expect(page).to have_content "* denotes a required field" - end - - describe "displays required and optional fields properly" do - it { expect(page).to have_selector ".form-group.required", text: "Crop" } - it { expect(page).to have_selector 'input#seed_quantity' } - it { expect(page).to have_selector 'input#seed_plant_before' } - it { expect(page).to have_selector 'input#seed_days_until_maturity_min' } - it { expect(page).to have_selector 'input#seed_days_until_maturity_max' } - it { expect(page).to have_selector '.form-group.required', text: 'Organic?' } - it { expect(page).to have_selector '.form-group.required', text: 'GMO?' } - it { expect(page).to have_selector '.form-group.required', text: 'Heirloom?' } - it { expect(page).to have_selector 'textarea#seed_description' } - it { expect(page).to have_selector '.form-group.required', text: 'Will trade' } - end - - describe "Adding a new seed", js: true do - before do - fill_autocomplete "crop", with: "mai" - select_from_autocomplete "maize" - within "form#new_seed" do - fill_in "Quantity", with: 42 - fill_in "Plant before", with: "2014-06-15" - fill_in "min", with: 999 - fill_in "max", with: 1999 - select "certified organic", from: "Organic?" - select "non-certified GMO-free", from: "GMO?" - select "heirloom", from: "Heirloom?" - fill_in "Description", with: "It's killer." - select "internationally", from: "Will trade" - click_button "Save" - end + it "has the required fields help text" do + expect(page).to have_content "* denotes a required field" end - it { expect(page).to have_content "Successfully added maize seed to your stash" } - it { expect(page).to have_content "Quantity: 42" } - it { expect(page).to have_content "Days until maturity: 999–1999" } - it { expect(page).to have_content "certified organic" } - it { expect(page).to have_content "non-certified GMO-free" } - it { expect(page).to have_content "Heirloom? heirloom" } - it { expect(page).to have_content "It's killer." } - end - - describe "Adding a seed from crop page" do - before do - visit crop_path(maize) - click_link "Add maize seeds to stash" - within "form#new_seed" do - expect(page).to have_selector "input[value='maize']" - click_button "Save" - end + describe "displays required and optional fields properly" do + it { expect(page).to have_selector ".form-group.required", text: "Crop" } + it { expect(page).to have_selector 'input#seed_quantity' } + it { expect(page).to have_selector 'input#seed_plant_before' } + it { expect(page).to have_selector 'input#seed_days_until_maturity_min' } + it { expect(page).to have_selector 'input#seed_days_until_maturity_max' } + it { expect(page).to have_selector '.form-group.required', text: 'Organic?' } + it { expect(page).to have_selector '.form-group.required', text: 'GMO?' } + it { expect(page).to have_selector '.form-group.required', text: 'Heirloom?' } + it { expect(page).to have_selector 'textarea#seed_description' } + it { expect(page).to have_selector '.form-group.required', text: 'Will trade' } end - it { expect(page).to have_content "Successfully added maize seed to your stash" } - it { expect(page).to have_content "maize" } + describe "Adding a new seed", js: true do + before do + fill_autocomplete "crop", with: "mai" + select_from_autocomplete "maize" + within "form#new_seed" do + fill_in "Quantity", with: 42 + fill_in "Plant before", with: "2014-06-15" + fill_in "min", with: 999 + fill_in "max", with: 1999 + select "certified organic", from: "Organic?" + select "non-certified GMO-free", from: "GMO?" + select "heirloom", from: "Heirloom?" + fill_in "Description", with: "It's killer." + select "internationally", from: "Will trade" + click_button "Save" + end + end + + it { expect(page).to have_content "Successfully added maize seed to your stash" } + it { expect(page).to have_content "Quantity: 42" } + it { expect(page).to have_content "Days until maturity: 999–1999" } + it { expect(page).to have_content "certified organic" } + it { expect(page).to have_content "non-certified GMO-free" } + it { expect(page).to have_content "Heirloom? heirloom" } + it { expect(page).to have_content "It's killer." } + end + + describe "Adding a seed from crop page" do + before do + visit crop_path(maize) + click_link "Add maize seeds to stash" + within "form#new_seed" do + expect(page).to have_selector "input[value='maize']" + click_button "Save" + end + end + + it { expect(page).to have_content "Successfully added maize seed to your stash" } + it { expect(page).to have_content "maize" } + end end end diff --git a/spec/features/seeds/misc_seeds_spec.rb b/spec/features/seeds/misc_seeds_spec.rb index 620a07823..467c1acb3 100644 --- a/spec/features/seeds/misc_seeds_spec.rb +++ b/spec/features/seeds/misc_seeds_spec.rb @@ -6,92 +6,92 @@ describe "seeds", js: true do context "signed in user" do let(:crop) { create :crop } - before { login_as member } + include_context 'signed in member' do + xit "button on index to edit seed" do + let!(:seed) { create :seed, owner: member } - xit "button on index to edit seed" do - let!(:seed) { create :seed, owner: member } + before do + visit seeds_path + click_link 'Actions' + click_link "Edit" + end - before do - visit seeds_path - click_link 'Actions' - click_link "Edit" + it { expect(current_path).to eq edit_seed_path(seed) } + it { expect(page).to have_content 'Editing seeds' } end - it { expect(current_path).to eq edit_seed_path(seed) } - it { expect(page).to have_content 'Editing seeds' } - end + describe "button on front page to add seeds" do + before do + visit root_path + click_link(href: new_seed_path) + end - describe "button on front page to add seeds" do - before do - visit root_path - click_link(href: new_seed_path) + it { expect(current_path).to eq new_seed_path } + it { expect(page).to have_content 'Save seeds' } end - it { expect(current_path).to eq new_seed_path } - it { expect(page).to have_content 'Save seeds' } - end + describe "Clicking link to owner's profile" do + before do + visit member_seeds_path(member) + click_link "View #{member}'s profile >>" + end - describe "Clicking link to owner's profile" do - before do - visit member_seeds_path(member) - click_link "View #{member}'s profile >>" + it { expect(current_path).to eq member_path(member) } end - it { expect(current_path).to eq member_path(member) } - end + # actually adding seeds is in spec/features/seeds_new_spec.rb - # actually adding seeds is in spec/features/seeds_new_spec.rb - - it "edit seeds" do - seed = create :seed, owner: member - visit seed_path(seed) - click_link 'Actions' - click_link 'Edit' - expect(current_path).to eq edit_seed_path(seed) - fill_in 'Quantity', with: seed.quantity * 2 - click_button 'Save' - expect(current_path).to eq seed_path(seed) - end - - describe "delete seeds" do - let(:seed) { FactoryBot.create :seed, owner: member } - - before do + it "edit seeds" do + seed = create :seed, owner: member visit seed_path(seed) click_link 'Actions' - accept_confirm do - click_link 'Delete' + click_link 'Edit' + expect(current_path).to eq edit_seed_path(seed) + fill_in 'Quantity', with: seed.quantity * 2 + click_button 'Save' + expect(current_path).to eq seed_path(seed) + end + + describe "delete seeds" do + let(:seed) { FactoryBot.create :seed, owner: member } + + before do + visit seed_path(seed) + click_link 'Actions' + accept_confirm do + click_link 'Delete' + end end + + it { expect(current_path).to eq seeds_path } end - it { expect(current_path).to eq seeds_path } - end + describe '#show' do + before { visit seed_path(seed) } - describe '#show' do - before { visit seed_path(seed) } + describe "view seeds with max and min days until maturity" do + let(:seed) { FactoryBot.create :seed, days_until_maturity_min: 5, days_until_maturity_max: 7 } - describe "view seeds with max and min days until maturity" do - let(:seed) { FactoryBot.create :seed, days_until_maturity_min: 5, days_until_maturity_max: 7 } + it { expect(page).to have_content "Days until maturity: 5–7" } + end - it { expect(page).to have_content "Days until maturity: 5–7" } - end + describe "view seeds with only max days until maturity" do + let(:seed) { FactoryBot.create :seed, days_until_maturity_max: 7 } - describe "view seeds with only max days until maturity" do - let(:seed) { FactoryBot.create :seed, days_until_maturity_max: 7 } + it { expect(page).to have_content "Days until maturity: 7" } + end - it { expect(page).to have_content "Days until maturity: 7" } - end + describe "view seeds with only min days until maturity" do + let(:seed) { FactoryBot.create :seed, days_until_maturity_min: 5 } - describe "view seeds with only min days until maturity" do - let(:seed) { FactoryBot.create :seed, days_until_maturity_min: 5 } + it { expect(page).to have_content "Days until maturity: 5" } + end - it { expect(page).to have_content "Days until maturity: 5" } - end + describe "view seeds with neither max nor min days until maturity" do + let(:seed) { FactoryBot.create :seed } - describe "view seeds with neither max nor min days until maturity" do - let(:seed) { FactoryBot.create :seed } - - it { expect(page).to have_content "Days until maturity: unknown" } + it { expect(page).to have_content "Days until maturity: unknown" } + end end end end diff --git a/spec/features/seeds/seed_photos.rb b/spec/features/seeds/seed_photos.rb index d1eacdd7f..b787e2b87 100644 --- a/spec/features/seeds/seed_photos.rb +++ b/spec/features/seeds/seed_photos.rb @@ -2,45 +2,44 @@ require 'rails_helper' require 'custom_matchers' describe "Seeds", :js do - subject do - login_as member - visit seed_path(seed) - page - end + include_context 'signed in member' do + before { visit seed_path(seed) } + subject { page } - let(:member) { FactoryBot.create :member } - let!(:seed) { FactoryBot.create :seed, owner: member } + let(:member) { FactoryBot.create :member } + let!(:seed) { FactoryBot.create :seed, owner: member } - it { is_expected.to have_content 'Add photo' } + it { is_expected.to have_content 'Add photo' } - # context 'no photos' do - # it { is_expected.to have_content 'no photos' } - # end - context 'has one photo' do - before { seed.photos = [photo] } + # context 'no photos' do + # it { is_expected.to have_content 'no photos' } + # end + context 'has one photo' do + before { seed.photos = [photo] } - let!(:photo) { FactoryBot.create :photo, title: 'hello photo' } + let!(:photo) { FactoryBot.create :photo, title: 'hello photo' } - it { is_expected.to have_xpath("//img[contains(@src,'#{photo.thumbnail_url}')]") } - it { is_expected.to have_xpath("//a[contains(@href,'#{photo_path(photo)}')]") } - end - - context 'has 50 photos' do - before { seed.photos = photos } - - let!(:photos) { FactoryBot.create_list :photo, 50 } - - it "shows newest photo" do - expect(subject).to have_xpath("//img[contains(@src,'#{photos.last.thumbnail_url}')]") + it { is_expected.to have_xpath("//img[contains(@src,'#{photo.thumbnail_url}')]") } + it { is_expected.to have_xpath("//a[contains(@href,'#{photo_path(photo)}')]") } end - it "links to newest photo" do - expect(subject).to have_xpath("//a[contains(@href,'#{photo_path(photos.last)}')]") - end - it "does not show oldest photo" do - expect(subject).not_to have_xpath("//img[contains(@src,'#{photos.first.thumbnail_url}')]") - end - it "does not link to oldest photo" do - expect(subject).not_to have_xpath("//a[contains(@href,'#{photo_path(photos.first)}')]") + + context 'has 50 photos' do + before { seed.photos = photos } + + let!(:photos) { FactoryBot.create_list :photo, 50 } + + it "shows newest photo" do + expect(subject).to have_xpath("//img[contains(@src,'#{photos.last.thumbnail_url}')]") + end + it "links to newest photo" do + expect(subject).to have_xpath("//a[contains(@href,'#{photo_path(photos.last)}')]") + end + it "does not show oldest photo" do + expect(subject).not_to have_xpath("//img[contains(@src,'#{photos.first.thumbnail_url}')]") + end + it "does not link to oldest photo" do + expect(subject).not_to have_xpath("//a[contains(@href,'#{photo_path(photos.first)}')]") + end end end end diff --git a/spec/support/devise.rb b/spec/support/devise.rb index 0cf56ea83..090ffc6a8 100644 --- a/spec/support/devise.rb +++ b/spec/support/devise.rb @@ -1,4 +1,5 @@ RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::ControllerHelpers, type: :view + config.include Devise::Test::IntegrationHelpers, type: :feature end diff --git a/spec/support/feature_helpers.rb b/spec/support/feature_helpers.rb index 1f3fc1902..0b85fe72a 100644 --- a/spec/support/feature_helpers.rb +++ b/spec/support/feature_helpers.rb @@ -10,9 +10,22 @@ module FeatureHelpers page.execute_script " $('#{selector}').mouseenter().click() " end - shared_context 'signed in' do - before { sign_in user } - after { sign_out user } + shared_context 'signed in member' do + let(:member) { FactoryBot.create :member } + include_examples 'sign in' + end + shared_context 'signed in crop wrangler' do + let(:member) { FactoryBot.create :crop_wrangling_member } + include_examples 'sign in' + end + shared_context 'signed in admin' do + let(:member) { FactoryBot.create :admin_member } + include_examples 'sign in' + end + + shared_examples 'sign in' do + before { sign_in member } + after { sign_out member } end end