Class: Phlex::Sorbet::RSpec::Matchers::HaveProps

Inherits:
Object
  • Object
show all
Defined in:
lib/phlex/sorbet/rspec/matchers.rb

Overview

Matcher for validating multiple props at once

Examples:

expect(MyComponent).to have_props(user_id: Integer, name: String)
expect(MyComponent).to have_props(:user_id, Integer).and_prop(:name, String)

Instance Method Summary collapse

Constructor Details

#initialize(args_hash_or_field = nil, expected_type = nil) ⇒ HaveProps

Returns a new instance of HaveProps.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 120

def initialize(args_hash_or_field = nil, expected_type = nil)
  @props_to_check = []

  if args_hash_or_field.is_a?(Hash)
    args_hash_or_field.each do |field_name, type|
      @props_to_check << [field_name, type]
    end
  elsif args_hash_or_field.is_a?(Symbol)
    @props_to_check << [args_hash_or_field, expected_type]
  end
end

Instance Method Details

#and_prop(field_name, expected_type) ⇒ Object



132
133
134
135
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 132

def and_prop(field_name, expected_type)
  @props_to_check << [field_name, expected_type]
  self
end

#descriptionObject



159
160
161
162
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 159

def description
  desc = @props_to_check.map { |name, type| "#{name}: #{type}" }.join(", ")
  "have props: #{desc}"
end

#failure_messageObject



151
152
153
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 151

def failure_message
  @failure_message || "expected #{@actual} to have the specified props"
end

#failure_message_when_negatedObject



155
156
157
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 155

def failure_message_when_negated
  "expected #{@actual}::Props not to have the specified props"
end

#matches?(component_class) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 137

def matches?(component_class)
  @actual = component_class

  @props_to_check.each do |field_name, expected_type|
    matcher = HaveProp.new(field_name, expected_type)
    unless matcher.matches?(component_class)
      @failure_message = matcher.failure_message
      return false
    end
  end

  true
end