Initial commit
This commit is contained in:
54
spec/support/matchers/allow_content_type.rb
Normal file
54
spec/support/matchers/allow_content_type.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
require "rspec/expectations"
|
||||
RSpec::Matchers.define :allow_content_type do |*content_types|
|
||||
match do |record|
|
||||
matcher.matches?(record, content_types)
|
||||
end
|
||||
|
||||
chain :for do |attr_name|
|
||||
matcher.for(attr_name)
|
||||
end
|
||||
|
||||
chain :with_message do |message|
|
||||
matcher.with_message(message)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def matcher
|
||||
@matcher ||= AllowContentTypeMatcher.new
|
||||
end
|
||||
|
||||
class AllowContentTypeMatcher
|
||||
def for(attr_name)
|
||||
@attr_name = attr_name
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@message = message
|
||||
end
|
||||
|
||||
def matches?(record, content_types)
|
||||
Array.wrap(content_types).all? do |content_type|
|
||||
record.send(attr_name).attach attachment_for(content_type)
|
||||
record.valid?
|
||||
!record.errors[attr_name].include? message
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :attr_name
|
||||
|
||||
def attachment_for(content_type)
|
||||
suffix = content_type.to_s.split("/").last
|
||||
|
||||
{ io: StringIO.new("Hello world!"), filename: "test.#{suffix}", content_type: content_type }
|
||||
end
|
||||
|
||||
def message
|
||||
@message || I18n.translate("activerecord.errors.messages.content_type")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
RSpec::Matchers.alias_matcher :allow_content_types, :allow_content_type
|
||||
8
spec/support/matchers/have_invalid_field.rb
Normal file
8
spec/support/matchers/have_invalid_field.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
require "rspec/expectations"
|
||||
|
||||
RSpec::Matchers.define :have_invalid_field do |field_name|
|
||||
match do |page|
|
||||
page.has_field?(name, class: "is-invalid") ||
|
||||
page.find_field(field_name).native.attribute("validationMessage").present?
|
||||
end
|
||||
end
|
||||
45
spec/support/matchers/validate_attachment_of.rb
Normal file
45
spec/support/matchers/validate_attachment_of.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
require "rspec/expectations"
|
||||
|
||||
RSpec::Matchers.define :validate_attachment_of do |attr_name|
|
||||
match do |record|
|
||||
matcher.matches?(record, attr_name)
|
||||
end
|
||||
|
||||
chain :on do |validation_context|
|
||||
matcher.on(validation_context)
|
||||
end
|
||||
|
||||
chain :with_message do |message|
|
||||
matcher.with_message(message)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def matcher
|
||||
@matcher ||= ValidateAttachmentOfMatcher.new
|
||||
end
|
||||
|
||||
class ValidateAttachmentOfMatcher
|
||||
def on(validation_context)
|
||||
@validation_context = validation_context
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@message = message
|
||||
end
|
||||
|
||||
def matches?(record, attr_name)
|
||||
record.send(attr_name).purge
|
||||
record.valid?(validation_context)
|
||||
record.errors[attr_name].include? message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :validation_context
|
||||
|
||||
def message
|
||||
@message || I18n.translate("activerecord.errors.messages.attached")
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user