File tree Expand file tree Collapse file tree 4 files changed +42
-13
lines changed Expand file tree Collapse file tree 4 files changed +42
-13
lines changed Original file line number Diff line number Diff line change 1414 // `data-react-class` DOM elements
1515 findDOMNodes : function ( searchSelector ) {
1616 // we will use fully qualified paths as we do not bind the callbacks
17- var selector ;
18- if ( typeof searchSelector === 'undefined' ) {
19- var selector = '[' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
20- } else {
21- var selector = searchSelector + ' [' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
17+ var selector , parent ;
18+
19+ switch ( typeof searchSelector ) {
20+ case 'undefined' :
21+ selector = '[' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
22+ parent = document ;
23+ break ;
24+ case 'object' :
25+ selector = '[' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
26+ parent = searchSelector ;
27+ break ;
28+ case 'string' :
29+ selector = searchSelector + ' [' + window . ReactRailsUJS . CLASS_NAME_ATTR + ']' ;
30+ parent = document ;
31+ break
32+ default :
33+ break ;
2234 }
2335
2436 if ( $ ) {
25- return $ ( selector ) ;
37+ return $ ( selector , parent ) ;
2638 } else {
27- return document . querySelectorAll ( selector ) ;
39+ return parent . querySelectorAll ( selector ) ;
2840 }
2941 } ,
3042
Original file line number Diff line number Diff line change 22 < li > <%= link_to 'Alice' , page_path ( :id => 0 ) %> </ li >
33 < li > <%= link_to 'Bob' , page_path ( :id => 1 ) %> </ li >
44</ ul >
5+
56< div id ='test-component '>
67 <%= react_component 'HelloMessage' , :name => @name %>
78</ div >
8- < a href ='# ' onClick ="ReactRailsUJS.unmountComponents('#test-component') "> Unmount at #test-component</ a >
9- < a href ='# ' onClick ="ReactRailsUJS.mountComponents('#test-component') "> Mount at #test-component</ a >
9+
10+ < a href ='# ' onClick ="ReactRailsUJS.unmountComponents('#test-component') "> Unmount at selector #test-component</ a >
11+ < a href ='# ' onClick ="ReactRailsUJS.mountComponents('#test-component') "> Mount at selector #test-component</ a >
12+
13+ < a href ='# ' onClick ="ReactRailsUJS.unmountComponents(document.querySelector('#test-component')) "> Unmount at node #test-component</ a >
14+ < a href ='# ' onClick ="ReactRailsUJS.mountComponents(document.querySelector('#test-component')) "> Mount at node #test-component</ a >
Original file line number Diff line number Diff line change @@ -34,7 +34,8 @@ def copy_directory(dir)
3434 assert_application_file_modified
3535 end
3636
37- test "modifies `application.js` it's empty" do
37+ test "modifies `application.js` if it's empty" do
38+ FileUtils . mkdir_p destination_root + '/app/assets/javascripts/'
3839 File . write ( destination_root + '/app/assets/javascripts/application.js' , '' )
3940
4041 run_generator
Original file line number Diff line number Diff line change @@ -87,14 +87,25 @@ class ViewHelperTest < ActionDispatch::IntegrationTest
8787 assert page . has_content? ( 'Hello Bob' )
8888 end
8989
90- test 'react_ujs can unount at node ' do
90+ test 'react_ujs can unmount/mount using a selector reference ' do
9191 visit '/pages/1'
9292 assert page . has_content? ( 'Hello Bob' )
9393
94- page . click_link ' Unmount at #test-component'
94+ page . click_link " Unmount at selector #test-component"
9595 assert page . has_no_content? ( 'Hello Bob' )
9696
97- page . click_link 'Mount at #test-component'
97+ page . click_link "Mount at selector #test-component"
98+ assert page . has_content? ( 'Hello Bob' )
99+ end
100+
101+ test 'react_ujs can unmount/mount using a dom node context' do
102+ visit '/pages/1'
103+ assert page . has_content? ( 'Hello Bob' )
104+
105+ page . click_link "Unmount at node #test-component"
106+ assert page . has_no_content? ( 'Hello Bob' )
107+
108+ page . click_link "Mount at node #test-component"
98109 assert page . has_content? ( 'Hello Bob' )
99110 end
100111
You can’t perform that action at this time.
0 commit comments