|
188 | 188 | end |
189 | 189 | end |
190 | 190 | end |
| 191 | + |
| 192 | + describe "#check_async_usage" do |
| 193 | + let(:checker) { instance_double(ReactOnRails::SystemChecker) } |
| 194 | + |
| 195 | + before do |
| 196 | + allow(doctor).to receive(:checker).and_return(checker) |
| 197 | + allow(checker).to receive_messages(add_error: true, add_warning: true, add_info: true) |
| 198 | + allow(File).to receive(:exist?).and_call_original |
| 199 | + allow(Dir).to receive(:glob).and_return([]) |
| 200 | + end |
| 201 | + |
| 202 | + context "when Pro gem is installed" do |
| 203 | + before do |
| 204 | + allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(true) |
| 205 | + end |
| 206 | + |
| 207 | + it "skips the check" do |
| 208 | + doctor.send(:check_async_usage) |
| 209 | + expect(checker).not_to have_received(:add_error) |
| 210 | + expect(checker).not_to have_received(:add_warning) |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + context "when Pro gem is not installed" do |
| 215 | + before do |
| 216 | + allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false) |
| 217 | + end |
| 218 | + |
| 219 | + context "when async is used in view files" do |
| 220 | + before do |
| 221 | + allow(Dir).to receive(:glob).with("app/views/**/*.erb").and_return(["app/views/layouts/application.html.erb"]) |
| 222 | + allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([]) |
| 223 | + allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true) |
| 224 | + allow(File).to receive(:read).with("app/views/layouts/application.html.erb") |
| 225 | + .and_return('<%= javascript_pack_tag "application", :async %>') |
| 226 | + allow(File).to receive(:exist?).with("config/initializers/react_on_rails.rb").and_return(false) |
| 227 | + allow(doctor).to receive(:relativize_path).with("app/views/layouts/application.html.erb") |
| 228 | + .and_return("app/views/layouts/application.html.erb") |
| 229 | + end |
| 230 | + |
| 231 | + it "reports an error" do |
| 232 | + doctor.send(:check_async_usage) |
| 233 | + expect(checker).to have_received(:add_error).with("🚫 :async usage detected without React on Rails Pro") |
| 234 | + expect(checker).to have_received(:add_error) |
| 235 | + .with(" javascript_pack_tag with :async found in view files:") |
| 236 | + end |
| 237 | + end |
| 238 | + |
| 239 | + context "when generated_component_packs_loading_strategy is :async" do |
| 240 | + before do |
| 241 | + allow(File).to receive(:exist?).with("config/initializers/react_on_rails.rb").and_return(true) |
| 242 | + allow(File).to receive(:read).with("config/initializers/react_on_rails.rb") |
| 243 | + .and_return("config.generated_component_packs_loading_strategy = :async") |
| 244 | + end |
| 245 | + |
| 246 | + it "reports an error" do |
| 247 | + doctor.send(:check_async_usage) |
| 248 | + expect(checker).to have_received(:add_error).with("🚫 :async usage detected without React on Rails Pro") |
| 249 | + expect(checker).to have_received(:add_error) |
| 250 | + .with(" config.generated_component_packs_loading_strategy = :async in initializer") |
| 251 | + end |
| 252 | + end |
| 253 | + |
| 254 | + context "when no async usage is detected" do |
| 255 | + before do |
| 256 | + allow(File).to receive(:exist?).with("config/initializers/react_on_rails.rb").and_return(true) |
| 257 | + allow(File).to receive(:read).with("config/initializers/react_on_rails.rb") |
| 258 | + .and_return("config.generated_component_packs_loading_strategy = :defer") |
| 259 | + end |
| 260 | + |
| 261 | + it "does not report any issues" do |
| 262 | + doctor.send(:check_async_usage) |
| 263 | + expect(checker).not_to have_received(:add_error) |
| 264 | + expect(checker).not_to have_received(:add_warning) |
| 265 | + end |
| 266 | + end |
| 267 | + end |
| 268 | + end |
| 269 | + |
| 270 | + describe "#scan_view_files_for_async_pack_tag" do |
| 271 | + before do |
| 272 | + allow(Dir).to receive(:glob).and_call_original |
| 273 | + allow(File).to receive(:exist?).and_call_original |
| 274 | + allow(File).to receive(:read).and_call_original |
| 275 | + end |
| 276 | + |
| 277 | + context "when view files contain javascript_pack_tag with :async" do |
| 278 | + before do |
| 279 | + allow(Dir).to receive(:glob).with("app/views/**/*.erb") |
| 280 | + .and_return(["app/views/layouts/application.html.erb"]) |
| 281 | + allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([]) |
| 282 | + allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true) |
| 283 | + allow(File).to receive(:read).with("app/views/layouts/application.html.erb") |
| 284 | + .and_return('<%= javascript_pack_tag "app", :async %>') |
| 285 | + allow(doctor).to receive(:relativize_path).with("app/views/layouts/application.html.erb") |
| 286 | + .and_return("app/views/layouts/application.html.erb") |
| 287 | + end |
| 288 | + |
| 289 | + it "returns files with async" do |
| 290 | + files = doctor.send(:scan_view_files_for_async_pack_tag) |
| 291 | + expect(files).to include("app/views/layouts/application.html.erb") |
| 292 | + end |
| 293 | + end |
| 294 | + |
| 295 | + context "when view files do not contain async" do |
| 296 | + before do |
| 297 | + allow(Dir).to receive(:glob).with("app/views/**/*.erb") |
| 298 | + .and_return(["app/views/layouts/application.html.erb"]) |
| 299 | + allow(Dir).to receive(:glob).with("app/views/**/*.haml").and_return([]) |
| 300 | + allow(File).to receive(:exist?).with("app/views/layouts/application.html.erb").and_return(true) |
| 301 | + allow(File).to receive(:read).with("app/views/layouts/application.html.erb") |
| 302 | + .and_return('<%= javascript_pack_tag "app" %>') |
| 303 | + end |
| 304 | + |
| 305 | + it "returns empty array" do |
| 306 | + files = doctor.send(:scan_view_files_for_async_pack_tag) |
| 307 | + expect(files).to be_empty |
| 308 | + end |
| 309 | + end |
| 310 | + end |
| 311 | + |
| 312 | + describe "#config_has_async_loading_strategy?" do |
| 313 | + context "when config file has :async strategy" do |
| 314 | + before do |
| 315 | + allow(File).to receive(:exist?).with("config/initializers/react_on_rails.rb").and_return(true) |
| 316 | + allow(File).to receive(:read).with("config/initializers/react_on_rails.rb") |
| 317 | + .and_return("config.generated_component_packs_loading_strategy = :async") |
| 318 | + end |
| 319 | + |
| 320 | + it "returns true" do |
| 321 | + expect(doctor.send(:config_has_async_loading_strategy?)).to be true |
| 322 | + end |
| 323 | + end |
| 324 | + |
| 325 | + context "when config file has different strategy" do |
| 326 | + before do |
| 327 | + allow(File).to receive(:exist?).with("config/initializers/react_on_rails.rb").and_return(true) |
| 328 | + allow(File).to receive(:read).with("config/initializers/react_on_rails.rb") |
| 329 | + .and_return("config.generated_component_packs_loading_strategy = :defer") |
| 330 | + end |
| 331 | + |
| 332 | + it "returns false" do |
| 333 | + expect(doctor.send(:config_has_async_loading_strategy?)).to be false |
| 334 | + end |
| 335 | + end |
| 336 | + |
| 337 | + context "when config file does not exist" do |
| 338 | + before do |
| 339 | + allow(File).to receive(:exist?).with("config/initializers/react_on_rails.rb").and_return(false) |
| 340 | + end |
| 341 | + |
| 342 | + it "returns false" do |
| 343 | + expect(doctor.send(:config_has_async_loading_strategy?)).to be false |
| 344 | + end |
| 345 | + end |
| 346 | + end |
191 | 347 | end |
0 commit comments