require 'spec_helper'
require 'reactrb/auto-import'
if opal?
module NativeLibraryTestModule
class Component < React::Component::Base
param :time_stamp
backtrace :none
render { NativeComponent(name: "There - #{params.time_stamp}") }
end
class NestedComponent < React::Component::Base
param :time_stamp
backtrace :none
render { NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{params.time_stamp}") }
end
end
describe "React::NativeLibrary" do
after(:each) do
%x{
delete window.NativeLibrary;
delete window.NativeComponent;
delete window.nativeLibrary;
delete window.nativeComponent;
delete window.NativeObject;
}
Object.send :remove_const, :NativeLibrary
Object.send :remove_const, :NativeComponent
end
describe "functional stateless component (supported in reactjs v14+ only)" do
it "is not detected as native React.js component by `native_react_component?`", v13_only: true do
expect(React::API.native_react_component?(`function C(){ return null }`)).to be_falsy
end
it "is detected as native React.js component by `native_react_component?`", v13_exclude: true do
expect(React::API.native_react_component?(`function C(){ return null }`)).to be_truthy
end
it "imports a React.js functional stateless component", v13_exclude: true do
%x{
window.NativeLibrary = {
FunctionalComponent: function HelloMessage(props){
return React.createElement("div", null, "Hello ", props.name);
}
}
}
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
imports "NativeLibrary.FunctionalComponent"
end
expect(React.render_to_static_markup(
React.create_element(Foo, name: "There"))).to eq('
Hello There.*<\/div>/)
end
it "will automatically import a React.js component when referenced in another component" do
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
render { NativeComponent(name: "There") }
end
%x{
window.NativeComponent = React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
})
}
expect(React.render_to_static_markup(
React.create_element(Foo))).to eq('
Hello There
')
end
it 'will automatically import a React.js component when referenced in another component with the _as_node suffix' do
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
render(:div) do
e = React::Element.new(NativeComponent_as_node(name: 'There'))
2.times { e.render }
end
end
%x{
window.NativeComponent = React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
})
}
expect(React.render_to_static_markup(
React.create_element(Foo))).to eq('
')
end
it "will automatically import a React.js component in a library when referenced in another component with the _as_node suffix" do
stub_const 'Foo', Class.new(React::Component::Base)
Foo.class_eval do
render(:div) do
e = React::Element.new(NativeLibrary::NativeComponent_as_node(name: 'There'))
2.times { e.render }
end
end
%x{
window.NativeLibrary = {
NativeComponent: React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
})
}
}
expect(React.render_to_static_markup(
React.create_element(Foo))).to eq('
')
end
it "will automatically import a React.js component when referenced as a constant" do
%x{
window.NativeComponent = React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
})
}
expect(React.render_to_static_markup(
React.create_element(NativeComponent, name: "There"))).to eq('
Hello There
')
end
it "will automatically import a native library containing a React.js component" do
%x{
window.NativeLibrary = {
NativeNestedLibrary: {
NativeComponent: React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
})
}
}
}
expect(React.render_to_static_markup(
React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))).to match(/
Hello There.*<\/div>/)
end
it "the library and components can begin with lower case letters" do
%x{
window.nativeLibrary = {
nativeComponent: React.createClass({
displayName: "HelloMessage",
render: function render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
})
}
}
expect(React.render_to_static_markup(
React.create_element(NativeLibrary::NativeComponent, name: "There"))).to eq('
Hello There
')
end
it "will produce a sensible error if the component is not in the library" do
%x{
window.NativeLibrary = {
NativeNestedLibrary: {
}
}
}
expect do
React.render_to_static_markup(React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))
end.to raise_error("could not import a react component named: NativeLibrary.NativeNestedLibrary.NativeComponent")
end
end
end
end