Skip to content

Commit

Permalink
Convert attributes test to GreenDots (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper authored Jun 9, 2023
2 parents f46e6be + 6795571 commit 8875d0a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 130 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ AllCops:
TargetRubyVersion: 2.7

Style/ExplicitBlockArgument:
Enabled: false
Enabled: false

Style/MixinUsage:
Enabled: false
79 changes: 79 additions & 0 deletions gd/phlex/sgml/attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

include TestHelper

class ToStrable
def to_str
"foo"
end
end

test "with symbol-keyed hash attributes" do
component = build_component_with_template do
div data: { name: { first_name: "Joel" } }
end

expect(component.new).to_render %(<div data-name-first-name="Joel"></div>)
end

test "with string-keyed hash attributes" do
component = build_component_with_template do
div data: { "name" => { "first_name" => "Joel" } }
end

expect(component.new).to_render %(<div data-name-first_name="Joel"></div>)
end

test "with an array of symbols and strings" do
component = build_component_with_template do
div class: ["bg-red-500", :rounded]
end

expect(component.new).to_render %(<div class="bg-red-500 rounded"></div>)
end

test "with a set of symbols and strings" do
component = build_component_with_template do
div class: Set.new(["bg-red-500", :rounded])
end

expect(component.new).to_render %(<div class="bg-red-500 rounded"></div>)
end

test "with a to_str-able object" do
component = build_component_with_template do
div class: ToStrable.new
end

expect(component.new).to_render %(<div class="foo"></div>)
end

test "with numeric integer/float" do
component = build_component_with_template do
input type: "range", min: 0, max: 10, step: 0.5
end

expect(component.new).to_render %(<input type="range" min="0" max="10" step="0.5">)
end

if RUBY_ENGINE == "ruby"
context "with unique tag attributes" do
let def component
build_component_with_template do
div class: SecureRandom.hex
end
end

let def report
component.call

MemoryProfiler.report do
2.times { component.call }
end
end

test "doesn't leak memory" do
expect(report.total_retained) == 0
end
end
end
20 changes: 20 additions & 0 deletions gd/support/helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# frozen_string_literal: true

require "phlex"
require "bundler"

Bundler.require :test

module TestHelper
def build_component_with_template(&block)
Class.new(Phlex::HTML) { define_method(:template, &block) }
end
end

module ToRender
def to_render(expected_output)
output = subject.call
assert(output == expected_output) { "Expected `#{output.inspect}` to equal `#{expected_output.inspect}`." }
end
end

GreenDots.configure do |config|
config.register_matcher ToRender, Phlex::SGML
end
129 changes: 0 additions & 129 deletions test/phlex/view/attributes.rb

This file was deleted.

0 comments on commit 8875d0a

Please sign in to comment.