forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `have_rich_text` matcher tests usage of the `has_rich_text` macro that was added in Rails 6.
- Loading branch information
Showing
8 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
lib/shoulda/matchers/active_record/have_rich_text_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveRecord | ||
# The `have_rich_text` matcher tests usage of the | ||
# `has_rich_text` macro. | ||
# | ||
# #### Example | ||
# | ||
# class Post < ActiveRecord | ||
# has_rich_text :content | ||
# end | ||
# | ||
# # RSpec | ||
# RSpec.describe Post, type: :model do | ||
# it { is_expected.to have_rich_text(:content) } | ||
# end | ||
# | ||
# # Minitest (Shoulda) | ||
# class PostTest < ActiveSupport::TestCase | ||
# should have_rich_text(:content) | ||
# end | ||
# | ||
# @return [HaveRichText] | ||
# | ||
def have_rich_text(rich_text_attribute) | ||
HaveRichText.new(rich_text_attribute) | ||
end | ||
|
||
# @private | ||
class HaveRichText | ||
def initialize(rich_text_attribute) | ||
@rich_text_attribute = rich_text_attribute | ||
end | ||
|
||
def description | ||
"have configured :#{rich_text_attribute} as a ActionText::RichText association" | ||
end | ||
|
||
def failure_message | ||
"Expected #{subject.class} to #{error_description}" | ||
end | ||
|
||
def failure_message_when_negated | ||
"Did not expect #{subject.class} to have ActionText::RichText :#{rich_text_attribute}" | ||
end | ||
|
||
def matches?(subject) | ||
@subject = subject | ||
@error = run_checks | ||
@error.nil? | ||
end | ||
|
||
private | ||
|
||
attr_reader :error, :rich_text_attribute, :subject | ||
|
||
def run_checks | ||
if !has_attribute? | ||
":#{rich_text_attribute} does not exist" | ||
elsif !has_expected_action_text? | ||
:default | ||
end | ||
end | ||
|
||
def has_attribute? | ||
@subject.respond_to?(rich_text_attribute.to_s) | ||
end | ||
|
||
def has_expected_action_text? | ||
@subject.send(rich_text_attribute).class.name == 'ActionText::RichText' | ||
end | ||
|
||
def error_description | ||
error == :default ? description : "#{description} but #{error}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
spec/unit/shoulda/matchers/active_record/have_rich_text_matcher_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'unit_spec_helper' | ||
|
||
describe Shoulda::Matchers::ActiveRecord::HaveRichText, type: :model do | ||
def self.rich_text_is_defined? | ||
defined?(ActionText::RichText) | ||
end | ||
|
||
context '#description' do | ||
it 'returns the message including the name of the provided association' do | ||
matcher = have_rich_text(:content) | ||
expect(matcher.description). | ||
to eq('have configured :content as a ActionText::RichText association') | ||
end | ||
end | ||
|
||
if rich_text_is_defined? | ||
context 'when the model has a RichText association' do | ||
it 'matches when the subject configures has_rich_text' do | ||
valid_record = new_post(is_rich_text_association: true) | ||
|
||
expected_message = 'Did not expect Post to have ActionText::RichText :content' | ||
|
||
expect { have_rich_text(:content) }. | ||
to match_against(valid_record). | ||
or_fail_with(expected_message) | ||
end | ||
end | ||
|
||
context 'when the model does not have a RichText association' do | ||
it 'does not match when provided with a model attribute that exist' do | ||
invalid_record = new_post(has_invalid_content: true) | ||
expected_message = 'Expected Post to have configured :invalid_content as a ' \ | ||
'ActionText::RichText association' | ||
|
||
expect { have_rich_text(:invalid_content) }. | ||
not_to match_against(invalid_record). | ||
and_fail_with(expected_message) | ||
end | ||
|
||
it 'does not match when provided with a model attribute that does not exist' do | ||
invalid_record = new_post | ||
expected_message = 'Expected Post to have configured :invalid_attribute as a ' \ | ||
'ActionText::RichText association but :invalid_attribute does not exist' | ||
|
||
expect { have_rich_text(:invalid_attribute) }. | ||
not_to match_against(invalid_record). | ||
and_fail_with(expected_message) | ||
end | ||
end | ||
else | ||
it 'does not match when provided with a model attribute that exist' do | ||
invalid_record = new_post(has_invalid_content: true) | ||
expected_message = 'Expected Post to have configured :invalid_content as a ' \ | ||
'ActionText::RichText association' | ||
|
||
expect { have_rich_text(:invalid_content) }. | ||
not_to match_against(invalid_record). | ||
and_fail_with(expected_message) | ||
end | ||
|
||
it 'does not match when provided with a model attribute that does not exist' do | ||
invalid_record = new_post | ||
expected_message = 'Expected Post to have configured :invalid_attribute as a ' \ | ||
'ActionText::RichText association but :invalid_attribute does not exist' | ||
|
||
expect { have_rich_text(:invalid_attribute) }. | ||
not_to match_against(invalid_record). | ||
and_fail_with(expected_message) | ||
end | ||
end | ||
|
||
def new_post(has_invalid_content: false, is_rich_text_association: false) | ||
columns = {} | ||
|
||
if has_invalid_content | ||
columns[:invalid_content] = :string | ||
end | ||
|
||
define_model 'Post', columns do | ||
if is_rich_text_association | ||
has_rich_text :content | ||
end | ||
end.new | ||
end | ||
end |