Skip to content

Commit

Permalink
Merge pull request #82 from motohiro-mm/feature/add_test_of_meal_plans
Browse files Browse the repository at this point in the history
MealPlanのシステムテストを追加
  • Loading branch information
motohiro-mm authored Sep 24, 2024
2 parents 893eb12 + ce7e364 commit 9153496
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
bundler-cache: true
- name: Set up database schema
run: bin/rails db:schema:load
- name: Assets precompile
run: bundle exec rake assets:precompile
- name: Run tests
run: bundle exec rspec
lint:
Expand Down
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ Metrics/AbcSize:

Rails/I18nLocaleTexts:
Enabled: false

Metrics/MethodLength:
Max: 20

RSpec/ExampleLength:
Max: 20

RSpec/MultipleExpectations:
Enabled: false
19 changes: 12 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ gem 'tzinfo-data', platforms: %i[windows jruby]
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false

gem 'rexml', require: false

gem 'omniauth-google-oauth2'
gem 'omniauth-rails_csrf_protection'

gem 'simple_calendar'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri windows], require: 'debug/prelude'
Expand Down Expand Up @@ -67,3 +60,15 @@ group :development do
gem 'rubocop-performance', require: false
gem 'rubocop-rails', require: false
end

group :test do
gem 'capybara', require: false
gem 'selenium-webdriver', require: false
end

gem 'rexml', require: false

gem 'omniauth-google-oauth2'
gem 'omniauth-rails_csrf_protection'

gem 'simple_calendar'
50 changes: 37 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ GEM
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
base64 (0.2.0)
better_html (2.1.1)
Expand All @@ -87,6 +89,15 @@ GEM
brakeman (6.2.1)
racc
builder (3.3.0)
capybara (3.40.0)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.11)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
Expand Down Expand Up @@ -118,7 +129,7 @@ GEM
globalid (1.2.1)
activesupport (>= 6.1)
hashie (5.0.0)
i18n (1.14.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
Expand All @@ -129,7 +140,7 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
jwt (2.9.0)
jwt (2.9.1)
base64
language_server-protocol (3.17.0.3)
logger (1.6.1)
Expand All @@ -142,6 +153,7 @@ GEM
net-pop
net-smtp
marcel (1.0.4)
matrix (0.4.2)
mini_mime (1.1.5)
minitest (5.25.1)
msgpack (1.7.2)
Expand Down Expand Up @@ -200,7 +212,8 @@ GEM
pg (1.5.8)
psych (5.1.2)
stringio
puma (6.4.2)
public_suffix (6.0.1)
puma (6.4.3)
nio4r (~> 2.0)
racc (1.8.1)
rack (3.1.7)
Expand Down Expand Up @@ -287,15 +300,22 @@ GEM
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.26.1)
rubocop-rails (2.26.2)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rspec (3.0.5)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
rubyzip (2.3.2)
securerandom (0.3.1)
selenium-webdriver (4.25.0)
base64 (~> 0.2)
logger (~> 1.4)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
simple_calendar (3.0.4)
rails (>= 6.1)
smart_properties (1.17.0)
Expand All @@ -312,23 +332,22 @@ GEM
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.1)
tailwindcss-rails (2.7.4)
tailwindcss-rails (2.7.6)
railties (>= 7.0.0)
tailwindcss-rails (2.7.4-aarch64-linux)
tailwindcss-rails (2.7.6-aarch64-linux)
railties (>= 7.0.0)
tailwindcss-rails (2.7.4-arm-linux)
tailwindcss-rails (2.7.6-arm-linux)
railties (>= 7.0.0)
tailwindcss-rails (2.7.4-arm64-darwin)
tailwindcss-rails (2.7.6-arm64-darwin)
railties (>= 7.0.0)
tailwindcss-rails (2.7.4-x86_64-darwin)
tailwindcss-rails (2.7.6-x86_64-darwin)
railties (>= 7.0.0)
tailwindcss-rails (2.7.4-x86_64-linux)
tailwindcss-rails (2.7.6-x86_64-linux)
railties (>= 7.0.0)
thor (1.3.2)
timeout (0.4.1)
turbo-rails (2.0.7)
turbo-rails (2.0.10)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -341,10 +360,13 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webrick (1.8.1)
webrick (1.8.2)
websocket (1.2.11)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.18)

PLATFORMS
Expand All @@ -358,6 +380,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
brakeman
capybara
debug
erb_lint
factory_bot_rails
Expand All @@ -375,6 +398,7 @@ DEPENDENCIES
rubocop-performance
rubocop-rails
rubocop-rspec
selenium-webdriver
simple_calendar
sprockets-rails
stimulus-rails
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

module ApplicationHelper
def css_class_day(day)
day.strftime('date%Y%m%d')
end
end
3 changes: 3 additions & 0 deletions app/helpers/meal_plans_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

module MealPlansHelper
def meal_timing(meal_form)
Meal.timings.key(meal_form.index)
end
end
16 changes: 8 additions & 8 deletions app/views/meal_plans/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
</div>

<%= form.fields_for :meals do |meal_form| %>
<strong><%= Meal.timings.key(meal_form.index) %></strong>
<%= meal_form.hidden_field :timing, value: Meal.timings.key(meal_form.index) %>
<div class="my-5">
<%= meal_form.label :name %>
<%= meal_form.text_field :name,
class: 'block rounded-md border border-gray-400 px-3 py-2 mt-2 w-full' %>
</div>

<div class="<%= meal_timing(meal_form) %>">
<strong><%= meal_timing(meal_form) %></strong>
<%= meal_form.hidden_field :timing, value: meal_timing(meal_form) %>
<div class="my-5">
<%= meal_form.label :name %>
<%= meal_form.text_field :name,
class: 'block rounded-md border border-gray-400 px-3 py-2 mt-2 w-full' %>
<%= meal_form.label :memo %>
<%= meal_form.text_area :memo, rows: 4,
class: 'block rounded-md border border-gray-400 px-3 py-2 mt-2 w-full' %>
</div>
</div>
<% end %>

<div class="inline">
Expand Down
2 changes: 1 addition & 1 deletion app/views/simple_calendar/_week_calendar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<table class="w-full table-fixed border-collapse">
<tbody>
<% date_range.each do |day| %>
<tr class="border-t">
<tr class="<%= css_class_day(day) %> border-t">
<th class="text-left p-4 w-1/4 text-lg font-semibold text-stone-500 align-top">
<span class="text-stone-800"><%= day.strftime('%-m月%-d日') %></span>
<%= t('date.abbr_day_names')[day.wday] %>
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/meal_plans.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :meal_plan do
sequence(:meal_date) { |n| Time.zone.today.days_since(n) }
family
end
end
9 changes: 9 additions & 0 deletions spec/factories/meals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :meal do
sequence(:name) { |n| "TestMeal#{n}" }
sequence(:memo) { |n| "TestMemo#{n}" }
meal_plan
end
end
4 changes: 3 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }
Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
Expand Down Expand Up @@ -70,4 +70,6 @@
# config.filter_gems_from_backtrace("gem name")

config.include FactoryBot::Syntax::Methods
config.include LoginSupport, type: :system
config.include ApplicationHelper
end
11 changes: 11 additions & 0 deletions spec/support/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end

config.before(:each, :js, type: :system) do
driven_by :selenium_chrome_headless
end
end
17 changes: 17 additions & 0 deletions spec/support/login_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module LoginSupport
def log_in_as(user)
OmniAuth.configure do |config|
config.test_mode = true
config.mock_auth[:google_oauth2] =
OmniAuth::AuthHash.new({
provider: user.provider,
uid: user.uid,
info: { name: user.name }
})
end
visit root_path
click_on 'Googleでログイン'
end
end
62 changes: 62 additions & 0 deletions spec/system/meal_plans_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'MealPlans', type: :system do
let(:user) { create(:user) }
let(:meal_plan) { create(:meal_plan, family: user.family) }
let!(:meal_breakfast) { create(:meal, :breakfast, meal_plan:) }
let!(:meal_lunch) { create(:meal, :lunch, meal_plan:) }
let!(:meal_dinner) { create(:meal, :dinner, meal_plan:) }

before do
log_in_as user
end

it '献立を登録する' do
visit new_meal_plan_path(meal_date: Time.zone.today)

within '.breakfast' do
fill_in 'Name', with: 'シリアル'
end
within '.lunch' do
fill_in 'Name', with: 'ハンバーガー'
end
within '.dinner' do
fill_in 'Name', with: 'ステーキ'
end
click_on 'Create Meal plan'

expect(page).to have_css '#notice', text: 'MealPlan was successfully created.'
expect(page).to have_content Time.zone.today
expect(page).to have_content 'シリアル'
expect(page).to have_content 'ハンバーガー'
expect(page).to have_content 'ステーキ'
end

it '献立を詳細表示する' do
visit meal_plans_path(start_date: meal_plan.meal_date)
within ".#{css_class_day(meal_plan.meal_date)}" do
click_on '詳細 >'
end

expect(page).to have_css 'h2', text: meal_plan.meal_date
expect(page).to have_content meal_breakfast.name
expect(page).to have_content meal_lunch.name
expect(page).to have_content meal_dinner.name
end

it '献立を編集する' do
visit meal_plan_path(meal_plan)
click_on 'Edit this meal plan'

expect(page).to have_css 'h1', text: 'Editing meal_plan'
within '.lunch' do
fill_in 'Name', with: 'EditName'
end
click_on 'Update Meal plan'

expect(page).to have_css '#notice', text: 'MealPlan was successfully updated.'
expect(page).to have_content 'EditName'
end
end

0 comments on commit 9153496

Please sign in to comment.