Skip to content

Commit

Permalink
Test for collection with variable
Browse files Browse the repository at this point in the history
  • Loading branch information
skatkov committed Sep 20, 2024
1 parent 41a3a5a commit 7586fec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/pbbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
pb.logo partial: "asset", asset: racer.logo if racer.logo.present?
PBBUILDER

TEAM_PARTIAL = <<-PBBUILDER
pb.extract! racer, :name
pb.team_name team_name
PBBUILDER

ASSET_PARTIAL = <<-PBBUILDER
pb.url asset.url
pb.url_2x asset.url
Expand All @@ -26,6 +31,7 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
"_person.pb.pbbuilder" => PERSON_PARTIAL,
"racers/_racer.pb.pbbuilder" => RACER_PARTIAL,
"_asset.pb.pbbuilder" => ASSET_PARTIAL,
"_team.pb.pbbuilder" => TEAM_PARTIAL,

# Ensure we find only Pbbuilder partials from within Pbbuilder templates.
"_person.html.erb" => "Hello world!"
Expand Down Expand Up @@ -54,6 +60,20 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
assert_equal "https://google.com/test3.svg", result.friends.first.friends.first.friends.first.logo.url
end

test "should be possible to pass variable with collection" do
template = <<-PBBUILDER
racers = [Racer.new(1, "Johnny Test", [], nil, API::Asset.new(url: "https://google.com/test1.svg")), Racer.new(2, "Max Verstappen", [])]
pb.friends partial: "team", collection: racers, as: :racer, locals: { team_name: "Red Bull Racing" }
PBBUILDER

result = render(template)

assert_equal 2, result.friends.count
result.friends.each do |member|
assert_equal "Red Bull Racing", member.team_name
end
end

test "collection partial with fragment caching enabled" do
template = <<-PBBUILDER
racers = [Racer.new(1, "Johnny Test", [], nil, API::Asset.new(url: "https://google.com/test1.svg")), Racer.new(2, "Max Verstappen", [])]
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
optional :last_name, :string, 8
optional :boolean_me, :bool, 9
optional :logo, :message, 10, "pbbuildertest.Asset"
optional :team_name, :string, 11
end

add_message "pbbuildertest.Asset" do
Expand Down

0 comments on commit 7586fec

Please sign in to comment.