inline_metrics_filter_spec.rb 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# frozen_string_literal: true

require 'spec_helper'

describe Banzai::Filter::InlineMetricsFilter do
  include FilterSpecHelper

  let(:input) { %(<a href="#{url}">example</a>) }
  let(:doc) { filter(input) }

  context 'when the document has an external link' do
    let(:url) { 'https://foo.com' }

    it 'leaves regular non-metrics links unchanged' do
15
      expect(doc.to_s).to eq(input)
16 17 18 19 20 21 22 23
    end
  end

  context 'when the document has a metrics dashboard link' do
    let(:params) { ['foo', 'bar', 12] }
    let(:url) { urls.metrics_namespace_project_environment_url(*params) }

    it 'leaves the original link unchanged' do
24
      expect(doc.at_css('a').to_s).to eq(input)
25 26 27 28 29 30 31
    end

    it 'appends a metrics charts placeholder with dashboard url after metrics links' do
      node = doc.at_css('.js-render-metrics')
      expect(node).to be_present

      dashboard_url = urls.metrics_dashboard_namespace_project_environment_url(*params, embedded: true)
32
      expect(node.attribute('data-dashboard-url').to_s).to eq(dashboard_url)
33 34 35 36 37 38 39
    end

    context 'when the metrics dashboard link is part of a paragraph' do
      let(:paragraph) { %(This is an <a href="#{url}">example</a> of metrics.) }
      let(:input) { %(<p>#{paragraph}</p>) }

      it 'appends the charts placeholder after the enclosing paragraph' do
40
        expect(doc.at_css('p').to_s).to include(paragraph)
41 42 43
        expect(doc.at_css('.js-render-metrics')).to be_present
      end
    end
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

    context 'with dashboard params specified' do
      let(:params) do
        [
          'foo',
          'bar',
          12,
          {
            embedded: true,
            dashboard: 'config/prometheus/common_metrics.yml',
            group: 'System metrics (Kubernetes)',
            title: 'Core Usage (Pod Average)',
            y_label: 'Cores per Pod'
          }
        ]
      end

      it 'appends a metrics charts placeholder with dashboard url after metrics links' do
        node = doc.at_css('.js-render-metrics')
        expect(node).to be_present

        dashboard_url = urls.metrics_dashboard_namespace_project_environment_url(*params)
        expect(node.attribute('data-dashboard-url').to_s).to eq(dashboard_url)
      end
    end
69 70
  end
end