Sha256: a5facbb4448685f8a62ff79b2b30176ddfd24b57997bb8354048f9861da6d51a

Contents?: true

Size: 1.16 KB

Versions: 43

Compression:

Stored size: 1.16 KB

Contents

module Coco
  module TestHelpers
    NESTED_TAG_ATTRS = %w[data test_data x]

    def create_class(name, parent)
      klass = Class.new(parent)
      Object.const_set(name, klass)
      klass
    end

    def assert_component_selector(name, text: nil, visible: nil, **)
      assert_selector(component_selector(name, **), text: text, visible: visible)
    end

    def component_selector(name, **opts)
      attr_selector = build_attr_selector(opts.except(:append, :tag))
      "#{opts[:tag]}[data-component='#{name}']#{attr_selector}#{opts[:append]}".strip
    end

    def build_attr_selector(attrs)
      tag_attrs = attrs.except(*NESTED_TAG_ATTRS)
      NESTED_TAG_ATTRS.each do |key|
        subattrs = attrs.fetch(key, {})
        tag_attrs.merge!(prefix_keys(subattrs, key))
      end
      attrs.reduce("") do |str, (key, value)|
        if value.is_a?(Hash)
          value.each do |k, v|
            str += "[#{key}-#{k}='#{v}']"
          end
        else
          str += "[#{key}='#{value}']"
        end
        str
      end
    end

    def prefix_keys(attrs, prefix)
      attrs.transform_keys! { [prefix, _1].map(&:to_s).join("-").tr("_", "-") }
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
coveragebook_components-0.19.9 lib/coco/test_helpers.rb
coveragebook_components-0.19.8 lib/coco/test_helpers.rb
coveragebook_components-0.19.7 lib/coco/test_helpers.rb
coveragebook_components-0.19.6 lib/coco/test_helpers.rb
coveragebook_components-0.19.5 lib/coco/test_helpers.rb
coveragebook_components-0.19.4 lib/coco/test_helpers.rb
coveragebook_components-0.19.3 lib/coco/test_helpers.rb
coveragebook_components-0.19.2 lib/coco/test_helpers.rb
coveragebook_components-0.19.1 lib/coco/test_helpers.rb
coveragebook_components-0.19.0 lib/coco/test_helpers.rb
coveragebook_components-0.18.8 lib/coco/test_helpers.rb
coveragebook_components-0.18.7 lib/coco/test_helpers.rb
coveragebook_components-0.18.0 lib/coco/test_helpers.rb
coveragebook_components-0.17.7 lib/coco/test_helpers.rb
coveragebook_components-0.17.6 lib/coco/test_helpers.rb
coveragebook_components-0.17.5 lib/coco/test_helpers.rb
coveragebook_components-0.17.4 lib/coco/test_helpers.rb
coveragebook_components-0.17.3 lib/coco/test_helpers.rb
coveragebook_components-0.17.2 lib/coco/test_helpers.rb
coveragebook_components-0.17.1 lib/coco/test_helpers.rb