33namespace Arrilot \Widgets \Test ;
44
55use Arrilot \Widgets \Factories \AsyncWidgetFactory ;
6+ use Arrilot \Widgets \Test \Support \TestApplicationWrapper ;
7+ use Arrilot \Widgets \Test \Support \TestCase ;
68
79class AsyncWidgetFactoryTest extends TestCase
810{
@@ -20,65 +22,73 @@ public function testItCanRunAsyncWidget()
2022 {
2123 $ output = $ this ->factory ->run ('testDefaultSlider ' );
2224
23- $ this -> assertEquals (
25+ $ expected =
2426 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container"> ' .
2527 '<script type="text/javascript"> ' .
2628 'var widgetTimer1 = setInterval(function() { ' .
2729 'if (window.$) { ' .
28- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('TestDefaultSlider ' ).'); ' .
30+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('TestDefaultSlider ' )." '); " .
2931 'clearInterval(widgetTimer1); ' .
3032 '} ' .
3133 '}, 100); ' .
3234 '</script> ' .
33- '</div> ' , $ output );
35+ '</div> ' ;
36+
37+ $ this ->assertEquals ($ expected , $ output );
3438 }
3539
3640 public function testItCanRunAsyncWidgetWithPlaceholder ()
3741 {
3842 $ output = $ this ->factory ->run ('slider ' );
3943
40- $ this -> assertEquals (
44+ $ expected =
4145 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here! ' .
4246 '<script type="text/javascript"> ' .
4347 'var widgetTimer1 = setInterval(function() { ' .
4448 'if (window.$) { ' .
45- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('Slider ' ).'); ' .
49+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('Slider ' )." '); " .
4650 'clearInterval(widgetTimer1); ' .
4751 '} ' .
4852 '}, 100); ' .
4953 '</script> ' .
50- '</div> ' , $ output );
54+ '</div> ' ;
55+
56+ $ this ->assertEquals ($ expected , $ output );
5157 }
5258
5359 public function testItCanRunMultipleAsyncWidgets ()
5460 {
5561 $ output = $ this ->factory ->run ('slider ' );
5662
57- $ this -> assertEquals (
63+ $ expected =
5864 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here! ' .
5965 '<script type="text/javascript"> ' .
6066 'var widgetTimer1 = setInterval(function() { ' .
6167 'if (window.$) { ' .
62- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('Slider ' ).'); ' .
68+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('Slider ' )." '); " .
6369 'clearInterval(widgetTimer1); ' .
6470 '} ' .
6571 '}, 100); ' .
6672 '</script> ' .
67- '</div> ' , $ output );
73+ '</div> ' ;
74+
75+ $ this ->assertEquals ($ expected , $ output );
6876
6977 $ output = $ this ->factory ->run ('testDefaultSlider ' );
7078
71- $ this -> assertEquals (
79+ $ expected =
7280 '<div id="arrilot-widget-container-2" style="display:inline" class="arrilot-widget-container"> ' .
7381 '<script type="text/javascript"> ' .
7482 'var widgetTimer2 = setInterval(function() { ' .
7583 'if (window.$) { ' .
76- "$('#arrilot-widget-container-2').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('TestDefaultSlider ' , [], 2 ).'); ' .
84+ "$('#arrilot-widget-container-2').load(' " .$ this ->ajaxUrl ('TestDefaultSlider ' , [], 2 )." '); " .
7785 'clearInterval(widgetTimer2); ' .
7886 '} ' .
7987 '}, 100); ' .
8088 '</script> ' .
81- '</div> ' , $ output );
89+ '</div> ' ;
90+
91+ $ this ->assertEquals ($ expected , $ output );
8292 }
8393
8494 public function testItCanRunAsyncWidgetWithAdditionalParams ()
@@ -89,67 +99,108 @@ public function testItCanRunAsyncWidgetWithAdditionalParams()
8999 ];
90100
91101 $ output = $ this ->factory ->run ('testWidgetWithParamsInRun ' , [], 'parameter ' );
92- $ this ->assertEquals (
102+
103+ $ expected =
93104 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here! ' .
94105 '<script type="text/javascript"> ' .
95106 'var widgetTimer1 = setInterval(function() { ' .
96107 'if (window.$) { ' .
97- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('TestWidgetWithParamsInRun ' , $ params ).'); ' .
108+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('TestWidgetWithParamsInRun ' , $ params )." '); " .
98109 'clearInterval(widgetTimer1); ' .
99110 '} ' .
100111 '}, 100); ' .
101112 '</script> ' .
102- '</div> ' , $ output );
113+ '</div> ' ;
114+
115+ $ this ->assertEquals ($ expected , $ output );
103116 }
104117
105118 public function testItCanRunAsyncWidgetWithMagicMethod ()
106119 {
107120 $ output = $ this ->factory ->slider ();
108121
109- $ this -> assertEquals (
122+ $ expected =
110123 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container">Placeholder here! ' .
111124 '<script type="text/javascript"> ' .
112125 'var widgetTimer1 = setInterval(function() { ' .
113126 'if (window.$) { ' .
114- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('Slider ' ).'); ' .
127+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('Slider ' )." '); " .
115128 'clearInterval(widgetTimer1); ' .
116129 '} ' .
117130 '}, 100); ' .
118131 '</script> ' .
119- '</div> ' , $ output );
132+ '</div> ' ;
133+
134+ $ this ->assertEquals ($ expected , $ output );
120135 }
121136
122137 public function testItCanRunNestedAsyncWidget ()
123138 {
124139 $ output = $ this ->factory ->run ('Profile\TestNamespace\TestFeed ' );
125140
126- $ this -> assertEquals (
141+ $ expected =
127142 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container"> ' .
128143 '<script type="text/javascript"> ' .
129144 'var widgetTimer1 = setInterval(function() { ' .
130145 'if (window.$) { ' .
131- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('Profile\TestNamespace\TestFeed ' ).'); ' .
146+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('Profile\TestNamespace\TestFeed ' )." '); " .
132147 'clearInterval(widgetTimer1); ' .
133148 '} ' .
134149 '}, 100); ' .
135150 '</script> ' .
136- '</div> ' , $ output );
151+ '</div> ' ;
152+
153+ $ this ->assertEquals ($ expected , $ output );
137154 }
138155
139156 public function testItCanRunNestedAsyncWidgetUsingDotNotation ()
140157 {
141158 $ output = $ this ->factory ->run ('profile.testNamespace.testFeed ' );
142159
143- $ this -> assertEquals (
160+ $ expected =
144161 '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container"> ' .
145162 '<script type="text/javascript"> ' .
146163 'var widgetTimer1 = setInterval(function() { ' .
147164 'if (window.$) { ' .
148- "$('#arrilot-widget-container-1').load('/arrilot/load-widget', " .$ this ->javascriptDataStub ('Profile\testNamespace\testFeed ' ).'); ' .
165+ "$('#arrilot-widget-container-1').load(' " .$ this ->ajaxUrl ('Profile\testNamespace\testFeed ' )." '); " .
149166 'clearInterval(widgetTimer1); ' .
150167 '} ' .
151168 '}, 100); ' .
152169 '</script> ' .
153- '</div> ' , $ output );
170+ '</div> ' ;
171+
172+ $ this ->assertEquals ($ expected , $ output );
173+ }
174+
175+ public function testItCanRunAsyncWidgetWithoutJquery ()
176+ {
177+
178+ $ this ->factory ->app ->config ['laravel-widgets.use_jquery_for_ajax_calls ' ] = false ;
179+
180+ $ output = $ this ->factory ->run ('testDefaultSlider ' );
181+
182+ $ expected =
183+ '<div id="arrilot-widget-container-1" style="display:inline" class="arrilot-widget-container"> ' .
184+ '<script type="text/javascript"> ' .
185+ 'var xhr = new XMLHttpRequest(); ' .
186+ 'xhr.open("GET", " ' .$ this ->ajaxUrl ('TestDefaultSlider ' ).'", true); ' .
187+ 'xhr.onreadystatechange = function() { ' .
188+ 'if(xhr.readyState == 4 && xhr.status == 200) { ' .
189+ 'var container = document.getElementById("arrilot-widget-container-1"); ' .
190+ 'container.innerHTML = xhr.responseText; ' .
191+ 'var scripts = container.getElementsByTagName("script"); ' .
192+ 'for(var i=0; i < scripts.length; i++) { ' .
193+ 'if (window.execScript) { ' .
194+ 'window.execScript(scripts[i].text); ' .
195+ '} else { ' .
196+ 'window["eval"].call(window, scripts[i].text); ' .
197+ '} ' .
198+ '} ' .
199+ '} ' .
200+ '}; ' .
201+ 'xhr.send(); ' .
202+ '</script> ' .
203+ '</div> ' ;
204+ $ this ->assertEquals ($ expected , $ output );
154205 }
155206}
0 commit comments