view_component/test/test_helper.rb

134 строки
3.7 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 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_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
yield
ensure
2021-05-10 22:32:29 +03:00
ActionView::Base.cache_template_loading = old_cache_template_loading
ViewComponent::CompileCache.cache = old_cache
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