diff --git a/heroesofddd_rails_application/test/controllers/heroes/dwelling_integration_test.rb b/heroesofddd_rails_application/test/controllers/heroes/dwelling_integration_test.rb index 9f7d2b4..13a80e8 100644 --- a/heroesofddd_rails_application/test/controllers/heroes/dwelling_integration_test.rb +++ b/heroesofddd_rails_application/test/controllers/heroes/dwelling_integration_test.rb @@ -1,16 +1,17 @@ require "test_helper" +require "building_blocks/application/app_context" class DwellingsIntegrationTest < ActionDispatch::IntegrationTest include EventStoreTest def setup + super @dwelling_id = SecureRandom.uuid @creature_id = "angel" - @cost_per_troop = Heroes::SharedKernel::Resources::Cost.resources([ :GOLD, 3000 ], [ :GEM, 1 ]) - + @cost_per_troop = Heroes::SharedKernel::Resources::Cost.resources([:GOLD, 3000], [:GEM, 1]) @game_id = SecureRandom.uuid @app_context = ::BuildingBlocks::Application::AppContext.for_game(@game_id) - @stream_name ="Game::$#{@game_id}::CreatureRecruitment::Dwelling$#{@dwelling_id}" + @stream_name = "Game::$#{@game_id}::CreatureRecruitment::Dwelling$#{@dwelling_id}" end test "viewing the recruitment page" do @@ -26,7 +27,6 @@ def setup assert_select "#dwelling-#{@dwelling_id}" do assert_select ".recruitment__title", "Recruit Angels" assert_select ".recruitment__count-value", "10" - assert_select "#recruit-count", "0" assert_select ".recruitment__slider-input[max='10']" end end @@ -37,11 +37,13 @@ def setup given_domain_event(@stream_name, Heroes::CreatureRecruitment::AvailableCreaturesChanged.new(@dwelling_id, @creature_id, 10)) # When - post recruit_heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id), params: { recruit_count: 5 } + post recruit_heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id), + params: { recruit_count: 5 }, + as: :turbo_stream # Then - assert_redirected_to heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id) - follow_redirect! + assert_response :success + assert_turbo_stream action: :replace, target: "dwelling-#{@dwelling_id}" assert_select "#dwelling-#{@dwelling_id}" do assert_select ".recruitment__count-value", "5" end @@ -58,38 +60,25 @@ def setup assert_response :not_found end - test "updating total cost when changing recruit count" do - # Given - given_domain_event(@stream_name, Heroes::CreatureRecruitment::DwellingBuilt.new(@dwelling_id, @creature_id, @cost_per_troop)) - given_domain_event(@stream_name, Heroes::CreatureRecruitment::AvailableCreaturesChanged.new(@dwelling_id, @creature_id, 10)) - - # When - get heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id) - - # Then - assert_response :success - assert_select "script", /container\.querySelector\('#total-gold'\)\.textContent = count \* 3000/ - assert_select "script", /container\.querySelector\('#total-gem'\)\.textContent = count \* 1/ - end - test "recruiting zero creatures" do # Given given_domain_event(@stream_name, Heroes::CreatureRecruitment::DwellingBuilt.new(@dwelling_id, @creature_id, @cost_per_troop)) given_domain_event(@stream_name, Heroes::CreatureRecruitment::AvailableCreaturesChanged.new(@dwelling_id, @creature_id, 10)) # When - post recruit_heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id), params: { recruit_count: 0 } + post recruit_heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id), + params: { recruit_count: 0 }, + as: :turbo_stream # Then - assert_redirected_to heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id) - follow_redirect! + assert_response :success + assert_turbo_stream action: :replace, target: "dwelling-#{@dwelling_id}" assert_select "#dwelling-#{@dwelling_id}" do assert_select ".recruitment__count-value", "10" - assert_select ".recruitment__message-box__text", "Please select at least one creature to recruit." end end def default_app_context @app_context end -end +end \ No newline at end of file diff --git a/heroesofddd_rails_application/test/controllers/heroes/dwelling_system_test_case.rb b/heroesofddd_rails_application/test/controllers/heroes/dwelling_system_test_case.rb new file mode 100644 index 0000000..5317612 --- /dev/null +++ b/heroesofddd_rails_application/test/controllers/heroes/dwelling_system_test_case.rb @@ -0,0 +1,58 @@ +require "application_system_test_case" +require "heroes/creature_recruitment/write/build_dwelling/event_dwelling_built" +require "heroes/creature_recruitment/write/change_available_creatures/event_available_creatures_changed" +require "building_blocks/application/app_context" + +class DwellingSystemTest < ApplicationSystemTestCase + include EventStoreTest + + def setup + super + @dwelling_id = SecureRandom.uuid + @creature_id = "angel" + @cost_per_troop = Heroes::SharedKernel::Resources::Cost.resources([:GOLD, 3000], [:GEM, 1]) + @game_id = SecureRandom.uuid + @app_context = ::BuildingBlocks::Application::AppContext.for_game(@game_id) + @stream_name = "Game::$#{@game_id}::CreatureRecruitment::Dwelling$#{@dwelling_id}" + end + + test "updates total cost when changing recruit count" do + # given + given_domain_event(@stream_name, Heroes::CreatureRecruitment::DwellingBuilt.new(@dwelling_id, @creature_id, @cost_per_troop)) + given_domain_event(@stream_name, Heroes::CreatureRecruitment::AvailableCreaturesChanged.new(@dwelling_id, @creature_id, 10)) + + # when + visit heroes_game_creature_recruitment_dwelling_path(@game_id, @dwelling_id) + + # then + assert_selector ".recruitment__title", text: "Recruit Angels" + assert_selector ".recruitment__count-value", text: "10" + + # and: initially, the recruit count should be 0 + assert_selector "[data-dwelling-target='recruitCount']", text: "0" + assert_selector "#total-gold", text: "0" + assert_selector "#total-gem", text: "0" + + # when: change the recruit count to 5 + find(".recruitment__slider-input").set(5) + + # then: updated the totals + assert_selector "[data-dwelling-target='recruitCount']", text: "5" + assert_selector "#total-gold", text: "15000" + assert_selector "#total-gem", text: "5" + + # whenL change the recruit count to the maximum (10) + find(".recruitment__slider-input").set(10) + + # then: updated the totals + assert_selector "[data-dwelling-target='recruitCount']", text: "10" + assert_selector "#total-gold", text: "30000" + assert_selector "#total-gem", text: "10" + end + + private + + def default_app_context + @app_context + end +end \ No newline at end of file