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

refactor: Reorganize tests #64

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private class DummyPage
private class ExamplePage
include Blueprint::HTML

private def blueprint
Expand All @@ -13,55 +13,55 @@ private class DummyPage
end
end

describe "Blueprint::HTML attributes parser" do
describe "attributes handling" do
it "parses normal attributes" do
page = DummyPage.new
div = <<-HTML.strip
page = ExamplePage.new
div = normalize_html <<-HTML
<div class="hello" id="first">Normal attributes</div>
HTML

page.to_html.should contain(div)
end

it "converts attribute values to string" do
page = DummyPage.new
span = <<-HTML.strip
page = ExamplePage.new
span = normalize_html <<-HTML
<span id="421" float="2.4">Non-string attribute values</span>
HTML

page.to_html.should contain(span)
end

it "replaces `_` by `-` on attribute names" do
page = DummyPage.new
section = <<-HTML.strip
page = ExamplePage.new
section = normalize_html <<-HTML
<section v-model="user.name" @click="doSomething">Transform attribute name</section>
HTML

page.to_html.should contain(section)
end

it "accepts boolean attributes" do
page = DummyPage.new
input = <<-HTML.strip
page = ExamplePage.new
input = normalize_html <<-HTML
<input disabled outline="true" border="false">
HTML

page.to_html.should contain(input)
end

it "expands nested attributes" do
page = DummyPage.new
nav = <<-HTML.strip
page = ExamplePage.new
nav = normalize_html <<-HTML
<nav aria-target="#home" aria-selected="false" aria-enabled>Nested attributes</nav>
HTML

page.to_html.should contain(nav)
end

it "flattens, compacts and joins array attributes" do
page = DummyPage.new
nav = <<-HTML.strip
page = ExamplePage.new
nav = normalize_html <<-HTML
<div class="a b c d">Array attributes</div>
HTML

Expand Down
17 changes: 0 additions & 17 deletions spec/blueprint/html/builder_spec.cr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private class DummyPage
private class ExamplePage
include Blueprint::HTML

private def blueprint
Expand Down Expand Up @@ -66,37 +66,39 @@ private class ComplexComponent
end
end

describe "Blueprint::HTML renderer" do
describe "#render" do
it "can render another blueprints" do
page = DummyPage.new
basic_component = <<-HTML.strip
<header>Basic component</header>
HTML
describe "component rendering" do
it "can render another blueprints" do
page = ExamplePage.new
basic_component = normalize_html <<-HTML
<header>Basic component</header>
HTML

page.to_html.should contain(basic_component)
end

it "can provide content to another blueprints" do
page = DummyPage.new
content_component = <<-HTML.strip
<div><p><span>Passing content to component</span></p></div>
HTML
page.to_html.should contain(basic_component)
end

page.to_html.should contain(content_component)
end
it "can provide content to another blueprints" do
page = ExamplePage.new
content_component = normalize_html <<-HTML
<div>
<p>
<span>Passing content to component</span>
</p>
</div>
HTML

page.to_html.should contain(content_component)
end

it "can use another blueprint methods" do
page = DummyPage.new
complex_component = <<-HTML.strip.gsub(/\R\s+/, "")
<div class="bg-white border shadow">
<div class="p-4 font-bold text-lg">My card</div>
<div class="px-4 py-2">Card content</div>
<footer>Footer tag</footer>
</div>
HTML

page.to_html.should contain(complex_component)
end
it "can use another blueprint methods" do
page = ExamplePage.new
complex_component = normalize_html <<-HTML
<div class="bg-white border shadow">
<div class="p-4 font-bold text-lg">My card</div>
<div class="px-4 py-2">Card content</div>
<footer>Footer tag</footer>
</div>
HTML

page.to_html.should contain(complex_component)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ require "../../spec_helper"

require "../../../src/blueprint/html/component_registrar"

private class DummyPage
private class ExamplePage
include Blueprint::HTML
include Blueprint::HTML::ComponentRegistrar

register_component :required_block_component, RequiredBlockComponent
register_component :no_block_component, NoBlockComponent, block: false
register_component :optional_block_component, OptionalBlockComponent, block: :optional
register_component :component_with_block, ComponentWithBlock
register_component :component_without_block, ComponentWithoutBlock, block: false
register_component :component_with_optional_block, ComponentWithOptionalBlock, block: :optional

private def blueprint
required_block_component do
component_with_block do
"Component with required block"
end

no_block_component
component_without_block

optional_block_component do
component_with_optional_block do
"Component with optional block"
end

optional_block_component
component_with_optional_block
end
end

private class RequiredBlockComponent
private class ComponentWithBlock
include Blueprint::HTML

private def blueprint(&)
Expand All @@ -35,15 +35,15 @@ private class RequiredBlockComponent
end
end

private class NoBlockComponent
private class ComponentWithoutBlock
include Blueprint::HTML

private def blueprint
h1 "Component without block"
end
end

private class OptionalBlockComponent
private class ComponentWithOptionalBlock
include Blueprint::HTML

private def blueprint(&)
Expand All @@ -53,28 +53,28 @@ private class OptionalBlockComponent
end
end

describe "Blueprint::HTML components registration" do
describe "components registration" do
it "allows component helper definition" do
page = DummyPage.new
expected_html = <<-HTML.strip
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<div id="required-block">Component with required block</div>
HTML

page.to_html.should contain expected_html
end

it "allows component helper definition without required block" do
page = DummyPage.new
expected_html = <<-HTML.strip
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<h1>Component without block</h1>
HTML

page.to_html.should contain expected_html
end

it "allows component helper definition with optional block" do
page = DummyPage.new
expected_html = <<-HTML.strip.gsub(/\R\s+/, "")
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<div id="optional-block">Component with optional block</div>
<div id="optional-block"></div>
HTML
Expand Down
8 changes: 4 additions & 4 deletions spec/blueprint/html/conditional_rendering_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private class DummyPage
private class ExamplePage
include Blueprint::HTML

private def blueprint
Expand Down Expand Up @@ -43,11 +43,11 @@ private class NoRenderComponent
end
end

describe "Blueprint::HTML conditional rendering" do
describe "conditional rendering" do
context "when component `#render?` returns false" do
it "doesn't render the component" do
page = DummyPage.new
expected_html = <<-HTML.strip
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<div></div>
HTML

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

private class DummyPage
private class ExamplePage
include Blueprint::HTML

register_element :v_btn
Expand All @@ -10,33 +10,43 @@ private class DummyPage
div do
v_btn(href: "#home", data: {id: 12, visible: true, disabled: false}) { "Home" }
v_btn("Contact", href: "#contact")
v_btn
card
end
end
end

describe "Blueprint::HTML custom elements registration" do
describe "custom elements registration" do
it "allows custom elements definition" do
page = DummyPage.new
expected_html = <<-HTML.strip
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<v-btn href="#home" data-id="12" data-visible>Home</v-btn>
HTML

page.to_html.should contain expected_html
end

it "allows passing content as first argument" do
page = DummyPage.new
expected_html = <<-HTML.strip
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<v-btn href="#contact">Contact</v-btn>
HTML

page.to_html.should contain expected_html
end

it "allows empty custom elements" do
page = DummyPage.new
expected_html = <<-HTML.strip
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<v-btn></v-btn>
HTML

page.to_html.should contain expected_html
end

it "allows defining custom tags" do
page = ExamplePage.new
expected_html = normalize_html <<-HTML
<MyCard></MyCard>
HTML

Expand Down
30 changes: 28 additions & 2 deletions spec/blueprint/html/enveloping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,42 @@ private class BasePage
end
end

private class Component
include Blueprint::HTML

def blueprint
h1 "Hello World!"
end

def envelope(&)
div class: "title" do
yield
end
end
end

private class IndexPage < BasePage
private def blueprint
h1 "Home"
render Component.new
end
end

describe "Blueprint::HTML enveloping" do
describe "enveloping" do
it "allows defining a blueprint wrapper" do
page = IndexPage.new
expected_html = normalize_html <<-HTML
<html>
<body>
<h1>Home</h1>

<div class="title">
<h1>Hello World!</h1>
</div>
</body>
</html>
HTML

page.to_html.should eq "<html><body><h1>Home</h1></body></html>"
page.to_html.should eq expected_html
end
end
Loading
Loading