2020-09-03 14:08:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SnowplowHelpers
|
|
|
|
# Asserts call for one snowplow event from `Gitlab::Tracking#event`.
|
|
|
|
#
|
|
|
|
# @param [Hash]
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# describe '#show', :snowplow do
|
|
|
|
# it 'tracks snowplow events' do
|
|
|
|
# get :show
|
|
|
|
#
|
|
|
|
# expect_snowplow_event(category: 'Experiment', action: 'start')
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# describe '#create', :snowplow do
|
|
|
|
# it 'tracks snowplow events' do
|
|
|
|
# post :create
|
|
|
|
#
|
|
|
|
# expect_snowplow_event(
|
|
|
|
# category: 'Experiment',
|
|
|
|
# action: 'created',
|
|
|
|
# )
|
|
|
|
# expect_snowplow_event(
|
|
|
|
# category: 'Experiment',
|
|
|
|
# action: 'accepted',
|
|
|
|
# property: 'property',
|
|
|
|
# label: 'label'
|
|
|
|
# )
|
|
|
|
# end
|
|
|
|
# end
|
2020-12-10 19:09:41 -05:00
|
|
|
#
|
|
|
|
# Passing context:
|
|
|
|
#
|
|
|
|
# Simply provide a hash that has the schema and data expected.
|
|
|
|
#
|
|
|
|
# expect_snowplow_event(
|
|
|
|
# category: 'Experiment',
|
|
|
|
# action: 'created',
|
|
|
|
# context: [
|
|
|
|
# {
|
|
|
|
# schema: 'iglu:com.gitlab/.../0-3-0',
|
|
|
|
# data: { key: 'value' }
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# )
|
2021-02-11 16:09:00 -05:00
|
|
|
def expect_snowplow_event(category:, action:, context: nil, **kwargs)
|
2020-12-10 19:09:41 -05:00
|
|
|
if context
|
|
|
|
kwargs[:context] = []
|
|
|
|
context.each do |c|
|
|
|
|
expect(SnowplowTracker::SelfDescribingJson).to have_received(:new)
|
|
|
|
.with(c[:schema], c[:data]).at_least(:once)
|
|
|
|
kwargs[:context] << an_instance_of(SnowplowTracker::SelfDescribingJson)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-28 14:08:52 -04:00
|
|
|
expect(Gitlab::Tracking).to have_received(:event) # rubocop:disable RSpec/ExpectGitlabTracking
|
|
|
|
.with(category, action, **kwargs).at_least(:once)
|
2020-09-03 14:08:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Asserts that no call to `Gitlab::Tracking#event` was made.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# describe '#show', :snowplow do
|
|
|
|
# it 'does not track any snowplow events' do
|
|
|
|
# get :show
|
|
|
|
#
|
|
|
|
# expect_no_snowplow_event
|
|
|
|
# end
|
|
|
|
# end
|
2021-05-05 05:10:02 -04:00
|
|
|
def expect_no_snowplow_event(category: nil, action: nil, **kwargs)
|
|
|
|
if category && action
|
|
|
|
expect(Gitlab::Tracking).not_to have_received(:event).with(category, action, **kwargs) # rubocop:disable RSpec/ExpectGitlabTracking
|
|
|
|
else
|
|
|
|
expect(Gitlab::Tracking).not_to have_received(:event) # rubocop:disable RSpec/ExpectGitlabTracking
|
|
|
|
end
|
2020-09-03 14:08:29 -04:00
|
|
|
end
|
|
|
|
end
|