diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 998975c34..e085a74cb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -170,14 +170,6 @@ RSpec/Capybara/VisibilityMatcher: Exclude: - 'spec/features/shared_examples/crop_suggest.rb' -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: NegatedMatcher. -RSpec/ChangeByZero: - Exclude: - - 'spec/models/comment_spec.rb' - - 'spec/models/post_spec.rb' - # Offense count: 292 # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without @@ -211,47 +203,6 @@ RSpec/EmptyExampleGroup: - 'spec/views/photos/edit.html.haml_spec.rb' - 'spec/views/posts/_single.html.haml_spec.rb' -# Offense count: 78 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowConsecutiveOneLiners. -RSpec/EmptyLineAfterExample: - Enabled: false - -# Offense count: 13 -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterExampleGroup: - Exclude: - - 'spec/controllers/crops_controller_spec.rb' - - 'spec/controllers/posts_controller_spec.rb' - - 'spec/features/cms_spec.rb' - - 'spec/features/crops/creating_a_crop_spec.rb' - - 'spec/features/likeable_spec.rb' - - 'spec/features/seeds/adding_seeds_spec.rb' - - 'spec/features/timeline/index_spec.rb' - - 'spec/models/planting_spec.rb' - - 'spec/services/crop_search_service_spec.rb' - -# Offense count: 51 -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterFinalLet: - Enabled: false - -# Offense count: 88 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowConsecutiveOneLiners. -RSpec/EmptyLineAfterHook: - Enabled: false - -# Offense count: 6 -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterSubject: - Exclude: - - 'spec/controllers/crops_controller_spec.rb' - - 'spec/features/members/deletion_spec.rb' - - 'spec/helpers/event_helper_spec.rb' - - 'spec/services/crop_search_service_spec.rb' - - 'spec/services/timeline_service_spec.rb' - # Offense count: 136 # Configuration parameters: CountAsOne. RSpec/ExampleLength: diff --git a/spec/controllers/admin/roles_controller_spec.rb b/spec/controllers/admin/roles_controller_spec.rb index 872e67dcf..e4b74fea7 100644 --- a/spec/controllers/admin/roles_controller_spec.rb +++ b/spec/controllers/admin/roles_controller_spec.rb @@ -11,11 +11,13 @@ describe Admin::RolesController do describe "GET index" do before { get :index } + it { expect(assigns(:roles)).to be_nil } end describe "GET new" do before { get :new } + it { expect(assigns(:role)).to be_nil } end @@ -27,11 +29,13 @@ describe Admin::RolesController do describe "GET edit" do before { get :edit, params: { id: role.to_param } } + it { expect(assigns(:role)).to be_nil } end describe "update" do before { patch :update, params: { id: role.id, role: { name: 'updated role' } } } + it { expect(Role.first.name).to eq 'MyString' } end end @@ -41,11 +45,13 @@ describe Admin::RolesController do describe "GET index" do before { get :index } + it { expect(assigns(:roles)).to eq([Role.find_by(name: 'admin'), role]) } end describe "GET new" do before { get :new } + it { expect(assigns(:role)).to be_a(Role) } end @@ -59,11 +65,13 @@ describe Admin::RolesController do describe "GET edit" do before { get :edit, params: { id: role.to_param } } + it { expect(assigns(:role)).to eq(role) } end describe "update" do before { patch :update, params: { id: role.id, role: { name: 'updated role' } } } + it { expect(Role.first.name).to eq 'updated role' } end end diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb index f79ca0cba..2a8721d51 100644 --- a/spec/controllers/crops_controller_spec.rb +++ b/spec/controllers/crops_controller_spec.rb @@ -44,7 +44,9 @@ describe CropsController do describe 'fetches the crop search page' do let!(:tomato) { FactoryBot.create :tomato } let!(:maize) { FactoryBot.create :maize } + before { Crop.reindex } + describe 'search form page' do before { get :search } @@ -54,6 +56,7 @@ describe CropsController do describe 'perform a search' do before { get :search, params: { term: 'tom' } } + it { expect(assigns(:term)).to eq 'tom' } it { expect(assigns(:crops).map(&:name)).to eq ['tomato'] } end @@ -81,13 +84,17 @@ describe CropsController do sci_name: { "1": "fancy sci name", "2": "" } } end + subject { put :create, params: crop_params } + context 'not logged in' do it { expect { subject }.not_to change(Crop, :count) } end + context 'logged in as member' do it { expect { subject }.not_to change(Crop, :count) } end + context 'wrangler' do include_context 'login as wrangler' it { expect { subject }.to change(Crop, :count).by(1) } diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index b3f5123f1..82abad361 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -36,6 +36,7 @@ describe PhotosController, :search do it "has indexed the photos of this crop" do expect(Photo.search).to include crop_photo end + it "assigns crop" do expect(assigns(:crop)).to eq crop end @@ -112,6 +113,7 @@ describe PhotosController, :search do type: "planting", id: planting.id } end + it { expect(flash[:alert]).not_to be_present } it { expect(Photo.last.plantings.first).to eq planting } end @@ -145,6 +147,7 @@ describe PhotosController, :search do photo: { source_id: photo.source_id, source: 'flickr' }, type: "harvest", id: harvest.id } end + it { expect(flash[:alert]).not_to be_present } it { expect(Photo.last.harvests.size).to eq 1 } end @@ -175,7 +178,9 @@ describe PhotosController, :search do describe "creates the planting/photo link" do let(:planting) { FactoryBot.create(:planting, garden:, owner: member) } let(:photo) { FactoryBot.create(:photo, owner: member) } + before { post :create, params: { photo: { source_id: photo.source_id, source: 'flickr' }, type: "planting", id: planting.id } } + it { expect(flash[:alert]).not_to be_present } it { expect(Photo.last.plantings.first).to eq planting } end diff --git a/spec/controllers/plantings_controller_spec.rb b/spec/controllers/plantings_controller_spec.rb index f7d9a0a21..87f26d5ed 100644 --- a/spec/controllers/plantings_controller_spec.rb +++ b/spec/controllers/plantings_controller_spec.rb @@ -19,6 +19,7 @@ describe PlantingsController, :search do let!(:maize) { FactoryBot.create(:maize) } let!(:planting1) { FactoryBot.create :planting, crop: tomato, owner: member1, created_at: 1.day.ago } let!(:planting2) { FactoryBot.create :planting, crop: maize, owner: member2, created_at: 5.days.ago } + before do Planting.reindex end @@ -129,8 +130,10 @@ describe PlantingsController, :search do describe 'GET :edit' do let(:my_planting) { FactoryBot.create :planting, owner: member } let(:not_my_planting) { FactoryBot.create :planting } + context 'my planting' do before { get :edit, params: { slug: my_planting } } + it { expect(assigns(:planting)).to eq my_planting } end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 9d88254ac..e1f044e63 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -15,12 +15,16 @@ describe PostsController do FactoryBot.create_list :post, 100 FactoryBot.create_list :post, 5, author: member end + describe "everyone's posts" do before { get :index } + it { expect(assigns(:posts).size).to eq 12 } end + describe "one member's posts" do before { get :index, params: { member_slug: member.slug } } + it { expect(assigns(:posts).size).to eq 5 } it { expect(assigns(:posts).first.author).to eq member } end diff --git a/spec/features/admin/forums_spec.rb b/spec/features/admin/forums_spec.rb index fda1aa602..dfe2f491e 100644 --- a/spec/features/admin/forums_spec.rb +++ b/spec/features/admin/forums_spec.rb @@ -13,6 +13,7 @@ describe "forums", js: true do click_link "Forums" end end + it { expect(page).to have_current_path forums_path, ignore_query: true } it { expect(page).to have_link "New forum" } end @@ -26,6 +27,7 @@ describe "forums", js: true do fill_in 'Description', with: "this is a new forum" click_button 'Save' end + it { expect(page).to have_current_path forum_path(Forum.last), ignore_query: true } it { expect(page).to have_content 'Forum was successfully created' } end @@ -38,6 +40,7 @@ describe "forums", js: true do click_button 'Save' forum.reload end + it { expect(page).to have_current_path forum_path(forum), ignore_query: true } it { expect(page).to have_content 'Forum was successfully updated' } it { expect(page).to have_content 'Something else' } @@ -50,6 +53,7 @@ describe "forums", js: true do click_link 'Delete' end end + it { expect(page).to have_current_path forums_path, ignore_query: true } it { expect(page).to have_content 'Forum was successfully deleted' } end diff --git a/spec/features/cms_spec.rb b/spec/features/cms_spec.rb index 3a9a107ca..569917747 100644 --- a/spec/features/cms_spec.rb +++ b/spec/features/cms_spec.rb @@ -17,6 +17,7 @@ describe "cms admin" do expect(page).to have_content "Please sign in as an admin user" end end + context 'admin' do include_context 'signed in admin' it "admin members can view CMS admin area" do diff --git a/spec/features/conversations/index_spec.rb b/spec/features/conversations/index_spec.rb index 4b94ed5fc..4728bbf4c 100644 --- a/spec/features/conversations/index_spec.rb +++ b/spec/features/conversations/index_spec.rb @@ -17,6 +17,7 @@ describe "Conversations", :js do click_link recipient.login_name click_link 'Inbox' end + it { expect(page).to have_content 'something i want to say' } it { page.percy_snapshot(page, name: 'conversations#index') } @@ -28,13 +29,17 @@ describe "Conversations", :js do describe 'view trash' do before { click_link 'trash' } + it { expect(page).to have_content 'something i want to say' } + describe 'restore conversation' do before { click_link class: 'restore' } + it { expect(page).not_to have_content 'something i want to say' } describe 'conversation was restored' do before { click_link 'inbox' } + it { expect(page).to have_content 'something i want to say' } end end diff --git a/spec/features/conversations/show_spec.rb b/spec/features/conversations/show_spec.rb index f325b3c26..5b1e899b1 100644 --- a/spec/features/conversations/show_spec.rb +++ b/spec/features/conversations/show_spec.rb @@ -28,6 +28,7 @@ describe "Conversations", :js do fill_in :body, with: 'i like this too' click_button 'Send' end + it { expect(page).to have_content "i like this too" } end end diff --git a/spec/features/crops/creating_a_crop_spec.rb b/spec/features/crops/creating_a_crop_spec.rb index cad18bdae..b4bb23e1b 100644 --- a/spec/features/crops/creating_a_crop_spec.rb +++ b/spec/features/crops/creating_a_crop_spec.rb @@ -31,6 +31,7 @@ describe "Crop", js: true do click_button "Save" end end + it { expect(page).to have_content 'crop was successfully created.' } it { expect(page).to have_content "This crop is currently pending approval." } it { expect(page).to have_content "Jasminum sambac 2" } @@ -43,6 +44,7 @@ describe "Crop", js: true do before do click_button "Save" end + it { expect(page).to have_content 'crop was successfully created.' } it { expect(page).to have_content "Jasminum sambac 2" } it { expect(page).to have_content "Matsurika" } @@ -51,16 +53,20 @@ describe "Crop", js: true do context 'anon' do before { visit new_crop_path } + it { expect(page).to have_content 'You need to sign in' } end + context 'member' do include_context 'signed in member' include_examples 'request crop' end + context 'crop wrangler' do include_context 'signed in crop wrangler' include_examples 'create crop' end + context 'admin' do include_context 'signed in admin' include_examples 'create crop' diff --git a/spec/features/crops/crop_detail_page_spec.rb b/spec/features/crops/crop_detail_page_spec.rb index 31133fbc6..6df7eeb52 100644 --- a/spec/features/crops/crop_detail_page_spec.rb +++ b/spec/features/crops/crop_detail_page_spec.rb @@ -6,6 +6,7 @@ describe "crop detail page", js: true do before do FactoryBot.create :plant_part, name: 'leaf' end + subject do # Update the medians after all the # data has been loaded @@ -34,12 +35,15 @@ describe "crop detail page", js: true do click_link 'Add to my garden' expect(page).to have_link "add new garden" end + it "has a link to harvest the crop" do click_link 'Record harvest' expect(page).to have_link "leaf" end + describe "Saving seeds" do before { click_link 'Save seeds' } + it { expect(page).to have_text "Will you offer these seeds for trade?" } it { expect(page).to have_button "locally" } it { expect(page).to have_button "nationally" } @@ -94,10 +98,12 @@ describe "crop detail page", js: true do context 'signed in' do include_context 'signed in member' before { seed.update! owner: member } + 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." diff --git a/spec/features/crops/crop_photos_spec.rb b/spec/features/crops/crop_photos_spec.rb index 92cbdb86a..0757127a8 100644 --- a/spec/features/crops/crop_photos_spec.rb +++ b/spec/features/crops/crop_photos_spec.rb @@ -65,6 +65,7 @@ describe "crop detail page", :js, :search do context "when signed in as photos owner" do include_context 'signed in member' let(:member) { owner_member } + include_examples "shows photos" end diff --git a/spec/features/crops/crop_wrangling_button_spec.rb b/spec/features/crops/crop_wrangling_button_spec.rb index ca50774ea..cba397cdc 100644 --- a/spec/features/crops/crop_wrangling_button_spec.rb +++ b/spec/features/crops/crop_wrangling_button_spec.rb @@ -5,18 +5,21 @@ require 'rails_helper' describe "crop wrangling button" do context 'not signed in' do before { visit crops_path } + it { expect(page).not_to have_link "Wrangle Crops", href: wrangle_crops_path } end context "signed in, but not a crop wrangler" do include_context 'signed in member' before { visit crops_path } + it { expect(page).not_to have_link "Wrangle Crops", href: wrangle_crops_path } end context "signed in crop wrangler" do include_context 'signed in crop wrangler' before { visit crops_path } + it { expect(page).to have_link "Wrangle Crops", href: wrangle_crops_path } end end diff --git a/spec/features/crops/show_spec.rb b/spec/features/crops/show_spec.rb index a219f774d..1ae280465 100644 --- a/spec/features/crops/show_spec.rb +++ b/spec/features/crops/show_spec.rb @@ -14,7 +14,9 @@ describe "browse crops" do let!(:cherry) { FactoryBot.create :crop, name: 'cherry tomato', parent: tomato } let!(:heirloom) { FactoryBot.create :crop, name: 'heirloom tomato', parent: tomato } let!(:striped) { FactoryBot.create :crop, name: 'striped tomato', parent: heirloom } + before { visit crop_path(tomato) } + it { expect(page).to have_link heirloom.name, href: crop_path(heirloom) } it { expect(page).to have_link cherry.name, href: crop_path(cherry) } it { expect(page).to have_link striped.name, href: crop_path(striped) } diff --git a/spec/features/gardens/actions_spec.rb b/spec/features/gardens/actions_spec.rb index a127ead47..cb8ed4f89 100644 --- a/spec/features/gardens/actions_spec.rb +++ b/spec/features/gardens/actions_spec.rb @@ -29,6 +29,7 @@ describe "Gardens" do 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' @@ -61,6 +62,7 @@ describe "Gardens" do 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" } @@ -70,6 +72,7 @@ describe "Gardens" do describe "someone else's garden" do before { visit garden_path(other_member_garden) } + it { is_expected.not_to have_link 'Actions' } end end diff --git a/spec/features/gardens/index_spec.rb b/spec/features/gardens/index_spec.rb index 04df61b5b..5aedb1850 100644 --- a/spec/features/gardens/index_spec.rb +++ b/spec/features/gardens/index_spec.rb @@ -19,6 +19,7 @@ describe "Gardens#index", :js do 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)) @@ -35,12 +36,15 @@ describe "Gardens#index", :js do 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 @@ -64,6 +68,7 @@ describe "Gardens#index", :js do 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 diff --git a/spec/features/harvests/harvesting_a_crop_spec.rb b/spec/features/harvests/harvesting_a_crop_spec.rb index da9219cc1..1d31a21fe 100644 --- a/spec/features/harvests/harvesting_a_crop_spec.rb +++ b/spec/features/harvests/harvesting_a_crop_spec.rb @@ -42,7 +42,9 @@ describe "Harvesting a crop", :js, :search do describe 'member harvests' do before { visit member_harvests_path(member) } + it { expect(page).to have_text "#{member.login_name}'s harvests" } + it "Clicking link to owner's profile" do within '.login-name' do click_link member.login_name @@ -64,6 +66,7 @@ describe "Harvesting a crop", :js, :search do 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" diff --git a/spec/features/home/home_spec.rb b/spec/features/home/home_spec.rb index d2bae29d4..e9117e3e8 100644 --- a/spec/features/home/home_spec.rb +++ b/spec/features/home/home_spec.rb @@ -37,9 +37,11 @@ describe "home page", :search do it "show tradeable seed" do expect(subject).to have_link href: seed_path(tradable_seed) end + it "does not show finished seeds" do expect(subject).not_to have_link href: seed_path(finished_seed) end + it "does not show untradable seeds" do expect(subject).not_to have_link href: seed_path(untradable_seed) end @@ -64,12 +66,14 @@ describe "home page", :search do shared_examples "show crops" do describe 'shows crops section' do before { crop.reindex } + it { is_expected.to have_text 'Some of our crops' } it { is_expected.to have_link href: crop_path(crop) } end describe 'shows recently added crops' do it { is_expected.to have_text 'Recently Added' } + it 'link to newest crops' do expect(subject).to have_link crop.name, href: crop_path(crop) end diff --git a/spec/features/likeable_spec.rb b/spec/features/likeable_spec.rb index ba04c04d3..81b2d8264 100644 --- a/spec/features/likeable_spec.rb +++ b/spec/features/likeable_spec.rb @@ -53,24 +53,32 @@ describe 'Likeable', :js, search: true do end describe 'photos#index' do let(:path) { photos_path } + include_examples 'photo can be liked' end + describe 'photos#show' do let(:path) { photo_path(photo) } + include_examples 'photo can be liked' end + describe 'crops#show' do let(:crop) { FactoryBot.create :crop } let(:planting) { FactoryBot.create :planting, owner: member, crop: } let(:path) { crop_path(crop) } + before { planting.photos << photo } + include_examples 'photo can be liked' end end describe 'posts' do let(:like_count_class) { "#post-#{post.id} .like-count" } + before { visit post_path(post) } + it 'can be liked' do expect(page).to have_css(like_count_class, text: "0") expect(page).to have_link 'Like' diff --git a/spec/features/locale_spec.rb b/spec/features/locale_spec.rb index ac4ba0818..21926159d 100644 --- a/spec/features/locale_spec.rb +++ b/spec/features/locale_spec.rb @@ -6,6 +6,7 @@ describe "Changing locales", js: true do after { I18n.locale = :en } let(:member) { FactoryBot.create :member } + it "Locale can be set with a query param" do # Login then log out, to ensure we're now logged out login_as member diff --git a/spec/features/members/ban_spec.rb b/spec/features/members/ban_spec.rb index 3395637c2..b7c8f00fc 100644 --- a/spec/features/members/ban_spec.rb +++ b/spec/features/members/ban_spec.rb @@ -9,11 +9,14 @@ describe "members list" do context 'logged in as admin' do include_context 'signed in admin' 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 diff --git a/spec/features/members/deletion_spec.rb b/spec/features/members/deletion_spec.rb index d020e0c3b..f7059965c 100644 --- a/spec/features/members/deletion_spec.rb +++ b/spec/features/members/deletion_spec.rb @@ -89,6 +89,7 @@ describe "member deletion" do describe 'member exists but is marked deleted' do subject { Member.all.find(member.id) } + it { expect(subject).to eq member } it { expect(subject.discarded?).to be true } it { expect(Member.kept).not_to include(member) } @@ -158,6 +159,7 @@ describe "member deletion" do let(:otherwrangler) { FactoryBot.create(:crop_wrangling_member) } let(:crop) { FactoryBot.create(:crop, creator: member) } before { FactoryBot.create(:cropbot) } + let!(:ex_wrangler) { FactoryBot.create(:crop_wrangling_member, login_name: "ex_wrangler") } it "leaves crops behind" do diff --git a/spec/features/members/profile_spec.rb b/spec/features/members/profile_spec.rb index eec256e24..22654291c 100644 --- a/spec/features/members/profile_spec.rb +++ b/spec/features/members/profile_spec.rb @@ -44,6 +44,7 @@ describe "member profile", js: true do visit member_path(public_member) expect(page).to have_content public_member.email end + it "private email address" do visit member_path(member) expect(page).not_to have_content member.email @@ -65,7 +66,9 @@ describe "member profile", js: true do let!(:harvest) { FactoryBot.create :harvest, owner: member } let!(:seed) { FactoryBot.create :seed, owner: member } let!(:post) { FactoryBot.create :post, author: member } + before { visit member_path(member) } + it { expect(page).to have_link(href: planting_path(planting)) } it { expect(page).to have_link(href: harvest_path(harvest)) } it { expect(page).to have_link(href: seed_path(seed)) } @@ -88,6 +91,7 @@ describe "member profile", js: true do describe 'user role labels' do describe "admin user's page" do before { visit member_path(admin_member) } + it { expect(page).to have_text "Admin" } end @@ -110,7 +114,9 @@ describe "member profile", js: true do let!(:old_planting) { FactoryBot.create :planting, owner: member, planted_at: 3.years.ago } let!(:finished_planting) { FactoryBot.create :finished_planting, owner: member } let!(:no_planted_at_planting) { FactoryBot.create :planting, owner: member, planted_at: nil } + before { visit member_path(member) } + it { expect(page).to have_link href: planting_path(new_planting) } it { expect(page).to have_link href: planting_path(old_planting) } it { expect(page).to have_link href: planting_path(finished_planting) } @@ -119,26 +125,34 @@ describe "member profile", js: true do context 'member has seeds' do let!(:seed) { FactoryBot.create :seed, owner: member } + before { visit member_path(member) } + it { expect(page).to have_link href: seed_path(seed) } end context 'member has harvests' do let!(:harvest) { FactoryBot.create :harvest, owner: member } + before { visit member_path(member) } + it { expect(page).to have_link href: harvest_path(harvest) } end context 'member has posts' do let!(:post) { FactoryBot.create :post, author: member } + before { visit member_path(member) } + it { expect(page).to have_link href: post_path(post) } end context 'member has comments' do let(:post) { FactoryBot.create :post } let!(:comment) { FactoryBot.create :comment, post:, author: member } + before { visit member_path(member) } + it { expect(page).to have_link href: post_path(post) } it { expect(page).to have_link href: comment_path(comment) } end @@ -146,7 +160,9 @@ describe "member profile", js: true do context 'photos' do let(:planting) { FactoryBot.create :planting, owner: member } let!(:photo) { FactoryBot.create :photo, owner: member, plantings: [planting] } + before { visit member_path(member) } + it { expect(page).to have_link href: photo_path(photo) } it { expect(page).to have_link href: planting_path(planting) } end @@ -230,6 +246,7 @@ describe "member profile", js: true do it "has a private message button" do expect(page).to have_link "Send message", href: new_message_path(recipient_id: other_member.id) end + it { expect(page).not_to have_link "Edit profile", href: edit_member_registration_path } end end diff --git a/spec/features/percy/percy_spec.rb b/spec/features/percy/percy_spec.rb index ceab0359d..6a574b858 100644 --- a/spec/features/percy/percy_spec.rb +++ b/spec/features/percy/percy_spec.rb @@ -6,6 +6,7 @@ describe 'Test with visual testing', js: true, type: :feature do # Use the same random seed every time so our random data is the same # on every run, so doesn't trigger percy to see changes before { Faker::Config.random = Random.new(42) } + let!(:member) { FactoryBot.create :member, login_name: 'percy', preferred_avatar_uri: gravatar } let!(:crop_wrangler) { FactoryBot.create :crop_wrangling_member, login_name: 'croppy', preferred_avatar_uri: gravatar2 } let!(:admin_user) { FactoryBot.create :admin_member, login_name: 'janitor', preferred_avatar_uri: gravatar3 } @@ -66,6 +67,7 @@ rest of the garden. " end let(:post) { FactoryBot.create :post, author: member, subject: "Watering", body: post_body } + before do # Freeze time, so we don't have variations in timestamps on the page Timecop.freeze(Time.zone.local(2019, 1, 1)) @@ -96,6 +98,7 @@ rest of the garden. FactoryBot.create :seed, owner: member, tradable_to: 'nationally' FactoryBot.create :seed, owner: someone_else, tradable_to: 'nationally' end + after { Timecop.return } shared_examples 'visit pages' do @@ -124,6 +127,7 @@ rest of the garden. expect(page).to have_text 'tomato' page.percy_snapshot(page, name: "#{prefix}/crops#show") end + it 'loads crops#index' do visit crops_path page.percy_snapshot(page, name: "#{prefix}/crops#index") @@ -189,6 +193,7 @@ rest of the garden. visit post_path(post) page.percy_snapshot(page, name: "#{prefix}/posts#show") end + it 'loads posts#index' do Member.all.limit(5).each do |member| FactoryBot.create_list :post, 12, author: member @@ -212,6 +217,7 @@ rest of the garden. context "when signed out" do let(:prefix) { 'signed-out' } + include_examples 'visit pages' it 'loads sign in page' do @@ -230,6 +236,7 @@ rest of the garden. visit new_member_password_path page.percy_snapshot(page, name: "forgot-password") end + it 'loads new confirmation' do visit new_member_confirmation_path page.percy_snapshot(page, name: "new-confimation") @@ -238,6 +245,7 @@ rest of the garden. context 'when signed in' do let(:prefix) { 'signed-in' } + include_context 'signed in member' include_examples 'visit pages' @@ -357,29 +365,36 @@ rest of the garden. context 'admin' do include_context 'signed in admin' before { visit admin_path } + it 'admin page' do page.percy_snapshot(page, name: 'Admin') end + it 'Roles' do click_link 'Roles' page.percy_snapshot(page, name: 'Admin Roles') end + it 'CMS' do click_link 'CMS' page.percy_snapshot(page, name: 'CMS') end + it 'Garden Types' do click_link 'Garden Types' page.percy_snapshot(page, name: 'Admin Garden type') end + it 'Alternate names' do click_link 'Alternate names' page.percy_snapshot(page, name: 'Admin Alternate names') end + it 'Scientific names' do click_link 'Scientific names' page.percy_snapshot(page, name: 'Admin Scientific names') end + it 'Members' do click_link 'Members' page.percy_snapshot(page, name: 'Admin Members') diff --git a/spec/features/photos/show_photo_spec.rb b/spec/features/photos/show_photo_spec.rb index 4b5fb2a90..6671e2a68 100644 --- a/spec/features/photos/show_photo_spec.rb +++ b/spec/features/photos/show_photo_spec.rb @@ -19,6 +19,7 @@ describe "show photo page" do 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 diff --git a/spec/features/places/searching_a_place_spec.rb b/spec/features/places/searching_a_place_spec.rb index 62156d332..711e9cedb 100644 --- a/spec/features/places/searching_a_place_spec.rb +++ b/spec/features/places/searching_a_place_spec.rb @@ -14,6 +14,7 @@ describe "User searches" do visit places_path search_with "Philippines" end + it { expect(page).to have_content "community near Philippines" } it { expect(page).to have_button "search_button" } it { expect(page).to have_content "Nearby members" } @@ -30,6 +31,7 @@ describe "User searches" do describe "Nearby plantings, seed, and members" do include_context 'signed in member' let(:member) { located_member } + before do visit places_path search_with "Philippines" diff --git a/spec/features/plantings/planting_a_crop_spec.rb b/spec/features/plantings/planting_a_crop_spec.rb index 695c24c5b..2577350d9 100644 --- a/spec/features/plantings/planting_a_crop_spec.rb +++ b/spec/features/plantings/planting_a_crop_spec.rb @@ -58,6 +58,7 @@ describe "Planting a crop", :js, :search do click_link member.login_name end end + it { expect(page).to have_current_path member_path(member), ignore_query: true } end diff --git a/spec/features/plantings/prediction_spec.rb b/spec/features/plantings/prediction_spec.rb index b890e023d..813250bbb 100644 --- a/spec/features/plantings/prediction_spec.rb +++ b/spec/features/plantings/prediction_spec.rb @@ -10,6 +10,7 @@ describe "Display a planting", :js do describe 'no harvest to predict from' do before { visit planting_path(planting) } + it { expect(planting.harvest_months).to eq({}) } it { expect(page).to have_content 'We need more data on this crop in your latitude.' } end @@ -20,7 +21,9 @@ describe "Display a planting", :js do FactoryBot.create :harvest, planting: planting, crop: crop, harvested_at: '18 June 2019' FactoryBot.create_list :harvest, 4, planting:, crop:, harvested_at: '18 August 2019' end + before { visit planting_path(planting) } + it { expect(page.find_by_id('month-1')[:class]).not_to include("badge-harvesting") } it { expect(page.find_by_id('month-2')[:class]).not_to include("badge-harvesting") } it { expect(page.find_by_id('month-5')[:class]).to include("badge-harvesting") } @@ -51,7 +54,9 @@ describe "Display a planting", :js do FactoryBot.create_list :harvest, 4, planting: faraway_planting, crop:, harvested_at: '18 December 2006' end + before { visit planting_path(planting) } + it { expect(page.find_by_id('month-1')[:class]).not_to include("badge-harvesting") } it { expect(page.find_by_id('month-2')[:class]).not_to include("badge-harvesting") } it { expect(page.find_by_id('month-5')[:class]).to include("badge-harvesting") } diff --git a/spec/features/plantings/show_spec.rb b/spec/features/plantings/show_spec.rb index 727287905..01053987c 100644 --- a/spec/features/plantings/show_spec.rb +++ b/spec/features/plantings/show_spec.rb @@ -9,21 +9,25 @@ describe "Display a planting", :js do context 'Perennial planted long ago' do let(:planting) { FactoryBot.create :perennial_planting } + it { expect(page).to have_text 'Perennial' } end context 'Perennial finished' do let(:planting) { FactoryBot.create :perennial_planting, planted_at: 6.years.ago, finished: true, finished_at: 1.year.ago } + it { expect(page).to have_text 'Perennial' } end context 'Annual no predictions' do let(:planting) { FactoryBot.create :annual_planting } + it { expect(page).not_to have_text 'Finish expected' } end context 'Annual with predicted finish' do let(:planting) { FactoryBot.create :predicatable_planting, planted_at: 2.weeks.ago } + it { expect(page).to have_text '28%' } it { expect(page).to have_text '14/50 days' } it { expect(page).to have_text "Planted #{I18n.l(2.weeks.ago.to_date)}" } @@ -32,11 +36,13 @@ describe "Display a planting", :js do context 'Annual finished' do let(:planting) { FactoryBot.create :annual_planting, planted_at: 100.days.ago, finished: true, finished_at: 1.day.ago } + it { expect(page).to have_text "Planted #{I18n.l(planting.planted_at)}" } end context 'Planting with harvests' do let(:planting) { FactoryBot.create(:harvest_with_planting).planting } + it { expect(page).to have_text 'Harvest started' } end @@ -56,11 +62,13 @@ describe "Display a planting", :js do FactoryBot.create :annual_planting, planted_at: 200.days.ago, crop: end + it { expect(page).to have_text 'First harvest expected' } end context 'with quantity' do let(:planting) { FactoryBot.create :planting, quantity: 100 } + it { expect(find('.plantingfact--quantity')).to have_text '100' } end end @@ -72,12 +80,15 @@ describe "Display a planting", :js do context 'with matching seeds' do let(:seed) { FactoryBot.create :seed, saved_at: 1.month.ago, owner: member } let(:planting) { FactoryBot.create :planting, planted_at: 1.day.ago, crop: seed.crop, owner: member } + it { expect(page).to have_text 'Is this from one of these plantings? ' } + describe 'linking to planting' do before do choose "planting_parent_seed_id_#{planting.id}" click_button 'save' end + it { expect(page).to have_text 'Parent seed' } it { expect(page).to have_link href: planting_path(planting) } end diff --git a/spec/features/seeds/adding_seeds_spec.rb b/spec/features/seeds/adding_seeds_spec.rb index f6789fd71..ded0977c3 100644 --- a/spec/features/seeds/adding_seeds_spec.rb +++ b/spec/features/seeds/adding_seeds_spec.rb @@ -61,14 +61,18 @@ describe "Seeds", :js, :search do visit crop_path(maize) click_link "Save seeds" end + describe 'no trades' do before { click_button "Save #{maize.name} seeds." } + it { expect(page).to have_content "nowhere" } it { expect(page).to have_content "Successfully added maize seed to your stash" } it { expect(page).to have_content "maize" } end + describe 'tradeable' do before { click_button "locally" } + it { expect(page).to have_content "locally" } it { expect(page).to have_content "Successfully added maize seed to your stash" } it { expect(page).to have_content "maize" } diff --git a/spec/features/seeds/seed_photos.rb b/spec/features/seeds/seed_photos.rb index 37640f127..61941b7f2 100644 --- a/spec/features/seeds/seed_photos.rb +++ b/spec/features/seeds/seed_photos.rb @@ -7,6 +7,7 @@ describe "Seeds", :js do context 'signed in' do include_context 'signed in member' before { visit seed_path(seed) } + subject { page } let(:member) { FactoryBot.create :member } @@ -34,12 +35,15 @@ describe "Seeds", :js do 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 diff --git a/spec/features/shared_examples/append_date.rb b/spec/features/shared_examples/append_date.rb index 3b8b6ec64..82566acef 100644 --- a/spec/features/shared_examples/append_date.rb +++ b/spec/features/shared_examples/append_date.rb @@ -15,6 +15,7 @@ shared_examples "append date" do find(".datepicker-days td.day", text: "21").click end end + it { expect(page).to have_content "Finished" } it { expect(page).to have_content "#{this_month} #{this_year}" } end @@ -25,6 +26,7 @@ shared_examples "append date" do click_link link_text click_link "Confirm without date" end + it { expect(page).to have_content("Finished") } end end diff --git a/spec/features/signin_spec.rb b/spec/features/signin_spec.rb index 907bc8492..a5b090a8f 100644 --- a/spec/features/signin_spec.rb +++ b/spec/features/signin_spec.rb @@ -45,6 +45,7 @@ describe "signin", js: true do conversation = member.send_message(recipient, 'hey there', 'kiaora') visit conversation_path(conversation) end + it { expect(page).to have_current_path new_member_session_path, ignore_query: true } end diff --git a/spec/features/timeline/index_spec.rb b/spec/features/timeline/index_spec.rb index 134c4a06f..004f333d0 100644 --- a/spec/features/timeline/index_spec.rb +++ b/spec/features/timeline/index_spec.rb @@ -29,6 +29,7 @@ describe "timeline", js: true do it { expect(page).to have_link href: planting_path(finished_planting) } it { expect(page).not_to have_link href: planting_path(no_planted_at_planting) } end + describe 'shows the friends you follow' do it { expect(page).to have_link href: member_path(friend1) } it { expect(page).to have_link href: member_path(friend2) } diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 2a0a4950c..042dd8ffc 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -75,18 +75,23 @@ describe ApplicationHelper do it 'works when :alert' do expect(build_alert_classes(:alert)).to include 'alert-danger' end + it 'works when :danger' do expect(build_alert_classes(:danger)).to include 'alert-danger' end + it 'works when :error' do expect(build_alert_classes(:error)).to include 'alert-danger' end + it 'works when :validation_errors' do expect(build_alert_classes(:validation_errors)).to include 'alert-danger' end + it 'includes base classes' do expect(build_alert_classes(:danger)).to include 'alert alert-dismissable' end + it 'does not include danger when info' do expect(build_alert_classes(:info)).not_to include ' alert-danger' end @@ -96,12 +101,15 @@ describe ApplicationHelper do it 'works when :warning' do expect(build_alert_classes(:warning)).to include 'alert-warning' end + it 'works when :todo' do expect(build_alert_classes(:todo)).to include 'alert-warning' end + it 'includes base classes' do expect(build_alert_classes(:warning)).to include 'alert alert-dismissable' end + it 'does not include warning when info' do expect(build_alert_classes(:info)).not_to include ' alert-warning' end @@ -111,12 +119,15 @@ describe ApplicationHelper do it 'works when :notice' do expect(build_alert_classes(:notice)).to include 'alert-success' end + it 'works when :success' do expect(build_alert_classes(:success)).to include 'alert-success' end + it 'includes base classes' do expect(build_alert_classes(:success)).to include 'alert alert-dismissable' end + it 'does not include success when info' do expect(build_alert_classes(:info)).not_to include ' alert-success' end @@ -126,12 +137,15 @@ describe ApplicationHelper do it 'works when :info' do expect(build_alert_classes(:info)).to include 'alert-info' end + it 'works when blank' do expect(build_alert_classes).to include 'alert-info' end + it 'includes base classes' do expect(build_alert_classes(:info)).to include 'alert alert-dismissable' end + it 'does not include info when danger' do expect(build_alert_classes(:danger)).not_to include ' alert-info' end diff --git a/spec/helpers/event_helper_spec.rb b/spec/helpers/event_helper_spec.rb index 634953c50..d18d53a76 100644 --- a/spec/helpers/event_helper_spec.rb +++ b/spec/helpers/event_helper_spec.rb @@ -11,38 +11,45 @@ RSpec.describe EventHelper, type: :helper do let(:photo) { FactoryBot.create :photo } subject { resolve_model(event) } + describe 'plantings' do let(:event) { OpenStruct.new(id: planting.id, event_type: 'planting') } + it { expect(subject).to eq planting } it { expect(event_description(event)).to have_text "planted #{planting.crop.name}" } end describe 'harvests' do let(:event) { OpenStruct.new(id: harvest.id, event_type: 'harvest') } + it { expect(subject).to eq harvest } it { expect(event_description(event)).to have_text "harvested" } end describe 'seeds' do let(:event) { OpenStruct.new(id: seed.id, event_type: 'seed') } + it { expect(subject).to eq seed } it { expect(event_description(event)).to have_text "saved #{seed.crop.name} seeds" } end describe 'posts' do let(:event) { OpenStruct.new(id: post.id, event_type: 'post') } + it { expect(subject).to eq post } it { expect(event_description(event)).to have_text 'wrote a post' } end describe 'comments' do let(:event) { OpenStruct.new(id: comment.id, event_type: 'comment') } + it { expect(subject).to eq comment } it { expect(event_description(event)).to have_text 'commented on' } end describe 'photos' do let(:event) { OpenStruct.new(id: photo.id, event_type: 'photo') } + it { expect(subject).to eq photo } it { expect(event_description(event)).to have_text "took a photo" } end diff --git a/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb index 8efde0465..dffebaafe 100644 --- a/spec/helpers/photos_helper_spec.rb +++ b/spec/helpers/photos_helper_spec.rb @@ -52,7 +52,9 @@ describe PhotosHelper do describe "has a flickr photo" do let(:garden_photo) { FactoryBot.create(:photo, owner: garden.owner, source: 'flickr') } + before { garden.photos << garden_photo } + it { is_expected.to eq garden_photo.fullsize_url } end end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index f65a1146e..f5725650f 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -18,12 +18,14 @@ describe Ability do recipient: FactoryBot.create(:member)) ability.should_not be_able_to(:read, notification) end + it "member can't send messages to themself" do ability.should_not be_able_to(:create, FactoryBot.create(:notification, recipient: member, sender: member)) end + it "member can send messages to someone else" do ability.should be_able_to(:create, FactoryBot.create(:notification, @@ -64,9 +66,11 @@ describe Ability do it "can create crops" do ability.should be_able_to(:create, Crop) end + it "can update crops" do ability.should be_able_to(:update, crop) end + it "can destroy crops" do ability.should be_able_to(:destroy, crop) end @@ -80,6 +84,7 @@ describe Ability do it "can read plant parts" do ability.should be_able_to(:read, plant_part) end + it "can't manage plant parts" do ability.should_not be_able_to(:create, PlantPart) ability.should_not be_able_to(:update, plant_part) @@ -97,6 +102,7 @@ describe Ability do it "can read plant_part details" do ability.should be_able_to(:read, plant_part) end + it "can manage plant_part details" do ability.should be_able_to(:create, PlantPart) ability.should be_able_to(:update, plant_part) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index a02491bad..15235f83e 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -37,7 +37,7 @@ describe Comment do @p = FactoryBot.create(:post, author: @m) expect do FactoryBot.create(:comment, post: @p, author: @m) - end.to change(Notification, :count).by(0) + end.not_to change(Notification, :count) end end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 1286d3dfa..16a9fee0b 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -152,6 +152,7 @@ describe Crop do before { planting.photos << photo } it { expect(crop.default_photo).to eq photo } + include_examples 'has default photo' end @@ -162,6 +163,7 @@ describe Crop do before { harvest.photos << photo } it { expect(crop.default_photo).to eq photo } + include_examples 'has default photo' context 'and planting photo' do @@ -301,6 +303,7 @@ describe Crop do let(:crop2_planting) { crop2.plantings.first } let(:member) { FactoryBot.create :member, login_name: 'pikachu' } + describe 'lists interesting crops' do before do # they need 3+ plantings each to be interesting diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index f3349dd24..32d71cac9 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -138,6 +138,7 @@ describe Garden do described_class.active.should include active described_class.active.should_not include inactive end + it 'includes inactive garden in inactive scope' do described_class.inactive.should include inactive described_class.inactive.should_not include active diff --git a/spec/models/member_spec.rb b/spec/models/member_spec.rb index eed5bf464..ab678d6ef 100644 --- a/spec/models/member_spec.rb +++ b/spec/models/member_spec.rb @@ -138,21 +138,25 @@ describe Member do member.should_not be_valid member.errors[:login_name].should include("should be between 2 and 25 characters long") end + it "doesn't allow really long names" do member = FactoryBot.build(:invalid_member_longname) member.should_not be_valid member.errors[:login_name].should include("should be between 2 and 25 characters long") end + it "doesn't allow spaces in names" do member = FactoryBot.build(:invalid_member_spaces) member.should_not be_valid member.errors[:login_name].should include("may only include letters, numbers, or underscores") end + it "doesn't allow other chars in names" do member = FactoryBot.build(:invalid_member_badchars) member.should_not be_valid member.errors[:login_name].should include("may only include letters, numbers, or underscores") end + it "doesn't allow reserved names" do member = FactoryBot.build(:invalid_member_badname) member.should_not be_valid @@ -165,10 +169,12 @@ describe Member do member = FactoryBot.build(:valid_member_alphanumeric) member.should be_valid end + it "allows uppercase chars in names" do member = FactoryBot.build(:valid_member_uppercase) member.should be_valid end + it "allows underscores in names" do member = FactoryBot.build(:valid_member_underscore) member.should be_valid diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 3b258e7d0..d3fd558ed 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -27,13 +27,16 @@ describe Photo do describe 'with a second older photo' do let(:old_photo) { FactoryBot.create(:photo, owner: member, created_at: 1.year.ago, date_taken: 2.years.ago) } + # Add an old photo before { planting.photos << old_photo } + it { expect(planting.default_photo).to eq photo } it { expect(planting.crop.default_photo).to eq photo } describe 'and someone likes the old photo' do before { FactoryBot.create :like, likeable: old_photo } + it { expect(planting.default_photo).to eq old_photo } it { expect(planting.crop.default_photo).to eq old_photo } end @@ -42,6 +45,7 @@ describe Photo do describe 'to a harvest' do let(:crop) { harvest.crop } + before { harvest.photos << photo } it { expect(harvest.photos).to eq [photo] } @@ -247,6 +251,7 @@ describe Photo do it "finds just one" do expect(described_class.search.count).to eq 1 end + it "finds the matching photo" do expect(described_class.search).to include photo end @@ -259,6 +264,7 @@ describe Photo do it "finds photos by owner in search index" do expect(described_class.search(where: { owner_id: planting.owner_id })).to include photo end + it "finds photos by crop in search index" do expect(described_class.search(where: { crops: planting.crop.id })).to include photo end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index f145b0e7e..d454dc8b0 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -140,6 +140,7 @@ describe Planting do end it { expect(crop.median_days_to_first_harvest).to eq(20) } + describe 'sets median time to harvest' do let(:planting) { FactoryBot.create :planting, crop:, planted_at: Time.zone.today } @@ -211,7 +212,9 @@ describe Planting do describe 'planting perennial' do let(:crop) { FactoryBot.create(:crop, name: 'feijoa', perennial: true) } + it { expect(planting.perennial?).to be true } + describe 'no harvest to predict from' do it { expect(planting.harvest_months).to eq({}) } end @@ -222,6 +225,7 @@ describe Planting do FactoryBot.create :harvest, planting: planting, crop: crop, harvested_at: '18 June 2019' FactoryBot.create_list :harvest, 4, planting:, crop:, harvested_at: '18 August 2019' end + it { expect(planting.harvest_months).to eq(5 => 1, 6 => 1, 8 => 4) } end @@ -252,9 +256,11 @@ describe Planting do FactoryBot.create_list :harvest, 4, planting: faraway_planting, crop:, harvested_at: '18 December 2006' end + it { expect(planting.harvest_months).to eq(5 => 1, 6 => 1, 8 => 4) } end end + it 'has an owner' do planting.owner.should be_an_instance_of Member end @@ -277,10 +283,12 @@ describe Planting do planting = FactoryBot.create :planting, planted_at: Time.zone.today + 1 expect(planting.planted?).to be(false) end + it "is false for never planted" do planting = FactoryBot.create :planting, planted_at: nil expect(planting.planted?).to be(false) end + it "is true for past plantings" do planting = FactoryBot.create :planting, planted_at: Time.zone.today - 1 expect(planting.planted?).to be(true) @@ -518,9 +526,11 @@ describe Planting do it "planting has a parent seed" do expect(planting.parent_seed).to eq(parent_seed) end + it "seed has a child planting" do expect(parent_seed.child_plantings).to eq [planting] end + describe 'grandchildren' do let(:grandchild_seed) { FactoryBot.create :seed, parent_planting: planting } @@ -537,6 +547,7 @@ describe Planting do let!(:finished_planting) do FactoryBot.create :finished_planting, owner: member, garden: member.gardens.first end + it { expect(member.plantings.active).to include(planting) } it { expect(member.plantings.active).not_to include(finished_planting) } end @@ -547,6 +558,7 @@ describe Planting do let!(:planting) { FactoryBot.create :planting, :reindex } before { described_class.reindex } + subject { described_class.homepage_records(100) } it { expect(subject.count).to eq 2 } diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 8ead28dd0..8c0140771 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -128,7 +128,7 @@ describe Post do it "doesn't send notifications if you mention yourself" do expect do FactoryBot.create(:post, author: member, body: "@#{member}") - end.to change(Notification, :count).by(0) + end.not_to change(Notification, :count) end end diff --git a/spec/models/seed_spec.rb b/spec/models/seed_spec.rb index c9e8c26a3..55b4302a7 100644 --- a/spec/models/seed_spec.rb +++ b/spec/models/seed_spec.rb @@ -169,6 +169,7 @@ describe Seed do it "seed has a parent planting" do expect(seed.parent_planting).to eq(parent_planting) end + it "planting has a child seed" do expect(parent_planting.child_seeds).to eq [seed] end @@ -204,6 +205,7 @@ describe Seed do let!(:untradable_seed) { FactoryBot.create :untradable_seed, :reindex } before { described_class.reindex } + subject { described_class.homepage_records(100) } it { expect(subject.count).to eq 1 } diff --git a/spec/routing/member_routing_spec.rb b/spec/routing/member_routing_spec.rb index 790ff94b1..367656d83 100644 --- a/spec/routing/member_routing_spec.rb +++ b/spec/routing/member_routing_spec.rb @@ -35,6 +35,7 @@ describe MembersController do it "routes to harvests#index" do get("/members/name/harvests").should route_to("harvests#index", member_slug: 'name') end + it "routes to plantings#index" do get("/members/name/plantings").should route_to("plantings#index", member_slug: 'name') end diff --git a/spec/services/crop_search_service_spec.rb b/spec/services/crop_search_service_spec.rb index 349f8b532..71adaacb4 100644 --- a/spec/services/crop_search_service_spec.rb +++ b/spec/services/crop_search_service_spec.rb @@ -14,6 +14,7 @@ RSpec.describe CropSearchService, type: :service do let!(:taewa) { FactoryBot.create(:crop, name: 'taewa') } let!(:zucchini) { FactoryBot.create(:crop, name: 'zucchini') } let!(:broccoli) { FactoryBot.create(:crop, name: 'broccoli') } + before do # Alternate name FactoryBot.create :alternate_name, name: 'fungus', crop: mushroom @@ -55,18 +56,23 @@ RSpec.describe CropSearchService, type: :service do describe 'biased to higher planting counts' do subject { search('mushroom') } + before do # Having plantings should bring these crops to the top of the search results FactoryBot.create_list :planting, 10, crop: white FactoryBot.create_list :planting, 4, crop: shitake Crop.reindex end + it { expect(subject.first).to eq 'white mushroom' } it { expect(subject.second).to eq 'shitake mushroom' } end + describe "biased to crops you've planted" do subject { described_class.search('mushroom', current_member: owner).map(&:name) } + let(:owner) { FactoryBot.create :member } + before do FactoryBot.create_list :planting, 10, crop: brown FactoryBot.create :planting, crop: oyster, owner: owner @@ -74,6 +80,7 @@ RSpec.describe CropSearchService, type: :service do FactoryBot.create :planting, crop: shitake, owner: owner Crop.reindex end + it { expect(subject.first).to eq oyster.name } it { expect(subject.second).to eq shitake.name } end diff --git a/spec/services/timeline_service_spec.rb b/spec/services/timeline_service_spec.rb index 87fe2c84d..57630f47a 100644 --- a/spec/services/timeline_service_spec.rb +++ b/spec/services/timeline_service_spec.rb @@ -14,7 +14,9 @@ describe TimelineService do describe 'a friend you followed' do before { friend.followers << member } + subject { TimelineService.followed_query(member) } + it { expect(subject.first.id).to eq friend_planting.id } it { expect(subject.first.event_type).to eq 'planting' } it { expect(subject.second.id).to eq friend_harvest.id } diff --git a/spec/views/devise/registrations/edit_spec.rb b/spec/views/devise/registrations/edit_spec.rb index 1ca0ae8ad..4721f507b 100644 --- a/spec/views/devise/registrations/edit_spec.rb +++ b/spec/views/devise/registrations/edit_spec.rb @@ -76,6 +76,7 @@ describe 'devise/registrations/edit.html.haml', type: "view" do it 'has a link to twitter profile' do assert_select "a", href: "http://twitter.com/#{@twitter_auth.name}" end + it 'has a link to disconnect' do render assert_select "a", href: @twitter_auth, text: "Disconnect" @@ -98,6 +99,7 @@ describe 'devise/registrations/edit.html.haml', type: "view" do it 'has a link to flickr photostream' do assert_select "a", href: "http://flickr.com/photos/#{@flickr_auth.uid}" end + it 'has a link to disconnect' do render assert_select "a", href: @flickr_auth, text: "Disconnect" diff --git a/spec/views/forums/index.html.haml_spec.rb b/spec/views/forums/index.html.haml_spec.rb index 07db35d39..c8078c183 100644 --- a/spec/views/forums/index.html.haml_spec.rb +++ b/spec/views/forums/index.html.haml_spec.rb @@ -6,6 +6,7 @@ describe "forums/index" do let(:admin) { FactoryBot.create(:admin_member) } let(:forum1) { FactoryBot.create(:forum) } let(:forum2) { FactoryBot.create(:forum) } + before do controller.stub(:current_user) { admin } assign(:forums, [forum1, forum2]) @@ -24,6 +25,7 @@ describe "forums/index" do context "posts" do let!(:post) { FactoryBot.create(:forum_post, forum: forum1) } let!(:comment) { FactoryBot.create(:comment, post:) } + before { render } describe "displays posts" do diff --git a/spec/views/harvests/index.rss.haml_spec.rb b/spec/views/harvests/index.rss.haml_spec.rb index e4384c474..6246f57c0 100644 --- a/spec/views/harvests/index.rss.haml_spec.rb +++ b/spec/views/harvests/index.rss.haml_spec.rb @@ -18,6 +18,7 @@ describe 'harvests/index.rss.haml', :search do context 'all harvests' do before { render } + it 'shows RSS feed title' do expect(rendered).to have_content "Recent harvests from all members" end @@ -32,6 +33,7 @@ describe 'harvests/index.rss.haml', :search do assign(:crop, @tomato) render end + it "displays crop's name in title" do expect(rendered).to have_content @tomato.name end diff --git a/spec/views/home/_seeds.html.haml_spec.rb b/spec/views/home/_seeds.html.haml_spec.rb index a447329d5..142b8f1e1 100644 --- a/spec/views/home/_seeds.html.haml_spec.rb +++ b/spec/views/home/_seeds.html.haml_spec.rb @@ -5,6 +5,7 @@ require 'rails_helper' describe 'home/_seeds.html.haml', search: true, type: "view" do let!(:seed) { FactoryBot.create(:tradable_seed, owner:) } let(:owner) { FactoryBot.create(:london_member) } + before do Seed.searchkick_index.refresh render diff --git a/spec/views/layouts/_header_spec.rb b/spec/views/layouts/_header_spec.rb index b87bcb4d6..a06b2784e 100644 --- a/spec/views/layouts/_header_spec.rb +++ b/spec/views/layouts/_header_spec.rb @@ -64,15 +64,19 @@ describe 'layouts/_header.html.haml', type: "view" do it 'has member login name' do rendered.should have_content @member.login_name.to_s end + it "shows link to member's gardens" do assert_select("a[href='#{member_gardens_path(@member)}']", "Gardens") end + it "shows link to member's plantings" do assert_select("a[href='#{member_plantings_path(@member)}']", "Plantings") end + it "shows link to member's seeds" do assert_select("a[href='#{member_seeds_path(@member)}']", "Seeds") end + it "shows link to member's posts" do assert_select("a[href='#{member_posts_path(@member)}']", "Posts") end diff --git a/spec/views/plantings/show.html.haml_spec.rb b/spec/views/plantings/show.html.haml_spec.rb index 8281092f3..fb1228f64 100644 --- a/spec/views/plantings/show.html.haml_spec.rb +++ b/spec/views/plantings/show.html.haml_spec.rb @@ -24,6 +24,7 @@ describe "plantings/show" do describe "shows the sunniness" do before { render } + it { expect(rendered).to have_content 'Planted in' } it { expect(rendered).to have_content 'sun' } end @@ -34,6 +35,7 @@ describe "plantings/show" do describe "shows planted_from" do before { render } + it { expect(rendered).to have_content 'Grown from' } it { expect(rendered).to have_content 'cutting' } end @@ -43,6 +45,7 @@ describe "plantings/show" do planting.update(planted_from: '') render end + it { expect(rendered).not_to have_content 'Planted from' } end end @@ -59,6 +62,7 @@ describe "plantings/show" do describe "shows a link to add photos" do before { render } + it { expect(rendered).to have_content "Add photo" } end diff --git a/spec/views/posts/new.html.haml_spec.rb b/spec/views/posts/new.html.haml_spec.rb index a40755f5a..663cf5ead 100644 --- a/spec/views/posts/new.html.haml_spec.rb +++ b/spec/views/posts/new.html.haml_spec.rb @@ -52,6 +52,7 @@ describe "posts/new" do it 'tells the user what forum it will be posted in' do expect(rendered).to have_content "This post will be posted in the forum #{forum.name}" end + it { expect(rendered).to have_link forum.name } describe "asks what's going on generally" do diff --git a/spec/views/scientific_names/edit.html.haml_spec.rb b/spec/views/scientific_names/edit.html.haml_spec.rb index e99988f8a..0b5fdaa4a 100644 --- a/spec/views/scientific_names/edit.html.haml_spec.rb +++ b/spec/views/scientific_names/edit.html.haml_spec.rb @@ -17,6 +17,7 @@ describe "scientific_names/edit" do it "shows the creator" do expect(rendered).to have_content "Added by #{member} less than a minute ago." end + it { expect(rendered).to have_link member_path(member) } it "renders the edit scientific_name form" do diff --git a/spec/views/seeds/index.rss.haml_spec.rb b/spec/views/seeds/index.rss.haml_spec.rb index 42bf396f2..a641d4f02 100644 --- a/spec/views/seeds/index.rss.haml_spec.rb +++ b/spec/views/seeds/index.rss.haml_spec.rb @@ -24,6 +24,7 @@ describe 'seeds/index.rss.haml', :search do context 'all seeds' do let!(:seed) { FactoryBot.create(:seed) } let!(:tradable) { FactoryBot.create(:tradable_seed) } + before do Seed.searchkick_index.refresh assign(:seeds, Seed.search(load: false))