1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
# frozen_string_literal: true
RSpec.shared_examples 'namespace visits tracking worker' do
let_it_be(:base_time) { DateTime.now }
context 'when params are provided' do
before do
worker.perform(entity_type, entity.id, user.id, base_time)
end
include_examples 'an idempotent worker' do
let(:job_args) { [entity_type, entity.id, user.id, base_time] }
it 'tracks the entity visit' do
latest_visit = model.last
expect(model.count).to be(1)
expect(latest_visit[:entity_id]).to be(entity.id)
expect(latest_visit.user_id).to be(user.id)
end
end
context 'when a visit occurs within 15 minutes of a previously tracked one' do
[-15.minutes, 15.minutes].each do |time_diff|
it 'does not track the visit' do
worker.perform(entity_type, entity.id, user.id, base_time + time_diff)
expect(model.count).to be(1)
end
end
end
context 'when a visit occurs more than 15 minutes away from a previously tracked one' do
[-16.minutes, 16.minutes].each do |time_diff|
it 'tracks the visit' do
worker.perform(entity_type, entity.id, user.id, base_time + time_diff)
expect(model.count).to be > 1
end
end
end
end
context 'when user is missing' do
before do
worker.perform(entity_type, entity.id, nil, base_time)
end
it 'does not do anything' do
expect(model.count).to be(0)
end
end
context 'when entity is missing' do
before do
worker.perform(entity_type, nil, user.id, base_time)
end
it 'does not do anything' do
expect(model.count).to be(0)
end
end
end
|