Skip to content

Commit dc68d2d

Browse files
authored
Merge pull request #10217 from alphagov/CM-115-stop-embedded-object-form-from-inheriting-its-parents-value
(CM-115) Move calculation of value to the FormComponent
2 parents 6645d09 + 114a963 commit dc68d2d

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

lib/engines/content_block_manager/app/components/content_block_manager/content_block_edition/details/fields/base_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ContentBlockManager::ContentBlockEdition::Details::Fields::BaseComponent <
66
def initialize(content_block_edition:, field:, value: nil, object_id: nil)
77
@content_block_edition = content_block_edition
88
@field = field
9-
@value = value || content_block_edition.details&.fetch(field.name, nil)
9+
@value = value
1010
@object_id = object_id
1111
end
1212

lib/engines/content_block_manager/app/components/content_block_manager/content_block_edition/details/form_component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def component_args(field)
2020
{
2121
content_block_edition:,
2222
field:,
23+
value: content_block_edition.details&.fetch(field.name, nil),
2324
}
2425
end
2526
end

lib/engines/content_block_manager/test/components/content_block/edition/details/fields/string_component_test.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ class ContentBlockManager::ContentBlockEdition::Details::Fields::StringComponent
2121
assert_selector "input[type=\"text\"][name=\"#{expected_name}\"][id=\"#{expected_id}\"]"
2222
end
2323

24-
it "should show the value when present" do
25-
content_block_edition.details = { "email_address": "[email protected]" }
26-
24+
it "should show the value when provided" do
2725
render_inline(
2826
ContentBlockManager::ContentBlockEdition::Details::Fields::StringComponent.new(
2927
content_block_edition:,
3028
field:,
29+
3130
),
3231
)
3332

3433
assert_selector 'input[value="[email protected]"]'
3534
end
3635

36+
it "should show the value from an embedded object" do
37+
content_block_edition.details = { "description": "[email protected]" }
38+
end
39+
3740
it "should show errors when present" do
3841
content_block_edition.errors.add(:details_email_address, "Some error goes here")
3942

lib/engines/content_block_manager/test/components/content_block/edition/details/form_component_test.rb

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,25 @@ class ContentBlockManager::ContentBlockEdition::Details::FormComponentTest < Vie
3030
let(:bar_field) { stub("field", name: "bar", component_name: "string", enum_values: nil) }
3131
let(:baz_field) { stub("field", name: "baz", component_name: "enum", enum_values: %w[some enum]) }
3232

33+
let(:foo_stub) { stub("string_component") }
34+
let(:bar_stub) { stub("string_component") }
35+
let(:baz_stub) { stub("enum_component") }
36+
37+
let(:component) do
38+
ContentBlockManager::ContentBlockEdition::Details::FormComponent.new(
39+
content_block_edition:,
40+
schema:,
41+
)
42+
end
43+
3344
before do
3445
schema.stubs(:fields).returns([foo_field, bar_field, baz_field])
46+
component.expects(:render).with(foo_stub)
47+
component.expects(:render).with(bar_stub)
48+
component.expects(:render).with(baz_stub)
3549
end
3650

3751
it "renders fields for each property" do
38-
foo_stub = stub("string_component")
39-
bar_stub = stub("string_component")
40-
baz_stub = stub("enum_component")
41-
4252
ContentBlockManager::ContentBlockEdition::Details::Fields::StringComponent.expects(:new).with(
4353
content_block_edition:,
4454
field: foo_field,
@@ -55,15 +65,35 @@ class ContentBlockManager::ContentBlockEdition::Details::FormComponentTest < Vie
5565
enum: %w[some enum],
5666
).returns(baz_stub)
5767

58-
component = ContentBlockManager::ContentBlockEdition::Details::FormComponent.new(
68+
assert render_inline(component)
69+
end
70+
71+
it "sends values to the field components when the block has them" do
72+
content_block_edition.details = {
73+
"foo" => "foo value",
74+
"bar" => "bar value",
75+
"baz" => "baz value",
76+
}
77+
78+
ContentBlockManager::ContentBlockEdition::Details::Fields::StringComponent.expects(:new).with(
5979
content_block_edition:,
60-
schema:,
61-
)
80+
field: foo_field,
81+
value: "foo value",
82+
).returns(foo_stub)
6283

63-
component.expects(:render).with(foo_stub)
64-
component.expects(:render).with(bar_stub)
65-
component.expects(:render).with(baz_stub)
84+
ContentBlockManager::ContentBlockEdition::Details::Fields::StringComponent.expects(:new).with(
85+
content_block_edition:,
86+
field: bar_field,
87+
value: "bar value",
88+
).returns(bar_stub)
89+
90+
ContentBlockManager::ContentBlockEdition::Details::Fields::EnumComponent.expects(:new).with(
91+
content_block_edition:,
92+
field: baz_field,
93+
value: "baz value",
94+
enum: %w[some enum],
95+
).returns(baz_stub)
6696

67-
render_inline(component)
97+
assert render_inline(component)
6898
end
6999
end

0 commit comments

Comments
 (0)