|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class ContentBlockManager::ContentBlockEdition::Details::Fields::CountryComponentTest < ViewComponent::TestCase |
| 4 | + extend Minitest::Spec::DSL |
| 5 | + |
| 6 | + let(:content_block_edition) { build(:content_block_edition, :email_address) } |
| 7 | + let(:field) { stub("field", name: "country") } |
| 8 | + |
| 9 | + let(:world_locations) { build_list(:world_location, 5) } |
| 10 | + |
| 11 | + before do |
| 12 | + WorldLocation.stubs(:all).returns(world_locations) |
| 13 | + end |
| 14 | + |
| 15 | + it "should render an select field populated with WorldLocations" do |
| 16 | + render_inline( |
| 17 | + ContentBlockManager::ContentBlockEdition::Details::Fields::CountryComponent.new( |
| 18 | + content_block_edition:, |
| 19 | + field:, |
| 20 | + ), |
| 21 | + ) |
| 22 | + |
| 23 | + expected_name = "content_block/edition[details][country]" |
| 24 | + expected_id = "#{ContentBlockManager::ContentBlockEdition::Details::Fields::BaseComponent::PARENT_CLASS}_details_country" |
| 25 | + |
| 26 | + assert_selector "label", text: "Country" |
| 27 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"]" |
| 28 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"] option[value=\"\"]" |
| 29 | + |
| 30 | + world_locations.each do |location| |
| 31 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"] option[value=\"#{location.name}\"]", text: location.name |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + it "should show an option as selected when value is given" do |
| 36 | + render_inline( |
| 37 | + ContentBlockManager::ContentBlockEdition::Details::Fields::CountryComponent.new( |
| 38 | + content_block_edition:, |
| 39 | + field:, |
| 40 | + value: world_locations.first.name, |
| 41 | + ), |
| 42 | + ) |
| 43 | + |
| 44 | + expected_name = "content_block/edition[details][country]" |
| 45 | + expected_id = "#{ContentBlockManager::ContentBlockEdition::Details::Fields::BaseComponent::PARENT_CLASS}_details_country" |
| 46 | + |
| 47 | + assert_selector "label", text: "Country" |
| 48 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"]" |
| 49 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"] option[value=\"\"]" |
| 50 | + |
| 51 | + world_locations.each do |location| |
| 52 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"] option[value=\"#{location.name}\"]", text: location.name |
| 53 | + end |
| 54 | + |
| 55 | + assert_selector "select[name=\"#{expected_name}\"][id=\"#{expected_id}\"] option[value=\"#{world_locations.first.name}\"][selected]", text: world_locations.first.name |
| 56 | + end |
| 57 | + |
| 58 | + it "should show errors when present" do |
| 59 | + content_block_edition.errors.add(:details_country, "Some error goes here") |
| 60 | + |
| 61 | + render_inline( |
| 62 | + ContentBlockManager::ContentBlockEdition::Details::Fields::CountryComponent.new( |
| 63 | + content_block_edition:, |
| 64 | + field:, |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + assert_selector ".govuk-form-group--error" |
| 69 | + assert_selector ".govuk-error-message", text: "Some error goes here" |
| 70 | + assert_selector "select.govuk-select--error" |
| 71 | + end |
| 72 | +end |
0 commit comments