Sha256: 099ec56228a79efe4252d68ae4218c7609f7589bb9961c886ca6374a26aca98f

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require "active_support"
require "active_support/test_case"

class StimulusReflex::TestCase < ActiveSupport::TestCase
  class TestChannel < ActionCable::Channel::TestCase
    delegate :env, to: :connection

    def initialize(connection_opts = {})
      super("StimulusReflex::Channel")
      @connection = stub_connection(connection_opts.merge(env: {}))
    end

    def stream_name
      ids = connection.identifiers.map { |identifier| connection.send(identifier).try(:id) || connection.send(identifier) }
      [
        "StimulusReflex::Channel",
        ids.select(&:present?).join(";")
      ].select(&:present?).join(":")
    end
  end

  module Behavior
    extend ActiveSupport::Concern

    module ClassMethods
      def reflex_class
      end
    end

    def build_reflex(opts = {})
      channel = opts.fetch(:channel, TestChannel.new(opts.fetch(:connection, {})))
      element = opts.fetch(:element, StimulusReflex::Element.new)

      self.class.reflex_class.new(
        channel, element: element, url: opts.fetch(:url, ""), method_name: opts.dig(:method_name), params: opts.fetch(:params, {})
      )
    end
  end

  include Behavior
rescue NameError => e
  if e.missing_name == "ActionCable::Channel::TestCase"
    raise "Please install action-cable-testing https://github.com/palkan/action-cable-testing"
  else
    raise
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stimulus_reflex_testing-0.3.0 lib/stimulus_reflex/test_case.rb
stimulus_reflex_testing-0.2.2 lib/stimulus_reflex/test_case.rb