diff --git a/Rakefile b/Rakefile index f0d1362e..60a360ae 100644 --- a/Rakefile +++ b/Rakefile @@ -8,4 +8,11 @@ RSpec::Core::RakeTask.new(:spec) require 'rubocop/rake_task' RuboCop::RakeTask.new -task default: %i(rubocop spec) +task default: %i(rubocop spec test) + +require 'rake/testtask' +Rake::TestTask.new(:test) do |t| + t.libs << "test" + t.libs << "lib" + t.test_files = FileList["test/**/*_test.rb"] +end diff --git a/test/formtastic_test.rb b/test/formtastic_test.rb new file mode 100644 index 00000000..327d29dd --- /dev/null +++ b/test/formtastic_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' +require 'trix_editor_cases' + +require 'formtastic' +require 'formtastic/inputs/trix_editor_input' + +class FormtasticTest < Minitest::Test + include TrixEditorCases + + include Formtastic::Helpers::FormHelper + Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder + + private + + def render_form + semantic_form_for(@post) do |f| + f.input(:body, as: :trix_editor) + end + end +end diff --git a/test/simple_form_test.rb b/test/simple_form_test.rb new file mode 100644 index 00000000..8b566702 --- /dev/null +++ b/test/simple_form_test.rb @@ -0,0 +1,32 @@ +require 'test_helper' +require 'trix_editor_cases' + +require 'simple_form' + +SimpleForm.setup do |config| + config.custom_inputs_namespaces << 'Trix::SimpleForm' +end + +require 'trix/simple_form/trix_editor_input' + +class SimpleFormTest < Minitest::Test + include TrixEditorCases + + include SimpleForm::ActionViewExtensions::FormHelper + + def controller + Class.new do + def action_name + 'hi' + end + end.new + end + + private + + def render_form + simple_form_for(@post, url: 'some-url') do |f| + f.input(:body, as: :trix_editor) + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 00000000..397632d2 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,5 @@ +$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) + +require 'trix' + +require 'minitest/autorun' diff --git a/test/trix_editor_cases.rb b/test/trix_editor_cases.rb new file mode 100644 index 00000000..bd7b9ab2 --- /dev/null +++ b/test/trix_editor_cases.rb @@ -0,0 +1,72 @@ +require 'active_model' +require 'action_view' + +class Post + include ActiveModel::Model + attr_accessor :body +end + +module RailsCompatibilityLayer + include ActionPack + include ActionView::Context if defined?(ActionView::Context) + include ActionController::RecordIdentifier if defined?(ActionController::RecordIdentifier) + include ActionView::Helpers::FormHelper + include ActionDispatch::Routing::PolymorphicRoutes + include ActionView::RecordIdentifier if defined?(ActionView::RecordIdentifier) + + require 'rails/dom/testing/assertions' + include Rails::Dom::Testing::Assertions + + class Application < Rails::Application + # Configure the default encoding used in templates for Ruby 1.9. + config.encoding = 'utf-8' + config.active_support.deprecation = :stderr + config.secret_key_base = 'secret' + config.eager_load = false + end + + def protect_against_forgery? + false + end + + def posts_path(*) + '' + end + + def _routes + Rails.application.routes + end +end + +module TrixEditorCases + include RailsCompatibilityLayer + + def setup + @output_buffer = '' + @post = Post.new(body: 'body') + + concat render_form + end + + def test_contains_hidden_input_field + assert_select format('input[type="hidden"][id="post_body"][value="%s"]', @post.body) + end + + def test_contains_editor_tag + assert_select 'trix-editor[input="post_body"]' + end + + def test_contains_class + assert_select 'trix-editor.trix-content' + end + + private + + def document_root_element + Nokogiri::HTML::Document.parse(@output_buffer).root + end + + def render_form + raise NotImplementedError + end +end diff --git a/test/trix_test.rb b/test/trix_test.rb new file mode 100644 index 00000000..d06cf4ff --- /dev/null +++ b/test/trix_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TrixTest < Minitest::Test + def test_trix_version_not_nil + refute_nil Trix::VERSION + end +end diff --git a/trix.gemspec b/trix.gemspec index 8d495560..b65d6a82 100644 --- a/trix.gemspec +++ b/trix.gemspec @@ -25,7 +25,9 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'appraisal' spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'formtastic', '~> 3.0' + spec.add_development_dependency 'minitest' spec.add_development_dependency 'simple_form', '~> 3.5' + spec.add_development_dependency 'rails-dom-testing' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec-rails' spec.add_development_dependency 'rspec-activemodel-mocks'