-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9658 from alphagov/content-modelling/659-preview-…
…content Content modelling/ Preview content
- Loading branch information
Showing
12 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...ntent_block_manager/content_block_edition/host_content/preview_details_component.html.erb
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,5 @@ | ||
<ul class="govuk-list"> | ||
<% list_items.each do |item| %> | ||
<li><strong><%= item[:key] %>: </strong><%= item[:value] %></li> | ||
<% end %> | ||
</ul> |
22 changes: 22 additions & 0 deletions
22
...nts/content_block_manager/content_block_edition/host_content/preview_details_component.rb
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,22 @@ | ||
class ContentBlockManager::ContentBlockEdition::HostContent::PreviewDetailsComponent < ViewComponent::Base | ||
def initialize(content_block_edition:, preview_content:) | ||
@content_block_edition = content_block_edition | ||
@preview_content = preview_content | ||
end | ||
|
||
private | ||
|
||
def list_items | ||
[*details_items, instances_item] | ||
end | ||
|
||
def details_items | ||
@content_block_edition.details.map do |key, value| | ||
{ key: key.humanize, value: } | ||
end | ||
end | ||
|
||
def instances_item | ||
{ key: "Instances", value: @preview_content.instances_count } | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
...r/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class ContentBlockManager::ContentBlock::Editions::HostContentController < ContentBlockManager::BaseController | ||
def preview | ||
host_content_id = params[:host_content_id] | ||
content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id]) | ||
@preview_content = ContentBlockManager::GetPreviewContent.for_content_id(content_id: host_content_id, content_block_edition:) | ||
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id]) | ||
@preview_content = ContentBlockManager::GetPreviewContent.for_content_id(content_id: host_content_id, content_block_edition: @content_block_edition) | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
lib/engines/content_block_manager/app/models/content_block_manager/preview_content.rb
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module ContentBlockManager | ||
class PreviewContent < Data.define(:title, :html) | ||
class PreviewContent < Data.define(:title, :html, :instances_count) | ||
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
8 changes: 5 additions & 3 deletions
8
...ager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb
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 |
---|---|---|
|
@@ -508,15 +508,17 @@ def should_show_edit_form_for_email_address_content_block(document_title, email_ | |
Plek.website_root + @current_host_document["base_path"], | ||
).to_return( | ||
status: 200, | ||
body: "<h1>#{@current_host_document['title']}</h1><p>iframe preview</p>", | ||
body: "<body><h1>#{@current_host_document['title']}</h1><p>iframe preview</p>#{@content_block.render}</body>", | ||
) | ||
|
||
click_on @current_host_document["title"] | ||
end | ||
|
||
Then("The preview page opens in a new tab") do | ||
Then("the preview page opens in a new tab") do | ||
page.switch_to_window(page.windows.last) | ||
assert_text "Preview content block" | ||
assert_text "Preview email address" | ||
assert_text "Instances: 1" | ||
assert_text "Email address: [email protected]" | ||
within_frame "preview" do | ||
assert_text @current_host_document["title"] | ||
end | ||
|
21 changes: 21 additions & 0 deletions
21
...ager/test/components/content_block/edition/host_content/preview_details_component_test.rb
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,21 @@ | ||
require "test_helper" | ||
|
||
class ContentBlockManager::ContentBlockEdition::HostContent::PreviewDetailsComponentTest < ViewComponent::TestCase | ||
extend Minitest::Spec::DSL | ||
|
||
let(:content_block_edition) { build(:content_block_edition, :email_address, details: { "email_address": "[email protected]" }) } | ||
let(:preview_content) { build(:preview_content, instances_count: 2) } | ||
|
||
it "returns a list of details for preview content" do | ||
render_inline( | ||
ContentBlockManager::ContentBlockEdition::HostContent::PreviewDetailsComponent.new( | ||
content_block_edition:, | ||
preview_content:, | ||
), | ||
) | ||
|
||
assert_selector "li", count: 2 | ||
assert_selector "li", text: "Email address: [email protected]" | ||
assert_selector "li", text: "Instances: 2" | ||
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase | |
let(:host_base_path) { "/test" } | ||
let(:uri_mock) { mock } | ||
let(:fake_frontend_response) do | ||
"<body><p>test</p><span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">[email protected]</span></body>" | ||
"<body class=\"govuk-body\"><p>test</p><span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">[email protected]</span></body>" | ||
end | ||
let(:block_render) do | ||
"<span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">[email protected]</span>" | ||
|
@@ -19,10 +19,10 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase | |
"<span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\" style=\"background-color: yellow;\">[email protected]</span>" | ||
end | ||
let(:expected_html) do | ||
"<body><p>test</p>#{block_render_with_style}</body>" | ||
"<body class=\"govuk-body draft\"><p>test</p>#{block_render_with_style}</body>" | ||
end | ||
let(:error_html) do | ||
"<html><body><p>Preview not found</p></body></html>" | ||
"<html><body class=\" draft\"><p>Preview not found</p></body></html>" | ||
end | ||
let(:document) do | ||
build(:content_block_document, :email_address, content_id: preview_content_id) | ||
|
@@ -37,12 +37,11 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase | |
block_to_preview.expects(:render).returns(block_render) | ||
end | ||
|
||
it "returns the title and preview HTML for a document" do | ||
it "returns the title of host document" do | ||
Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) | ||
|
||
expected_content = { | ||
title: host_title, | ||
html: Nokogiri::HTML.parse(expected_html), | ||
} | ||
|
||
actual_content = ContentBlockManager::GetPreviewContent.for_content_id( | ||
|
@@ -51,6 +50,35 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase | |
) | ||
|
||
assert_equal expected_content[:title], actual_content.title | ||
end | ||
|
||
it "returns the count of instances" do | ||
Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) | ||
|
||
expected_content = { | ||
instances_count: 1, | ||
} | ||
|
||
actual_content = ContentBlockManager::GetPreviewContent.for_content_id( | ||
content_id: host_content_id, | ||
content_block_edition: block_to_preview, | ||
) | ||
|
||
assert_equal expected_content[:instances_count], actual_content.instances_count | ||
end | ||
|
||
it "returns the preview html" do | ||
Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) | ||
|
||
expected_content = { | ||
html: Nokogiri::HTML.parse(expected_html), | ||
} | ||
|
||
actual_content = ContentBlockManager::GetPreviewContent.for_content_id( | ||
content_id: host_content_id, | ||
content_block_edition: block_to_preview, | ||
) | ||
|
||
assert_equal expected_content[:html].to_s, actual_content.html.to_s | ||
end | ||
|
||
|