Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for collection with variable #61

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 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,34 @@ 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 "should be possible to pass variable with CollectionRenderer" 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 racers, partial: "team", 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
Loading