require "spec_helper" describe React do after(:each) do React::API.clear_component_class_cache end 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) 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) 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) 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(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(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(instance.getDOMNode.childNodes.length).to eq(3) end end describe "custom element" do before do stub_const 'Foo', Class.new Foo.class_eval do def render React.create_element("div") { "lorem" } 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") 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) 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) end it "should allow creating with properties" do element = React.create_element(Foo, foo: "bar") expect(element.props.foo).to eq("bar") end it "should raise error if provided class doesn't defined `render`" do expect { React.create_element(Array) }.to raise_error end it "should use the same instance for the same ReactComponent" do Foo.class_eval do attr_accessor :a def initialize(n) self.a = 10 end def component_will_mount self.a = 20 end def render React.create_element("div") { self.a.to_s } end end expect(React.render_to_static_markup(React.create_element(Foo))).to eq("