view_component/test/test_helper.rb

161 строка
4.3 KiB
Ruby
Исходник Обычный вид История

2019-08-16 20:09:33 +03:00
# frozen_string_literal: true
require "simplecov"
2020-07-27 23:46:40 +03:00
require "simplecov-console"
2020-09-22 01:56:14 +03:00
if ENV["MEASURE_COVERAGE"]
2020-09-22 01:54:36 +03:00
SimpleCov.start do
command_name "rails#{ENV["RAILS_VERSION"]}-ruby#{ENV["RUBY_VERSION"]}" if ENV["RUBY_VERSION"]
2020-08-22 01:23:38 +03:00
2020-09-22 01:54:36 +03:00
formatter SimpleCov::Formatter::Console
end
end
2019-08-16 20:09:33 +03:00
require "bundler/setup"
require "pp"
require "pathname"
require "minitest/autorun"
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"
require "view_component/deprecation"
ViewComponent::Deprecation.behavior = :silence
require File.expand_path("../sandbox/config/environment.rb", __FILE__)
2019-08-16 20:09:33 +03:00
require "rails/test_help"
2021-02-17 04:36:40 +03:00
# Sets custom preview paths in tests.
#
# @param new_value [Array<String>] List of preview paths
# @yield Test code to run
# @return [void]
def with_preview_paths(new_value)
old_value = Rails.application.config.view_component.preview_paths
Rails.application.config.view_component.preview_paths = new_value
yield
Rails.application.config.view_component.preview_paths = old_value
end
def with_preview_route(new_value)
old_value = Rails.application.config.view_component.preview_route
Rails.application.config.view_component.preview_route = new_value
app.reloader.reload!
yield
Rails.application.config.view_component.preview_route = old_value
app.reloader.reload!
end
2020-10-04 04:33:05 +03:00
def with_preview_controller(new_value)
old_value = Rails.application.config.view_component.preview_controller
Rails.application.config.view_component.preview_controller = new_value
app.reloader.reload!
yield
Rails.application.config.view_component.preview_controller = old_value
app.reloader.reload!
end
def with_custom_component_path(new_value)
old_value = ViewComponent::Base.view_component_path
ViewComponent::Base.view_component_path = new_value
yield
ensure
ViewComponent::Base.view_component_path = old_value
end
2021-09-17 20:30:48 +03:00
def with_custom_component_parent_class(new_value)
old_value = ViewComponent::Base.component_parent_class
ViewComponent::Base.component_parent_class = new_value
yield
ensure
ViewComponent::Base.component_parent_class = old_value
end
def with_application_component_class
Object.const_set("ApplicationComponent", Class.new(Object))
yield
ensure
Object.send(:remove_const, :ApplicationComponent)
end
def with_generate_sidecar(enabled)
old_value = ViewComponent::Base.generate.sidecar
ViewComponent::Base.generate.sidecar = enabled
yield
ensure
ViewComponent::Base.generate.sidecar = old_value
end
def with_new_cache
begin
old_cache = ViewComponent::CompileCache.cache
ViewComponent::CompileCache.cache = Set.new
2021-05-10 22:32:29 +03:00
old_cache_template_loading = ActionView::Base.cache_template_loading
ActionView::Base.cache_template_loading = false
reset_render_template_methods
yield
ensure
2021-05-10 22:32:29 +03:00
ActionView::Base.cache_template_loading = old_cache_template_loading
ViewComponent::CompileCache.cache = old_cache
reset_render_template_methods
end
end
def reset_render_template_methods
ViewComponent::Base.descendants.each do |klass|
klass.compiler.reset_render_template_for
end
end
def without_template_annotations
2021-05-10 17:58:56 +03:00
if ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames)
old_value = ActionView::Base.annotate_rendered_view_with_filenames
ActionView::Base.annotate_rendered_view_with_filenames = false
app.reloader.reload! if defined?(app)
2021-05-10 17:58:56 +03:00
with_new_cache do
yield
end
ActionView::Base.annotate_rendered_view_with_filenames = old_value
app.reloader.reload! if defined?(app)
2021-05-10 17:58:56 +03:00
else
yield
end
end
def modify_file(file, content)
filename = Rails.root.join(file)
old_content = File.read(filename)
begin
File.open(filename, "wb+") { |f| f.write(content) }
yield
ensure
File.open(filename, "wb+") { |f| f.write(old_content) }
end
end
def with_default_preview_layout(layout)
old_value = ViewComponent::Base.default_preview_layout
ViewComponent::Base.default_preview_layout = layout
yield
ViewComponent::Base.default_preview_layout = old_value
end
def with_render_monkey_patch_config(enabled)
old_default = ViewComponent::Base.render_monkey_patch_enabled
ViewComponent::Base.render_monkey_patch_enabled = enabled
yield
ensure
ViewComponent::Base.render_monkey_patch_enabled = old_default
end
Improve thread safety with development compiler mode (#1202) * Improved threadsafety * add blocking and non blocking compiler mode (blocking in Rails dev & test mode) * add monitor check in render method to ensure thread safety * fixes https://github.com/github/view_component/issues/1177 * Add test for both compiler modes * refactor tests * Refactorings: * rename blocking & non-blocking to development & production * use constants * some more renamings * Refactorings: * remove do_compile since extraction is no longer needed for DRY * Refactorings: * when compiler runs in production mode, don't add monitor check in the `.call` method * Refactorings: * refactor tests * Make compiler mode a class attribute on the compiler * add test * make test more explicit * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * DRY method implementation * fix order * Update docs/CHANGELOG.md Co-authored-by: Simon Fish <si@mon.fish> * Update lib/view_component/compiler.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * enhance comment * revert test * fix error * remove test which causes failures with multithread test * Revert "remove test which causes failures with multithread test" This reverts commit 87d5c1a9b12ff6c727889d9159a26c75e462d472. * remove test which causes failures with multithread test * Re-run the initializer after playing around with Rails.env * fix linter * Update test/view_component/integration_test.rb Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Joel Hawksley <joelhawksley@github.com> Co-authored-by: Blake Williams <blakewilliams@github.com> Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
2022-01-06 00:41:17 +03:00
def with_compiler_mode(mode)
previous_mode = ViewComponent::Compiler.mode
ViewComponent::Compiler.mode = mode
yield
ensure
ViewComponent::Compiler.mode = previous_mode
end