Skip to content

Commit

Permalink
Add tests for landing page presenter expanding images
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Nov 6, 2024
1 parent f3f6fe7 commit 46ca634
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/presenters/publishing_api/landing_page_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ def present_image(image, pattern)
end

def present_image_versions(image)
image.image_data.image_kind_config.versions.to_h do |v|
[v.name, image.url(v.name)]
image.image_data.image_kind_config.versions.map do |v|
{
name: v.name,
url: image.url(v.name),
}
end
end
end
Expand Down
101 changes: 101 additions & 0 deletions test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,105 @@ class PublishingApi::LandingPagePresenterTest < ActiveSupport::TestCase

assert_equal expected_details, presented_content[:details].deep_stringify_keys
end

test "it recursively expands images in the body" do
body = <<~YAML
blocks:
- type: hero
image:
sources:
- "[Image: big-cheese.960x640.jpg]"
- "[Image: minister-of-funk.960x640.jpg]"
- type: grid_container
blocks:
- type: hero
image:
sources:
- "[Image: big-cheese.960x640.jpg]"
YAML

landing_page = create(
:landing_page,
document: create(:document, id: 12_346, slug: "/landing-page/with-images"),
body:,
title: "Landing Page title",
summary: "Landing Page summary",
first_published_at: @first_published_at = Time.zone.now,
updated_at: 1.year.ago,
images: [
build(:image, image_data: build(:image_data, file: upload_fixture("big-cheese.960x640.jpg", "image/jpg"))),
build(:image, image_data: build(:image_data, file: upload_fixture("minister-of-funk.960x640.jpg", "image/jpg"))),
],
)

presented_landing_page = PublishingApi::LandingPagePresenter.new(landing_page)
presented_content = I18n.with_locale("en") { presented_landing_page.content }

assert_pattern do
presented_content[:details].deep_symbolize_keys => {
blocks: [
{
type: "hero",
image: {
sources: [
{ id: "big-cheese.960x640.jpg", image_kind: "default", versions: Array },
{ id: "minister-of-funk.960x640.jpg", image_kind: "default", versions: Array },
]
}
},
{
type: "grid_container",
blocks: [{
type: "hero",
image: {
sources: [
{ id: "big-cheese.960x640.jpg", image_kind: "default", versions: Array },
]
}
}],
},
]}
end
end

test "it presents errors if files are not found" do
body = <<~YAML
blocks:
- type: hero
image:
sources:
- "[Image: non-existent-file.jpg]"
YAML

landing_page = create(
:landing_page,
document: create(:document, id: 12_346, slug: "/landing-page/with-images"),
body:,
title: "Landing Page title",
summary: "Landing Page summary",
first_published_at: @first_published_at = Time.zone.now,
updated_at: 1.year.ago,
images: [],
)

presented_landing_page = PublishingApi::LandingPagePresenter.new(landing_page)
presented_content = I18n.with_locale("en") { presented_landing_page.content }
details = presented_content[:details].deep_symbolize_keys

assert_pattern do
details =>
{
blocks: [
{
type: "hero",
image: {
sources: [
{ errors: ["Image not found for pattern [Image: non-existent-file.jpg]"] },
]
}
},
],
}
end
end
end

0 comments on commit 46ca634

Please sign in to comment.