class RSpec::Rails::Matchers::CableReady::MutatedElement

@private

Public Class Methods

new(target) click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 9
def initialize(target)
  @target = target
  @channel_not_found = false
  @mutation_not_found = false
  @element_not_found = false
end

Public Instance Methods

failure_message() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 58
def failure_message
  if @channel_not_found
    "#{base_failure_message} but no broadcasted messages were found"
  elsif @mutation_not_found
    "#{base_failure_message} but no broadcasted messages with this mutation were found"
  elsif @element_not_found
    "#{base_failure_message} but message for given element was not found"
  else
    "#{base_failure_message} with #{@data} but mutated element with #{@message_data.except('selector')}"
  end
end
matches?(proc) click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 30
def matches?(proc)
  verify_channel_name_presence
  verify_action_presence

  proc.call

  if (options = mutation_options)
    @message_data = mutated_element_options(options)

    if @message_data.present?
      @message_data.except('selector') == @data
    else
      @element_not_found = true
      false
    end
  elsif broadcasted_message.blank?
    @channel_not_found = true
    false
  else
    @mutation_not_found = true
    false
  end
end
on_channel(channel_name) click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 25
def on_channel(channel_name)
  @channel_name = channel_name
  self
end
supports_block_expectations?() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 54
def supports_block_expectations?
  true
end
with(action, data = {}, &block) click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 16
def with(action, data = {}, &block)
  @data = data
  @action = action.to_s.camelize(:lower)
  @data = @data.with_indifferent_access if @data.is_a?(Hash)
  @data = @data.transform_keys { |key| key.camelize(:lower) }
  @block = block if block_given?
  self
end

Private Instance Methods

base_failure_message() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 100
def base_failure_message
  "expected to mutate element `#{@target}` on channel #{@channel_name}"
end
broadcasted_message() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 78
def broadcasted_message
  message = pubsub_adapter.broadcasts(@channel_name).first
  return unless message

  decoded = ActiveSupport::JSON.decode(message)
  decoded.with_indifferent_access if decoded.is_a?(Hash)
end
mutated_element_options(values) click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 108
def mutated_element_options(values)
  values.flatten.find { |el| el['selector'] == @target }
end
mutation_options() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 72
def mutation_options
  return unless broadcasted_message

  broadcasted_message.dig('operations', @action)
end
pubsub_adapter() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 104
def pubsub_adapter
  ::ActionCable.server.pubsub
end
verify_action_presence() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 86
def verify_action_presence
  return if @action.present?

  message = 'Please specify the type of element mutation using .with(type_of_mutation, options)'
  raise ArgumentError, message
end
verify_channel_name_presence() click to toggle source
# File lib/rspec/rails/matchers/cable_ready/mutated_element.rb, line 93
def verify_channel_name_presence
  return if @channel_name.present?

  message = 'Please specify the channel using .on_channel'
  raise ArgumentError, message
end