Skip to content

Commit 3e1791c

Browse files
committed
Add integration test for Datadog::Tracing::Contrib::ViewComponent
* In order to properly write an integration test for ViewComponent we need to spin up an ad-hoc Rails app that we render the component within, then confirm the span data is generated * The integration test includes the `datadog/tracing/contrib/rails/rails_helper`, which provides a `Rails test application` context * Since there were configuration changes between different versions of ViewComponent, we need to adjust the configuration settings based on which version of the Gem has been loaded * This can be done using `Gem.loaded_specs` and `Gem::Version`, which provide an API for version comparison * Testing across the Appraised versions also checks that the `use_deprecated_instrumentation_name` configuration is applied, and does collect the necessary data
1 parent 714985a commit 3e1791c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'datadog/tracing/contrib/rails/ext'
2+
require 'datadog/tracing/contrib/rails/rails_helper'
3+
require 'datadog/tracing/contrib/view_component/utils'
4+
require 'datadog/tracing/contrib/view_component/integration'
5+
6+
7+
require 'view_component'
8+
9+
RSpec.describe 'ViewComponent integration tests', execute_in_fork: Rails.version.to_i >= 8 do
10+
include_context 'Rails test application'
11+
include ViewComponent::TestHelpers
12+
13+
let(:initialize_block) do
14+
if Gem.loaded_specs["view_component"].version <= Gem::Version.new("3")
15+
require "view_component/engine"
16+
end
17+
18+
proc do
19+
config.view_component.instrumentation_enabled = true
20+
if Gem.loaded_specs["view_component"].version >= Gem::Version.new("4")
21+
config.view_component.previews.controller = "TestController"
22+
else
23+
config.view_component.use_deprecated_instrumentation_name = false
24+
config.view_component.test_controller = "TestController"
25+
end
26+
end
27+
end
28+
29+
let(:component) do
30+
stub_const("TestComponent", Class.new(ViewComponent::Base) do
31+
def call
32+
content_tag(:h1, "Hello")
33+
end
34+
end)
35+
end
36+
37+
let(:controllers) { [controller] }
38+
39+
let(:controller) do
40+
stub_const('TestController', Class.new(ActionController::Base))
41+
end
42+
43+
before do
44+
Datadog.configure do |c|
45+
if Gem.loaded_specs["view_component"].version >= Gem::Version.new("3")
46+
c.tracing.instrument :view_component
47+
else
48+
c.tracing.instrument :view_component, use_deprecated_instrumentation_name: true
49+
end
50+
end
51+
52+
allow(ENV).to receive(:[]).and_call_original
53+
54+
app
55+
end
56+
57+
it "stores instrumentation data when rendering" do
58+
controller.render(component.new)
59+
span = spans.find{|s| s.name == "view_component.render" }
60+
61+
expect(span.name).to eq("view_component.render")
62+
expect(span.resource).to eq("TestComponent")
63+
expect(span.type).to eq(Datadog::Tracing::Metadata::Ext::HTTP::TYPE_TEMPLATE)
64+
65+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT)).to eq("view_component")
66+
expect(span.get_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION)).to eq("render")
67+
68+
expect(span.get_tag("view_component.component_name")).to eq("TestComponent")
69+
expect(span.get_tag("view_component.component_identifier")).to eq("integration_test_spec.rb")
70+
end
71+
end

0 commit comments

Comments
 (0)