-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ContentBlockTools when rendering
This ensures there is one source of truth for how content blocks are rendered, and it can be used in other applications easily.
- Loading branch information
Showing
6 changed files
with
71 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
it "should send transformed content to the content store" do | ||
put "/v2/content/#{content_id}", params: payload.to_json | ||
|
||
expect_content_store_to_have_received_details_including({ "body" => "#{first_contact.title} #{second_contact.title}" }) | ||
expect_content_store_to_have_received_details_including({ "body" => "#{presented_details_for(first_contact)} #{presented_details_for(second_contact)}" }) | ||
end | ||
end | ||
|
||
|
@@ -52,7 +52,7 @@ | |
it "should send transformed content to the content store" do | ||
put "/v2/content/#{content_id}", params: payload_for_multiple_field_embeds.to_json | ||
|
||
expect_content_store_to_have_received_details_including({ "downtime_message" => first_contact.title.to_s }) | ||
expect_content_store_to_have_received_details_including({ "downtime_message" => presented_details_for(first_contact) }) | ||
end | ||
end | ||
|
||
|
@@ -128,11 +128,11 @@ | |
"body" => [ | ||
{ | ||
"content_type" => "text/govspeak", | ||
"content" => first_contact.title, | ||
"content" => presented_details_for(first_contact), | ||
}, | ||
{ | ||
"content_type" => "text/html", | ||
"content" => "<p>#{first_contact.title}</p>", | ||
"content" => "<p>#{presented_details_for(first_contact)}</p>", | ||
}, | ||
], | ||
}, | ||
|
@@ -142,11 +142,11 @@ | |
"body" => [ | ||
{ | ||
"content_type" => "text/govspeak", | ||
"content" => second_contact.title, | ||
"content" => presented_details_for(second_contact), | ||
}, | ||
{ | ||
"content_type" => "text/html", | ||
"content" => "<p>#{second_contact.title}</p>", | ||
"content" => "<p>#{presented_details_for(second_contact)}</p>", | ||
}, | ||
], | ||
}, | ||
|
@@ -176,32 +176,7 @@ | |
it "should send transformed content to the content store" do | ||
put "/v2/content/#{content_id}", params: payload.to_json | ||
|
||
expect_content_store_to_have_received_details_including({ "body" => array_including({ "content_type" => "text/govspeak", "content" => "#{first_contact.title} #{second_contact.title}" }) }) | ||
end | ||
end | ||
|
||
context "with an embedded email address" do | ||
let(:first_email_address) { create(:edition, state: "published", content_store: "live", document_type: "content_block_email_address", details: { email_address: "[email protected]" }) } | ||
let(:second_email_address) { create(:edition, state: "published", content_store: "live", document_type: "content_block_email_address", details: { email_address: "[email protected]" }) } | ||
let(:document) { create(:document, content_id:) } | ||
|
||
before do | ||
payload.merge!(document_type: "press_release", schema_name: "news_article", details: { body: "{{embed:content_block_email_address:#{first_email_address.document.content_id}}} {{embed:content_block_email_address:#{second_email_address.document.content_id}}}" }) | ||
end | ||
|
||
it "should create links" do | ||
expect { | ||
put "/v2/content/#{content_id}", params: payload.to_json | ||
}.to change(Link, :count).by(2) | ||
|
||
expect(Link.find_by(target_content_id: first_email_address.content_id)).not_to be_nil | ||
expect(Link.find_by(target_content_id: second_email_address.content_id)).not_to be_nil | ||
end | ||
|
||
it "should send transformed content to the content store" do | ||
put "/v2/content/#{content_id}", params: payload.to_json | ||
|
||
expect_content_store_to_have_received_details_including({ "body" => "#{first_email_address.details[:email_address]} #{second_email_address.details[:email_address]}" }) | ||
expect_content_store_to_have_received_details_including({ "body" => array_including({ "content_type" => "text/govspeak", "content" => "#{presented_details_for(first_contact)} #{presented_details_for(second_contact)}" }) }) | ||
end | ||
end | ||
|
||
|
@@ -226,7 +201,7 @@ | |
it "should send transformed content to the content store" do | ||
put "/v2/content/#{content_id}", params: payload.to_json | ||
|
||
expect_content_store_to_have_received_details_including({ "body" => "#{email_address.details[:email_address]} #{contact.title}" }) | ||
expect_content_store_to_have_received_details_including({ "body" => "#{presented_details_for(email_address)} #{presented_details_for(contact)}" }) | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
end | ||
let(:details) { {} } | ||
|
||
before do | ||
let!(:embedded_edition) do | ||
embedded_document = create(:document, content_id: embedded_content_id) | ||
create( | ||
:edition, | ||
|
@@ -29,12 +29,24 @@ | |
end | ||
|
||
describe "#render_embedded_content" do | ||
let(:expected_value) { "VALUE" } | ||
let(:stub_block) { double(ContentBlockTools::ContentBlock, render: expected_value) } | ||
|
||
before do | ||
expect(ContentBlockTools::ContentBlock).to receive(:new).with( | ||
document_type: embedded_edition.document_type, | ||
content_id: embedded_edition.document.content_id, | ||
title: embedded_edition.title, | ||
details: embedded_edition.details, | ||
).at_least(:once).and_return(stub_block) | ||
end | ||
|
||
context "when body is a string" do | ||
let(:details) { { body: "some string with a reference: {{embed:contact:#{embedded_content_id}}}" } } | ||
|
||
it "returns embedded content references with values from their editions" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: "some string with a reference: VALUE", | ||
body: "some string with a reference: #{expected_value}", | ||
}) | ||
end | ||
end | ||
|
@@ -50,8 +62,8 @@ | |
it "returns embedded content references with values from their editions" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: [ | ||
{ content_type: "text/govspeak", content: "some string with a reference: VALUE" }, | ||
{ content_type: "text/html", content: "some string with a reference: VALUE" }, | ||
{ content_type: "text/govspeak", content: "some string with a reference: #{expected_value}" }, | ||
{ content_type: "text/html", content: "some string with a reference: #{expected_value}" }, | ||
], | ||
}) | ||
end | ||
|
@@ -64,7 +76,7 @@ | |
|
||
it "returns embedded content references with values from their editions" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: { title: "some string with a reference: VALUE" }, | ||
body: { title: "some string with a reference: #{expected_value}" }, | ||
}) | ||
end | ||
end | ||
|
@@ -91,7 +103,7 @@ | |
parts: [ | ||
body: [ | ||
{ | ||
content: "some string with a reference: VALUE", | ||
content: "some string with a reference: #{expected_value}", | ||
content_type: "text/govspeak", | ||
}, | ||
], | ||
|
@@ -106,7 +118,7 @@ | |
context "when the embedded content is available in multiple locales" do | ||
let(:details) { { body: "some string with a reference: {{embed:contact:#{embedded_content_id}}}" } } | ||
|
||
before do | ||
let!(:welsh_edition) do | ||
embedded_document = create(:document, content_id: embedded_content_id, locale: "cy") | ||
create( | ||
:edition, | ||
|
@@ -121,17 +133,18 @@ | |
context "when the document is in the default language" do | ||
it "returns embedded content references with values from the same language" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: "some string with a reference: VALUE", | ||
body: "some string with a reference: #{expected_value}", | ||
}) | ||
end | ||
end | ||
|
||
context "when the document is in an available locale" do | ||
let(:document) { create(:document, locale: "cy") } | ||
let(:embedded_edition) { welsh_edition } | ||
|
||
it "returns embedded content references with values from the same language" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: "some string with a reference: WELSH", | ||
body: "some string with a reference: #{expected_value}", | ||
}) | ||
end | ||
end | ||
|
@@ -141,46 +154,16 @@ | |
|
||
it "returns embedded content references with values from the default language" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: "some string with a reference: VALUE", | ||
body: "some string with a reference: #{expected_value}", | ||
}) | ||
end | ||
end | ||
end | ||
|
||
context "when the document is an email address" do | ||
let(:embedded_document) { create(:document) } | ||
let(:links_hash) do | ||
{ | ||
embed: [embedded_document.content_id], | ||
} | ||
end | ||
|
||
before do | ||
create( | ||
:edition, | ||
document: embedded_document, | ||
state: "published", | ||
content_store: "live", | ||
document_type: "content_block_email_address", | ||
details: { | ||
email_address: "[email protected]", | ||
}, | ||
) | ||
end | ||
|
||
let(:details) { { body: "some string with a reference: {{embed:content_block_email_address:#{embedded_document.content_id}}}" } } | ||
|
||
it "returns an email address" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: "some string with a reference: [email protected]", | ||
}) | ||
end | ||
end | ||
|
||
context "when multiple documents are embedded in different parts of the document" do | ||
let(:other_embedded_content_id) { SecureRandom.uuid } | ||
|
||
before do | ||
let!(:other_embedded_edition) do | ||
embedded_document = create(:document, content_id: other_embedded_content_id) | ||
create( | ||
:edition, | ||
|
@@ -192,6 +175,18 @@ | |
) | ||
end | ||
|
||
let(:other_expected_value) { "VALUE2" } | ||
let(:other_stub_block) { double(ContentBlockTools::ContentBlock, render: other_expected_value) } | ||
|
||
before do | ||
expect(ContentBlockTools::ContentBlock).to receive(:new).with( | ||
document_type: other_embedded_edition.document_type, | ||
content_id: other_embedded_edition.document.content_id, | ||
title: other_embedded_edition.title, | ||
details: other_embedded_edition.details, | ||
).at_least(:once).and_return(other_stub_block) | ||
end | ||
|
||
let(:links_hash) do | ||
{ | ||
embed: [embedded_content_id, other_embedded_content_id], | ||
|
@@ -207,8 +202,8 @@ | |
|
||
it "returns embedded content references with values from their editions" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
title: "title string with reference: VALUE2", | ||
body: "some string with a reference: VALUE", | ||
title: "title string with reference: #{other_expected_value}", | ||
body: "some string with a reference: #{expected_value}", | ||
}) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module ContentBlockHelpers | ||
def presented_details_for(edition) | ||
ContentBlockTools::ContentBlock.new( | ||
document_type: edition.document_type, | ||
content_id: edition.document.content_id, | ||
title: edition.title, | ||
details: edition.details, | ||
).render | ||
end | ||
end | ||
|
||
RSpec.configure do |c| | ||
c.include ContentBlockHelpers | ||
end |