-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert attributes test to GreenDots (#574)
- Loading branch information
Showing
4 changed files
with
103 additions
and
130 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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 |
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,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 |
This file was deleted.
Oops, something went wrong.