spec/react/react_spec.rb in hyper-react-0.10.0 vs spec/react/react_spec.rb in hyper-react-0.11.0

- old
+ new

@@ -4,79 +4,88 @@ RSpec.describe React, type: :component do after(:each) do React::API.clear_component_class_cache end - describe "is_valid_element" do + describe "is_valid_element?" do it "should return true if passed a valid element" do element = React::Element.new(`React.createElement('div')`) - expect(React.is_valid_element(element)).to eq(true) + expect(React.is_valid_element?(element)).to eq(true) end it "should return false is passed a non React element" do element = React::Element.new(`{}`) - expect(React.is_valid_element(element)).to eq(false) + expect(React.is_valid_element?(element)).to eq(false) end end describe "create_element" do it "should create a valid element with only tag" do element = React.create_element('div') - expect(React.is_valid_element(element)).to eq(true) + expect(React.is_valid_element?(element)).to eq(true) end context "with block" do it "should create a valid element with text as only child when block yield String" do element = React.create_element('div') { "lorem ipsum" } - expect(React.is_valid_element(element)).to eq(true) + expect(React.is_valid_element?(element)).to eq(true) expect(element.props.children).to eq("lorem ipsum") end it "should create a valid element with children as array when block yield Array of element" do element = React.create_element('div') do [React.create_element('span'), React.create_element('span'), React.create_element('span')] end - expect(React.is_valid_element(element)).to eq(true) + expect(React.is_valid_element?(element)).to eq(true) expect(element.props.children.length).to eq(3) end it "should render element with children as array when block yield Array of element" do element = React.create_element('div') do [React.create_element('span'), React.create_element('span'), React.create_element('span')] end - instance = renderElementToDocument(element) - expect(Element[instance].children.length).to eq(3) + dom_node = React::Test::Utils.render_into_document(element) + + expect(`#{dom_node}.children.length`).to eq(3) end end describe "custom element" do before do stub_const 'Foo', Class.new Foo.class_eval do + def initialize(native) + @native = native + end + def render React.create_element("div") { "lorem" } end + + def props + Hash.new(`#@native.props`) + end end end it "should render element with only one children correctly" do element = React.create_element(Foo) { React.create_element('span') } - instance = renderElementToDocument(element) - expect(instance.props.children).not_to be_a(Array) - expect(instance.props.children.type).to eq("span") + instance = React::Test::Utils.render_into_document(element) + expect(instance.props[:children]).not_to be_a(Array) + expect(instance.props[:children][:type]).to eq("span") end it "should render element with more than one children correctly" do element = React.create_element(Foo) { [React.create_element('span'), React.create_element('span')] } - instance = renderElementToDocument(element) - expect(instance.props.children).to be_a(Array) - expect(instance.props.children.length).to eq(2) + instance = React::Test::Utils.render_into_document(element) + expect(instance.props[:children]).to be_a(Array) + expect(instance.props[:children].length).to eq(2) end it "should create a valid element provided class defined `render`" do element = React.create_element(Foo) - expect(React.is_valid_element(element)).to eq(true) + expect(React.is_valid_element?(element)).to eq(true) end it "should allow creating with properties" do element = React.create_element(Foo, foo: "bar") expect(element.props.foo).to eq("bar") @@ -100,27 +109,27 @@ def render React.create_element("div") { self.a.to_s } end end - expect(Foo).to render("<div>20</div>") + expect(Foo).to render_static_html("<div>20</div>") end it "should match the instance cycle to ReactComponent life cycle" do `var count = 0;` Foo.class_eval do - def initialize + def initialize(native) `count = count + 1;` end def render React.create_element("div") end end - renderToDocument(Foo) - renderToDocument(Foo) + React::Test::Utils.render_into_document(React.create_element(Foo)) + React::Test::Utils.render_into_document(React.create_element(Foo)) expect(`count`).to eq(2) end end @@ -170,22 +179,26 @@ it "should work without providing a block" do div = `document.createElement("div")` React.render(React.create_element('span') { "lorem" }, div) end - it "should return a React::Component::API compatible object" do - div = `document.createElement("div")` - component = React.render(React.create_element('span') { "lorem" }, div) - React::Component::API.public_instance_methods(true).each do |method_name| - expect(component).to respond_to(method_name) + it "returns the actual ruby instance" do + stub_const 'Foo', Class.new + Foo.class_eval do + def render + React.create_element("div") { "lorem" } + end end + + div = `document.createElement("div")` + instance = React.render(React.create_element(Foo), div) + expect(instance).to be_a(Foo) end - pending "should return nil to prevent abstraction leakage" do + it "returns the actual DOM node" do div = `document.createElement("div")` - expect { - React.render(React.create_element('span') { "lorem" }, div) - }.to be_nil + node = React.render(React.create_element('span') { "lorem" }, div) + expect(`#{node}.nodeType`).to eq(1) end end describe "render_to_string" do it "should render a React.Element to string" do