Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 0793ce1

Browse files
committed
Add React::Server
1 parent 5cf0431 commit 0793ce1

11 files changed

+66
-22
lines changed

lib/react/server.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module React
2+
module Server
3+
def self.render_to_string(element)
4+
if !(`typeof ReactDOMServer === 'undefined'`)
5+
React::RenderingContext.build { `ReactDOMServer.renderToString(#{element.to_n})` } # v0.15+
6+
elsif !(`typeof React.renderToString === 'undefined'`)
7+
React::RenderingContext.build { `React.renderToString(#{element.to_n})` }
8+
else
9+
raise "renderToString is not defined. In React >= v15 you must import it with ReactDOMServer"
10+
end
11+
end
12+
13+
def self.render_to_static_markup(element)
14+
if !(`typeof ReactDOMServer === 'undefined'`)
15+
React::RenderingContext.build { `ReactDOMServer.renderToStaticMarkup(#{element.to_n})` } # v0.15+
16+
elsif !(`typeof React.renderToString === 'undefined'`)
17+
React::RenderingContext.build { `React.renderToStaticMarkup(#{element.to_n})` }
18+
else
19+
raise "renderToStaticMarkup is not defined. In React >= v15 you must import it with ReactDOMServer"
20+
end
21+
end
22+
end
23+
end

lib/react/test/matchers/render_html_matcher.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def negative_failure_message
3030

3131
def render_to_html
3232
element = React.create_element(@component, @params)
33-
React.render_to_static_markup(element)
33+
React::Server.render_to_static_markup(element)
3434
end
3535

3636
def failure_string(negative = false)

lib/react/top_level.rb

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def self.is_valid_element?(element)
8484
end
8585

8686
def self.render_to_string(element)
87+
%x{ console.error("Warning: `React.render_to_string` is deprecated in favor of `React::Server.render_to_string`."); }
8788
if !(`typeof ReactDOMServer === 'undefined'`)
8889
React::RenderingContext.build { `ReactDOMServer.renderToString(#{element.to_n})` } # v0.15+
8990
elsif !(`typeof React.renderToString === 'undefined'`)
@@ -94,6 +95,7 @@ def self.render_to_string(element)
9495
end
9596

9697
def self.render_to_static_markup(element)
98+
%x{ console.error("Warning: `React.render_to_static_markup` is deprecated in favor of `React::Server.render_to_static_markup`."); }
9799
if !(`typeof ReactDOMServer === 'undefined'`)
98100
React::RenderingContext.build { `ReactDOMServer.renderToStaticMarkup(#{element.to_n})` } # v0.15+
99101
elsif !(`typeof React.renderToString === 'undefined'`)

spec/react/dsl_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def render
6565
end
6666
end
6767

68-
expect(React.render_to_static_markup(React.create_element(Foo))).to match(/<input data-foo="12"(\/)?>/)
68+
expect(React::Server.render_to_static_markup(React.create_element(Foo))).to match(/<input data-foo="12"(\/)?>/)
6969
end
7070

7171
it "will turn the last string in a block into a element" do
@@ -101,7 +101,7 @@ def render
101101
end
102102
end
103103

104-
expect(React.render_to_static_markup(React.create_element(Foo)).gsub("<br/>", "<br>")).to eq('<div><span>hello<br></span></div>')
104+
expect(React::Server.render_to_static_markup(React.create_element(Foo)).gsub("<br/>", "<br>")).to eq('<div><span>hello<br></span></div>')
105105
end
106106

107107
it "has a .td short hand String method" do
@@ -156,7 +156,7 @@ def render
156156
Comp()
157157
end
158158
end
159-
expect { React.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)) }
159+
expect { React::Server.render_to_static_markup(React.create_element(Mod::NestedMod::NestedComp)) }
160160
.to raise_error('Comp does not appear to be a react component.')
161161
end
162162

spec/react/element_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def render
2929
end
3030
end
3131

32-
expect(React.render_to_static_markup(React.create_element(Foo))).to match(/<input (type="text" value=""|value="" type="text")(\/)?>/)
32+
expect(React::Server.render_to_static_markup(React.create_element(Foo))).to match(/<input (type="text" value=""|value="" type="text")(\/)?>/)
3333
end
3434
end
3535

@@ -43,7 +43,7 @@ def render
4343
params.on_event
4444
end
4545
end
46-
expect(React.render_to_static_markup(React.create_element(Foo).on(:event) {'works!'})).to eq('<span>works!</span>')
46+
expect(React::Server.render_to_static_markup(React.create_element(Foo).on(:event) {'works!'})).to eq('<span>works!</span>')
4747
end
4848

4949
it 'will subscribe to multiple component event params' do
@@ -55,7 +55,7 @@ def render
5555
params.on_event1+params.on_event2
5656
end
5757
end
58-
expect(React.render_to_static_markup(React.create_element(Foo).on(:event1, :event2) {'works!'})).to eq('<span>works!works!</span>')
58+
expect(React::Server.render_to_static_markup(React.create_element(Foo).on(:event1, :event2) {'works!'})).to eq('<span>works!works!</span>')
5959
end
6060

6161
it 'will subscribe to a native components event param' do
@@ -71,7 +71,7 @@ def render
7171
Foo.class_eval do
7272
imports "NativeComponent"
7373
end
74-
expect(React.render_to_static_markup(React.create_element(Foo).on(:event) {'works!'})).to eq('<span>works!</span>')
74+
expect(React::Server.render_to_static_markup(React.create_element(Foo).on(:event) {'works!'})).to eq('<span>works!</span>')
7575
end
7676

7777
it 'will subscribe to a component event param with a non-default name' do
@@ -82,7 +82,7 @@ def render
8282
params.my_event
8383
end
8484
end
85-
expect(React.render_to_static_markup(React.create_element(Foo).on("<my_event>") {'works!'})).to eq('<span>works!</span>')
85+
expect(React::Server.render_to_static_markup(React.create_element(Foo).on("<my_event>") {'works!'})).to eq('<span>works!</span>')
8686
end
8787

8888
it 'will subscribe to a component event param using the deprecated naming convention and generate a message' do
@@ -99,7 +99,7 @@ def render
9999
var org_error_console = window.console.error;
100100
window.console.warn = window.console.error = function(str){log.push(str)}
101101
}
102-
expect(React.render_to_static_markup(React.create_element(Foo).on(:event) {'works!'})).to eq('<span>works!</span>')
102+
expect(React::Server.render_to_static_markup(React.create_element(Foo).on(:event) {'works!'})).to eq('<span>works!</span>')
103103
`window.console.warn = org_warn_console; window.console.error = org_error_console;`
104104
expect(`log[0]`).to match(/Warning: Failed prop( type|Type): In component `Foo`\nProvided prop `on_event` not specified in spec/)
105105
expect(`log[1]`).to eq("Warning: Deprecated feature used in React::Component. In future releases React::Element#on('event') will no longer respond to the '_onEvent' emitter.\nRename your emitter param to 'on_event' or use .on('<_onEvent>')")

spec/react/native_library_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class NestedComponent < React::Component::Base
216216
}
217217
})
218218
}
219-
expect(React.render_to_static_markup(
219+
expect(React::Server.render_to_static_markup(
220220
React.create_element(NativeLibraryTestModule::Component, time_stamp: Time.now))).to match(/<div>Hello There.*<\/div>/)
221221
end
222222

@@ -303,7 +303,7 @@ class NestedComponent < React::Component::Base
303303
}
304304
}
305305

306-
expect(React.render_to_static_markup(
306+
expect(React::Server.render_to_static_markup(
307307
React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))).to match(/<div>Hello There.*<\/div>/)
308308
end
309309

@@ -330,7 +330,7 @@ class NestedComponent < React::Component::Base
330330
}
331331
}
332332
expect do
333-
React.render_to_static_markup(React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))
333+
React::Server.render_to_static_markup(React.create_element(NativeLibraryTestModule::NestedComponent, time_stamp: Time.now))
334334
end.to raise_error("could not import a react component named: NativeLibrary.NativeNestedLibrary.NativeComponent")
335335

336336
end

spec/react/react_spec.rb

-7
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,6 @@ def render
201201
end
202202
end
203203

204-
describe "render_to_string" do
205-
it "should render a React.Element to string" do
206-
ele = React.create_element('span') { "lorem" }
207-
expect(React.render_to_string(ele)).to be_kind_of(String)
208-
end
209-
end
210-
211204
describe "unmount_component_at_node" do
212205
async "should unmount component at node" do
213206
div = `document.createElement("div")`

spec/react/server_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "spec_helper"
2+
3+
if opal?
4+
5+
RSpec.describe React::Server do
6+
after(:each) do
7+
React::API.clear_component_class_cache
8+
end
9+
10+
describe "render_to_string" do
11+
it "should render a React.Element to string" do
12+
ele = React.create_element('span') { "lorem" }
13+
expect(React::Server.render_to_string(ele)).to be_kind_of(String)
14+
end
15+
end
16+
17+
describe "render_to_static_markup" do
18+
it "should render a React.Element to static markup" do
19+
ele = React.create_element('span') { "lorem" }
20+
expect(React::Server.render_to_static_markup(ele)).to eq("<span>lorem</span>")
21+
end
22+
end
23+
end
24+
25+
end

spec/react/state_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def render
4545
var org_error_console = window.console.error;
4646
window.console.warn = window.console.error = function(str){log.push(str)}
4747
}
48-
markup = React.render_to_static_markup(React.create_element(StateTest))
48+
markup = React::Server.render_to_static_markup(React.create_element(StateTest))
4949
`window.console.warn = org_warn_console; window.console.error = org_error_console;`
5050
expect(markup).to eq('<span>Boom</span>')
5151
expect(StateTest.boom).to be_falsy

spec/react/top_level_component_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def render_top_level(controller, component_name)
4545
render_params: {}
4646
}
4747
element = React.create_element(React::TopLevelRailsComponent, params)
48-
React.render_to_static_markup(element)
48+
React::Server.render_to_static_markup(element)
4949
end
5050

5151
describe React::TopLevelRailsComponent do

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def ruby?
2020
require 'react/test/rspec'
2121
require 'react/test/utils'
2222
require 'react/top_level_render'
23+
require 'react/server'
2324

2425
require File.expand_path('../support/react/spec_helpers', __FILE__)
2526

0 commit comments

Comments
 (0)