Class: Phlex::Sorbet::RSpec::Matchers::RejectProps

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

Overview

Matcher for validating that a component rejects invalid props

Examples:

expect(MyComponent).to reject_props(user_id: "not an integer")
expect(MyComponent).to reject_props(user_id: "bad").with_error(Phlex::Sorbet::InvalidPropsError)

Instance Method Summary collapse

Constructor Details

#initialize(props) ⇒ RejectProps

Returns a new instance of RejectProps.



207
208
209
210
211
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 207

def initialize(props)
  @props = props
  @expected_error = nil
  @expected_message = nil
end

Instance Method Details

#descriptionObject



254
255
256
257
258
259
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 254

def description
  desc = "reject props #{@props.inspect}"
  desc += " with #{@expected_error}" if @expected_error
  desc += " matching #{@expected_message.inspect}" if @expected_message
  desc
end

#failure_messageObject



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 236

def failure_message
  if @accepted
    "expected #{@actual} to reject props #{@props.inspect}, but it accepted them"
  elsif @expected_error && @raised_error && !@raised_error.is_a?(@expected_error)
    "expected #{@actual} to raise #{@expected_error}, but raised #{@raised_error.class}"
  elsif @expected_message && @raised_error
    "expected error message to match #{@expected_message.inspect}, " \
      "but was #{@raised_error.message.inspect}"
  else
    "unexpected state in reject_props matcher"
  end
end

#failure_message_when_negatedObject



249
250
251
252
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 249

def failure_message_when_negated
  "expected #{@actual} not to reject props #{@props.inspect}, " \
    "but it raised #{@raised_error.class}"
end

#matches?(component_class) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 219

def matches?(component_class)
  @actual = component_class

  begin
    component_class.build_props(**@props)
    @accepted = true
    false
  rescue StandardError => e
    @raised_error = e

    return false if @expected_error && !e.is_a?(@expected_error)
    return false if @expected_message && @raised_error.message !~ @expected_message

    true
  end
end

#with_error(error_class, message_pattern = nil) ⇒ Object



213
214
215
216
217
# File 'lib/phlex/sorbet/rspec/matchers.rb', line 213

def with_error(error_class, message_pattern = nil)
  @expected_error = error_class
  @expected_message = message_pattern
  self
end