require 'spec_helper'
if opal?
module TestMod123
class Bar < React::Component::Base
end
end
describe 'the React DSL' do
context "render macro" do
it "can define the render method with the render macro with a html tag container" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
render(:div, class: :foo) do
"hello"
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('
hello
')
end
it "can define the render method with the render macro without a container" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
render do
"hello"
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('hello')
end
it "can define the render method with the render macro with a application defined container" do
stub_const 'Bar', Class.new(React::Component::Base)
Bar.class_eval do
param :p1
render { "hello #{params.p1}" }
end
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
render Bar, p1: "fred"
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('hello fred')
end
end
it "can use the upcase version of builtin tags" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
DIV { "hello" }
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('hello
')
end
it "will turn the last string in a block into a element" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello" }
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('hello
')
end
it "has a .span short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello".span; "goodby".span }
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('hellogoodby
')
end
it "has a .br short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello".br }
end
end
expect(React.render_to_static_markup(React.create_element(Foo)).gsub("
", "
")).to eq('hello
')
end
it "has a .td short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
table { tr { "hello".td } }
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('')
end
it "has a .para short hand String method" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div { "hello".para }
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('')
end
it 'can do a method call on a class name that is not a direct sibling' do
stub_const 'Mod', Module.new
stub_const 'Mod::NestedMod', Module.new
stub_const 'Mod::Comp', Class.new(React::Component::Base)
Mod::Comp.class_eval do
render { 'Mod::Comp' }
end
stub_const 'Mod::NestedMod::NestedComp', Class.new(React::Component::Base)
Mod::NestedMod::NestedComp.class_eval do
render do
Comp()
end
end
expect(React.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)))
.to eq('Mod::Comp')
end
it 'raises a meaningful error if a Constant Name is not actually a component' do
stub_const 'Mod', Module.new
stub_const 'Mod::NestedMod', Module.new
stub_const 'Mod::Comp', Class.new
stub_const 'Mod::NestedMod::NestedComp', Class.new(React::Component::Base)
Mod::NestedMod::NestedComp.class_eval do
backtrace :none
render do
Comp()
end
end
expect { React.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)) }
.to raise_error('Comp does not appear to be a react component.')
end
it "will treat the component class name as a first class component name" do
stub_const 'Mod::Bar', Class.new
Mod::Bar.class_eval do
include React::Component
def render
"a man walks into a bar"
end
end
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
def render
Mod::Bar()
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('a man walks into a bar')
end
it "can add class names by the haml .class notation" do
# stub_const 'Mod::Barz', Class.new(React::Component::Base)
TestMod123::Bar.class_eval do
collect_other_params_as :attributes
def render
"a man walks into a bar".span(attributes)
end
end
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
def render
TestMod123::Bar().the_class
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('a man walks into a bar')
end
it "can use the 'class' keyword for classes" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
span(class: "the-class") { "hello" }
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('hello')
end
it "can generate a unrendered node using the .as_node method" do # div { "hello" }.as_node
stub_const 'Foo', Class.new #(React::Component::Base)
Foo.class_eval do
include React::Component
def render
span { "hello".span.as_node.class.name }.as_node.render
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('React::Element')
end
it "can use the dangerously_set_inner_HTML param" do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div(dangerously_set_inner_HTML: { __html: "Hello Goodby" })
end
end
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('Hello Goodby
')
end
it 'should convert a hash param to hyphenated html attributes if in React::HASH_ATTRIBUTES' do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div(data: { foo: :bar }, aria: { foo_bar: :foo })
end
end
expect(React.render_to_static_markup(React.create_element(Foo)))
.to eq('')
end
it 'should not convert a hash param to hyphenated html attributes if not in React::HASH_ATTRIBUTES' do
stub_const 'Foo', Class.new
Foo.class_eval do
include React::Component
def render
div(title: { bar: :foo })
end
end
expect(React.render_to_static_markup(React.create_element(Foo)))
.to eq('')
end
it "will remove all elements passed as params from the rendering buffer" do
stub_const 'X2', Class.new
X2.class_eval do
include React::Component
param :ele
def render
div do
ele.render
ele.render
end
end
end
stub_const 'Test', Class.new
Test.class_eval do
include React::Component
def render
X2(ele: b { "hello" })
end
end
expect(React.render_to_static_markup(React.create_element(Test))).to eq('hellohello
')
end
end
end