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 ...@@ -12,6 +12,15 @@ RSpec.describe Groups::EmailCampaignsController do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:track) { 'create' } let(:track) { 'create' }
let(:series) { '0' } 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 before do
sign_in(user) sign_in(user)
...@@ -19,71 +28,57 @@ RSpec.describe Groups::EmailCampaignsController do ...@@ -19,71 +28,57 @@ RSpec.describe Groups::EmailCampaignsController do
allow(Gitlab::Tracking).to receive(:self_describing_event) allow(Gitlab::Tracking).to receive(:self_describing_event)
end end
subject(:get_index) do subject do
get group_email_campaigns_url(group, track: track, series: series) get group_email_campaigns_url(group, track: track, series: series)
response response
end end
RSpec::Matchers.define :track_event do |*args| shared_examples 'track and redirect' do
match do it do
expect(Gitlab::Tracking).to have_received(:self_describing_event).with( is_expected.to track_self_describing_event(schema, data)
described_class::EMAIL_CAMPAIGNS_SCHEMA_URL, is_expected.to have_gitlab_http_status(:redirect)
data: {
namespace_id: group.id,
track: track.to_sym,
series: series.to_i,
subject_line: subject_line(track.to_sym, series.to_i)
}
)
end end
end
match_when_negated do shared_examples 'no track and 404' do
expect(Gitlab::Tracking).not_to have_received(:self_describing_event) it do
is_expected.not_to track_self_describing_event
is_expected.to have_gitlab_http_status(:not_found)
end end
end end
context 'track parameter' do describe 'track parameter' do
where(:track, :valid) do context 'when valid' do
'create' | true where(track: Namespaces::InProductMarketingEmailsService::TRACKS.keys)
'verify' | true
'trial' | true with_them do
'team' | true it_behaves_like 'track and redirect'
'xxxx' | false end
nil | false
end end
with_them do context 'when invalid' do
it do where(track: [nil, 'xxxx'])
if valid
is_expected.to track_event with_them do
is_expected.to have_gitlab_http_status(:redirect) it_behaves_like 'no track and 404'
else
is_expected.not_to track_event
is_expected.to have_gitlab_http_status(:not_found)
end
end end
end end
end end
context 'series parameter' do describe 'series parameter' do
where(:series, :valid) do context 'when valid' do
'0' | true where(series: (0..Namespaces::InProductMarketingEmailsService::INTERVAL_DAYS.length - 1).to_a)
'1' | true
'2' | true with_them do
'-1' | false it_behaves_like 'track and redirect'
'3' | false end
nil | false
end end
with_them do context 'when invalid' do
it do where(series: [-1, nil, Namespaces::InProductMarketingEmailsService::INTERVAL_DAYS.length])
if valid
is_expected.to track_event with_them do
is_expected.to have_gitlab_http_status(:redirect) it_behaves_like 'no track and 404'
else
is_expected.not_to track_event
is_expected.to have_gitlab_http_status(:not_found)
end
end end
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