diff --git a/db/seeds.rb b/db/seeds.rb
index 4c325cf..2867d18 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -16,12 +16,32 @@ def maybe(&block)
# Production should not have any seeded data for the content or schools
return if Rails.env.production?
+fruit_veggie_quiz = Game.find_or_create_by!(slug: "fruit-veggie-quiz") do |game|
+ game.title = "Fruit and Veggie Quiz"
+ game.description = "Sort fruits and vegetables into the right food groups."
+end
+sleep_habits = Game.find_or_create_by!(slug: "sleep-habits") do |game|
+ game.title = "Sleep Habits"
+ game.description = "Pick the habits that help you get a good night's sleep."
+end
+# Intentionally not attached to any module so the "Add game" dropdown has an option.
+Game.find_or_create_by!(slug: "hydration-heroes") do |game|
+ game.title = "Hydration Heroes"
+ game.description = "Learn how much water your body needs each day."
+end
+games_by_program = { programs.first => fruit_veggie_quiz, programs.last => sleep_habits }
+
+# The first module of each program and level gets a game after its survey.
+# The second module of the basic level gets its game before the survey.
ContentModule.levels.keys.each do |level|
programs.each do |program|
+ game = games_by_program.fetch(program)
2.times do |i|
mod = program.content_modules.create!(name: "#{program.name} #{level.humanize} Module #{i + 1}", level: level)
link = Link.new(title: "Survey #{i + 1}", url: "https://example.com/survey/#{program.id}-#{level}-#{i}", link_type: "survey")
+ mod.contents.create!(contentable: game) if i == 1 && level == "basic"
mod.contents.create!(contentable: link)
+ mod.contents.create!(contentable: game) if i == 0
end
end
end
diff --git a/test/controllers/admin/content_modules_controller_test.rb b/test/controllers/admin/content_modules_controller_test.rb
index 07152cb..675d9ee 100644
--- a/test/controllers/admin/content_modules_controller_test.rb
+++ b/test/controllers/admin/content_modules_controller_test.rb
@@ -77,6 +77,18 @@ class Admin::ContentModulesControllerTest < ActionDispatch::IntegrationTest
get edit_admin_content_module_url(@content_module)
assert_select "select[name='content[contentable_id]']", count: 0
+ assert_select "p", text: /All games are in this module\./
+ end
+
+ test "edit links to creating a game when no games exist" do
+ Content.where(contentable_type: "Game").destroy_all
+ Game.destroy_all
+
+ get edit_admin_content_module_url(@content_module)
+
+ assert_select "select[name='content[contentable_id]']", count: 0
+ assert_select "p", text: /No games yet/
+ assert_select "a.link[href='#{new_admin_game_path}']", text: "Create a game"
end
test "index shows the contents count" do
diff --git a/test/controllers/admin/games_controller_test.rb b/test/controllers/admin/games_controller_test.rb
index 40da7d4..6e2d4a9 100644
--- a/test/controllers/admin/games_controller_test.rb
+++ b/test/controllers/admin/games_controller_test.rb
@@ -11,6 +11,12 @@ class Admin::GamesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
+ test "index marks the Games nav link as active" do
+ get admin_games_url
+
+ assert_select "nav a.active", text: "Games"
+ end
+
test "should get new" do
get new_admin_game_url
assert_response :success
@@ -26,6 +32,31 @@ class Admin::GamesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to admin_games_url
end
+ test "index shows how many modules use each game" do
+ get admin_games_url
+
+ assert_select "tr:has(a[href='#{admin_game_path(@game)}']) td:nth-child(3)", text: "1"
+ end
+
+ test "show lists the modules that use the game" do
+ get admin_game_url(@game)
+
+ assert_select "a[href=?]", edit_admin_content_module_path(content_modules(:one)), text: content_modules(:one).name
+ end
+
+ test "show says when the game is in no module" do
+ get admin_game_url(games(:two))
+
+ assert_select "p", text: "Not in any module yet."
+ end
+
+ test "delete confirmation warns when the game is in modules" do
+ get admin_game_url(@game)
+
+ assert_select "form[action=?] button[data-turbo-confirm=?]", admin_game_path(@game),
+ "Delete this game? It will be removed from 1 module and 1 student attempt will be deleted."
+ end
+
test "should get edit" do
get edit_admin_game_url(@game)
assert_response :success
diff --git a/test/helpers/admin/games_helper_test.rb b/test/helpers/admin/games_helper_test.rb
new file mode 100644
index 0000000..26b6425
--- /dev/null
+++ b/test/helpers/admin/games_helper_test.rb
@@ -0,0 +1,44 @@
+require "test_helper"
+
+class Admin::GamesHelperTest < ActionView::TestCase
+ setup do
+ @game = games(:one)
+ end
+
+ test "plain confirmation when the game has no modules or attempts" do
+ @game.contents.destroy_all
+ @game.game_attempts.destroy_all
+
+ assert_equal "Delete this game?", delete_game_confirmation(@game.reload)
+ end
+
+ test "warns about modules only" do
+ @game.game_attempts.destroy_all
+ contents(:one).dup.update!(content_module: content_modules(:two), position: 1)
+
+ assert_equal "Delete this game? It will be removed from 2 modules.", delete_game_confirmation(@game.reload)
+ end
+
+ test "warns about attempts only" do
+ @game = games(:two)
+ attempt = game_attempts(:two)
+ 4.times { |i| attempt.dup.update!(token: "extra-#{i}") }
+
+ assert_equal "Delete this game? 5 student attempts will be deleted.", delete_game_confirmation(@game.reload)
+ end
+
+ test "warns about modules and attempts" do
+ contents(:one).dup.update!(content_module: content_modules(:two), position: 1)
+ 4.times { |i| game_attempts(:one).dup.update!(token: "extra-#{i}") }
+
+ assert_equal "Delete this game? It will be removed from 2 modules and 5 student attempts will be deleted.",
+ delete_game_confirmation(@game.reload)
+ end
+
+ test "uses the preselected attempt count for games from with_attempt_counts" do
+ game = Game.includes(:contents).with_attempt_counts.find(@game.id)
+
+ assert_no_queries { delete_game_confirmation(game) }
+ assert_includes delete_game_confirmation(game), "1 student attempt will be deleted"
+ end
+end