Skip to content

Commit fa0f0aa

Browse files
authored
Merge pull request #463 from matestack/20201006_#458_async-on-app-level
Enable async components on app level
2 parents 50ed0b3 + 3927299 commit fa0f0aa

File tree

8 files changed

+92
-7
lines changed

8 files changed

+92
-7
lines changed

app/lib/matestack/ui/core/rendering/main_renderer.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def render(controller_instance, page_class, options)
2121
render_matestack_object(controller_instance, page_instance)
2222
elsif (params[:component_key].present? && params[:component_class].blank?)
2323
# async component rerendering from non isolated context
24+
app_instance = app_class.new(page_class, controller_instance, context)
2425
component_key = params[:component_key]
2526
page_instance = page_class.new(controller_instance: controller_instance, context: context)
26-
render_component(component_key, page_instance, controller_instance, context)
27+
render_component(component_key, app_instance, controller_instance, context)
2728
elsif (params[:component_class].present? && params[:component_key].blank?)
2829
# isolated component rendering
2930
component_class = params[:component_class]
@@ -75,15 +76,15 @@ def render_matestack_object(controller_instance, object, opts = {}, render_metho
7576
controller_instance.render rendering_options
7677
end
7778

78-
def render_component(component_key, page_instance, controller_instance, context)
79+
def render_component(component_key, app_or_page_instance, controller_instance, context)
7980
matched_component = nil
8081

81-
page_instance.matestack_set_skip_defer(false)
82+
app_or_page_instance.matestack_set_skip_defer(false)
8283

83-
page_instance.prepare
84-
page_instance.response
84+
app_or_page_instance.prepare
85+
app_or_page_instance.response
8586

86-
matched_component = dig_for_component(component_key, page_instance)
87+
matched_component = dig_for_component(component_key, app_or_page_instance)
8788

8889
unless matched_component.nil?
8990
render_matestack_object(controller_instance, matched_component, {}, :render_content)

spec/rails_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is copied to spec/ when you run 'rails generate rspec:install'
22
require 'spec_helper'
33
ENV['RAILS_ENV'] ||= 'test'
4-
require File.expand_path('../../config/environment', __FILE__)
4+
# require File.expand_path('../../config/environment', __dir__)
55
# Prevent database truncation if the environment is production
66
abort("The Rails environment is running in production mode!") if Rails.env.production?
77
require 'rspec/rails'

spec/spec_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,14 @@
140140
# as the one that triggered the failure.
141141
Kernel.srand config.seed
142142
=end
143+
144+
config.before :all, type: :feature do
145+
unless Rails.application.routes.url_helpers.method_defined?(:matestack_components_test_path)
146+
Rails.application.routes.append do
147+
get '/matestack_components_test', to: 'matestack_components#matestack_components_test', as: :matestack_components_test
148+
end
149+
Rails.application.reload_routes!
150+
end
151+
end
152+
143153
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'rails_helper'
2+
3+
describe "Async Component", type: :feature, js: true do
4+
include Utils
5+
6+
it 'works on page level' do
7+
matestack_render do
8+
async id: 'async', rerender_on: 'update' do
9+
plain 'Time now: '
10+
paragraph text: DateTime.now.strftime("%Q")
11+
end
12+
end
13+
14+
expect(page).to have_content('Time now:')
15+
initial_timestamp = page.find("p").text # initial page load
16+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("update")')
17+
expect(page).to have_content('Time now:')
18+
expect(page).not_to have_content(initial_timestamp)
19+
end
20+
21+
it 'should work on app level' do
22+
matestack_app do
23+
async id: 'async', rerender_on: 'update' do
24+
plain 'Time now: '
25+
paragraph text: DateTime.now.strftime("%Q")
26+
end
27+
yield_page
28+
end
29+
matestack_render reset_app: false do
30+
plain 'A page inside the app'
31+
end
32+
33+
expect(page).to have_content('A page inside the app')
34+
expect(page).to have_content('Time now:')
35+
initial_timestamp = page.find("p").text # initial page load
36+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("update")')
37+
expect(page).to have_content('Time now:')
38+
expect(page).not_to have_content(initial_timestamp)
39+
reset_matestack_app
40+
end
41+
42+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MatestackComponentsController < ApplicationController
2+
include Matestack::Ui::Core::ApplicationHelper
3+
4+
matestack_app MatestackWrapperApp
5+
6+
def matestack_components_test
7+
render MatestackWrapperPage
8+
end
9+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class MatestackWrapperApp < Matestack::Ui::App
2+
def response
3+
yield_page
4+
end
5+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class MatestackWrapperPage < Matestack::Ui::Page
2+
def response
3+
end
4+
end

spec/test/support/utils.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ def register_component(dsl_method, component_class)
2323
def register_self_as(dsl_method)
2424
register_component(dsl_method, self)
2525
end
26+
27+
def matestack_render(reset_app: true, &block)
28+
MatestackWrapperPage.define_method(:response, block)
29+
visit matestack_components_test_path
30+
reset_matestack_app if reset_app
31+
end
32+
33+
def matestack_app(&block)
34+
MatestackWrapperApp.define_method(:response, block)
35+
end
36+
37+
def reset_matestack_app
38+
matestack_app { yield_page }
39+
end
2640
end

0 commit comments

Comments
 (0)