Commit ba9feac9 authored by Alex Buijs's avatar Alex Buijs

Extract RSpec matcher and make test more robust

Introduce shared matcher for Snowplow tracking
parent fb418287
......@@ -12,6 +12,15 @@ RSpec.describe Groups::EmailCampaignsController do
let_it_be(:user) { create(:user) }
let(:track) { 'create' }
let(:series) { '0' }
let(:schema) { described_class::EMAIL_CAMPAIGNS_SCHEMA_URL }
let(:data) do
{
namespace_id: group.id,
track: track.to_sym,
series: series.to_i,
subject_line: subject_line(track.to_sym, series.to_i)
}
end
before do
sign_in(user)
......@@ -19,72 +28,58 @@ RSpec.describe Groups::EmailCampaignsController do
allow(Gitlab::Tracking).to receive(:self_describing_event)
end
subject(:get_index) do
subject do
get group_email_campaigns_url(group, track: track, series: series)
response
end
RSpec::Matchers.define :track_event do |*args|
match do
expect(Gitlab::Tracking).to have_received(:self_describing_event).with(
described_class::EMAIL_CAMPAIGNS_SCHEMA_URL,
data: {
namespace_id: group.id,
track: track.to_sym,
series: series.to_i,
subject_line: subject_line(track.to_sym, series.to_i)
}
)
shared_examples 'track and redirect' do
it do
is_expected.to track_self_describing_event(schema, data)
is_expected.to have_gitlab_http_status(:redirect)
end
end
match_when_negated do
expect(Gitlab::Tracking).not_to have_received(:self_describing_event)
shared_examples 'no track and 404' do
it do
is_expected.not_to track_self_describing_event
is_expected.to have_gitlab_http_status(:not_found)
end
end
context 'track parameter' do
where(:track, :valid) do
'create' | true
'verify' | true
'trial' | true
'team' | true
'xxxx' | false
nil | false
end
describe 'track parameter' do
context 'when valid' do
where(track: Namespaces::InProductMarketingEmailsService::TRACKS.keys)
with_them do
it do
if valid
is_expected.to track_event
is_expected.to have_gitlab_http_status(:redirect)
else
is_expected.not_to track_event
is_expected.to have_gitlab_http_status(:not_found)
it_behaves_like 'track and redirect'
end
end
context 'when invalid' do
where(track: [nil, 'xxxx'])
with_them do
it_behaves_like 'no track and 404'
end
end
context 'series parameter' do
where(:series, :valid) do
'0' | true
'1' | true
'2' | true
'-1' | false
'3' | false
nil | false
end
describe 'series parameter' do
context 'when valid' do
where(series: (0..Namespaces::InProductMarketingEmailsService::INTERVAL_DAYS.length - 1).to_a)
with_them do
it do
if valid
is_expected.to track_event
is_expected.to have_gitlab_http_status(:redirect)
else
is_expected.not_to track_event
is_expected.to have_gitlab_http_status(:not_found)
it_behaves_like 'track and redirect'
end
end
context 'when invalid' do
where(series: [-1, nil, Namespaces::InProductMarketingEmailsService::INTERVAL_DAYS.length])
with_them do
it_behaves_like 'no track and 404'
end
end
end
end
......
# frozen_string_literal: true
RSpec::Matchers.define :track_self_describing_event do |schema, data|
match do
expect(Gitlab::Tracking).to have_received(:self_describing_event)
.with(schema, data: data)
end
match_when_negated do
expect(Gitlab::Tracking).not_to have_received(:self_describing_event)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment