Skip to content

Commit

Permalink
dwelling integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszNaKodach committed Sep 29, 2024
1 parent aa36540 commit a882e86
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a882e86

Please sign in to comment.