|
1 |
| -require 'spec_helper' |
| 1 | +if false |
| 2 | + #require 'spec_helper' |
| 3 | +#require 'reactrb/auto-import' |
2 | 4 |
|
3 | 5 | if opal?
|
4 |
| -describe React::NativeLibrary do |
5 |
| - it "will import a React.js library into the Ruby name space" # class BS < NativeLibrary; imports 'ReactBootstrap'; end |
6 |
| - it "exclude specific components from a library" # exclude "Modal" # can't access BS.Modal |
7 |
| - it "rename specific components from a library" # rename "Modal" => "FooBar" # BS.FooBar connects to ReactBootstrap.Modal |
8 |
| - it "can importan multiple libraries into one class" |
| 6 | + |
| 7 | +module NativeLibraryTestModule |
| 8 | + class Component < React::Component::Base |
| 9 | + param :time_stamp |
| 10 | + render { puts "about to render Component"; NativeComponent(name: "There - #{params.time_stamp}") } |
| 11 | + end |
| 12 | + |
| 13 | + class NestedComponent < React::Component::Base |
| 14 | + param :time_stamp |
| 15 | + render { puts "about to render NestedComponent"; NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{params.time_stamp}") } |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +describe "React::NativeLibrary", only: true do |
| 20 | + |
| 21 | + after(:each) do |
| 22 | + %x{ |
| 23 | + delete window.NativeLibrary; |
| 24 | + delete window.NativeComponent; |
| 25 | + delete window.nativeLibrary; |
| 26 | + delete window.nativeComponent; |
| 27 | + delete window.NativeObject; |
| 28 | + } |
| 29 | + Object.send :remove_const, :NativeLibrary |
| 30 | + Object.send :remove_const, :NativeComponent |
| 31 | + end |
| 32 | + |
| 33 | + # it "will import a React.js library into the Ruby name space" do |
| 34 | + # %x{ |
| 35 | + # window.NativeLibrary = { |
| 36 | + # NativeComponent: React.createClass({ |
| 37 | + # displayName: "HelloMessage", |
| 38 | + # render: function render() { |
| 39 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 40 | + # } |
| 41 | + # }) |
| 42 | + # } |
| 43 | + # } |
| 44 | + # stub_const 'Foo', Class.new(React::NativeLibrary) |
| 45 | + # Foo.class_eval do |
| 46 | + # imports "NativeLibrary" |
| 47 | + # end |
| 48 | + # expect(React.render_to_static_markup( |
| 49 | + # React.create_element(Foo::NativeComponent, name: "There"))).to eq('<div>Hello There</div>') |
| 50 | + # end |
| 51 | + # |
| 52 | + # it "will rename an imported a React.js component" do |
| 53 | + # %x{ |
| 54 | + # window.NativeLibrary = { |
| 55 | + # NativeComponent: React.createClass({ |
| 56 | + # displayName: "HelloMessage", |
| 57 | + # render: function render() { |
| 58 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 59 | + # } |
| 60 | + # }) |
| 61 | + # } |
| 62 | + # } |
| 63 | + # stub_const 'Foo', Class.new(React::NativeLibrary) |
| 64 | + # Foo.class_eval do |
| 65 | + # imports "NativeLibrary" |
| 66 | + # rename "NativeComponent" => "Bar" |
| 67 | + # end |
| 68 | + # expect(React.render_to_static_markup( |
| 69 | + # React.create_element(Foo::Bar, name: "There"))).to eq('<div>Hello There</div>') |
| 70 | + # end |
| 71 | + # |
| 72 | + # it "will give a reasonable error when failing to import a renamed component" do |
| 73 | + # %x{ |
| 74 | + # window.NativeLibrary = { |
| 75 | + # NativeComponent: React.createClass({ |
| 76 | + # displayName: "HelloMessage", |
| 77 | + # render: function render() { |
| 78 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 79 | + # } |
| 80 | + # }) |
| 81 | + # } |
| 82 | + # } |
| 83 | + # stub_const 'Foo', Class.new(React::NativeLibrary) |
| 84 | + # expect do |
| 85 | + # Foo.class_eval do |
| 86 | + # imports "NativeLibrary" |
| 87 | + # rename "MispelledComponent" => "Bar" |
| 88 | + # end |
| 89 | + # end.to raise_error(/could not import MispelledComponent/) |
| 90 | + # end |
| 91 | + # |
| 92 | + # it "will import a single React.js component into the ruby name space" do |
| 93 | + # %x{ |
| 94 | + # window.NativeComponent = React.createClass({ |
| 95 | + # displayName: "HelloMessage", |
| 96 | + # render: function render() { |
| 97 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 98 | + # } |
| 99 | + # }) |
| 100 | + # } |
| 101 | + # stub_const 'Foo', Class.new(React::Component::Base) |
| 102 | + # Foo.class_eval do |
| 103 | + # imports "NativeComponent" |
| 104 | + # end |
| 105 | + # expect(React.render_to_static_markup( |
| 106 | + # React.create_element(Foo, name: "There"))).to eq('<div>Hello There</div>') |
| 107 | + # |
| 108 | + # end |
| 109 | + # |
| 110 | + # it "will import a name scoped React.js component into the ruby name space" do |
| 111 | + # %x{ |
| 112 | + # window.NativeLibrary = { |
| 113 | + # NativeComponent: React.createClass({ |
| 114 | + # displayName: "HelloMessage", |
| 115 | + # render: function render() { |
| 116 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 117 | + # } |
| 118 | + # }) |
| 119 | + # } |
| 120 | + # } |
| 121 | + # stub_const 'Foo', Class.new(React::Component::Base) |
| 122 | + # Foo.class_eval do |
| 123 | + # imports "NativeLibrary.NativeComponent" |
| 124 | + # end |
| 125 | + # expect(React.render_to_static_markup( |
| 126 | + # React.create_element(Foo, name: "There"))).to eq('<div>Hello There</div>') |
| 127 | + # |
| 128 | + # end |
| 129 | + # |
| 130 | + # it "will give a meaningful error if the React.js component is invalid" do |
| 131 | + # %x{ |
| 132 | + # window.NativeObject = {} |
| 133 | + # } |
| 134 | + # stub_const 'Foo', Class.new(React::Component::Base) |
| 135 | + # expect do |
| 136 | + # Foo.class_eval do |
| 137 | + # imports "NativeComponent" |
| 138 | + # end |
| 139 | + # end.to raise_error(/Cannot import NativeComponent: /) |
| 140 | + # expect do |
| 141 | + # Foo.class_eval do |
| 142 | + # imports "NativeObject" |
| 143 | + # end |
| 144 | + # end.to raise_error("Cannot import NativeObject: not a react native component") |
| 145 | + # end |
| 146 | + |
| 147 | + context "automatic importing" do |
| 148 | + |
| 149 | + it "will automatically import a React.js component when referenced in another component" do |
| 150 | + %x{ |
| 151 | + window.NativeComponent = React.createClass({ |
| 152 | + displayName: "HelloMessage", |
| 153 | + render: function render() { |
| 154 | + return React.createElement("div", null, "Hello ", this.props.name); |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | + expect(React.render_to_static_markup( |
| 159 | + React.create_element(NativeLibraryTestModule::Component, time_stamp: Time.now))).to eq('<div>Hello There</div>') |
| 160 | + end |
| 161 | + |
| 162 | + # it "will automatically import a React.js component when referenced in another component" do |
| 163 | + # stub_const 'Foo', Class.new(React::Component::Base) |
| 164 | + # Foo.class_eval do |
| 165 | + # render { NativeComponent(name: "There") } |
| 166 | + # end |
| 167 | + # %x{ |
| 168 | + # window.NativeComponent = React.createClass({ |
| 169 | + # displayName: "HelloMessage", |
| 170 | + # render: function render() { |
| 171 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 172 | + # } |
| 173 | + # }) |
| 174 | + # } |
| 175 | + # expect(React.render_to_static_markup( |
| 176 | + # React.create_element(Foo))).to eq('<div>Hello There</div>') |
| 177 | + # end |
| 178 | + |
| 179 | + # it "will automatically import a React.js component when referenced as a constant" do |
| 180 | + # %x{ |
| 181 | + # window.NativeComponent = React.createClass({ |
| 182 | + # displayName: "HelloMessage", |
| 183 | + # render: function render() { |
| 184 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 185 | + # } |
| 186 | + # }) |
| 187 | + # } |
| 188 | + # expect(React.render_to_static_markup( |
| 189 | + # React.create_element(NativeComponent, name: "There"))).to eq('<div>Hello There</div>') |
| 190 | + # end |
| 191 | + |
| 192 | + it "will automatically import a native library containing a React.js component" do |
| 193 | + %x{ |
| 194 | + window.NativeLibrary = { |
| 195 | + NativeNestedLibrary: { |
| 196 | + NativeComponent: React.createClass({ |
| 197 | + displayName: "HelloMessage", |
| 198 | + render: function render() { |
| 199 | + return React.createElement("div", null, "Hello ", this.props.name); |
| 200 | + } |
| 201 | + }) |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + expect(React.render_to_static_markup( |
| 207 | + React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))).to eq('<div>Hello There</div>') |
| 208 | + end |
| 209 | + |
| 210 | + # it "the library and components can begin with lower case letters" do |
| 211 | + # %x{ |
| 212 | + # window.nativeLibrary = { |
| 213 | + # nativeComponent: React.createClass({ |
| 214 | + # displayName: "HelloMessage", |
| 215 | + # render: function render() { |
| 216 | + # return React.createElement("div", null, "Hello ", this.props.name); |
| 217 | + # } |
| 218 | + # }) |
| 219 | + # } |
| 220 | + # } |
| 221 | + # expect(React.render_to_static_markup( |
| 222 | + # React.create_element(NativeLibrary::NativeComponent, name: "There"))).to eq('<div>Hello There</div>') |
| 223 | + # end |
| 224 | + |
| 225 | + it "will produce a sensible error if the component is not in the library" do |
| 226 | + %x{ |
| 227 | + window.NativeLibrary = { |
| 228 | + NativeNestedLibrary: { |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + #expect do |
| 233 | + begin |
| 234 | + `debugger` |
| 235 | + x = React.render_to_static_markup(React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now)) |
| 236 | + puts x |
| 237 | + rescue Exception => e |
| 238 | + `debugger` |
| 239 | + nil |
| 240 | + end |
| 241 | + #end.to raise_error("could not import a react component named: #{NativeLibrary.NativeNestedLibrary.NestedComponent}") |
| 242 | + |
| 243 | + end |
| 244 | + |
| 245 | + end |
| 246 | +end |
9 | 247 | end
|
10 | 248 | end
|
0 commit comments