summaryrefslogtreecommitdiff
path: root/spec/services/namespaces/in_product_marketing_email_records_spec.rb
blob: d80e20135d5f42d5cf9920db24e06299acdc1630 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Namespaces::InProductMarketingEmailRecords do
  let_it_be(:user) { create :user }

  subject(:records) { described_class.new }

  it 'initializes records' do
    expect(subject.records).to match_array []
  end

  describe '#save!' do
    before do
      allow(Users::InProductMarketingEmail).to receive(:bulk_insert!)

      records.add(user, :team_short, 0)
      records.add(user, :create, 1)
    end

    it 'bulk inserts added records' do
      expect(Users::InProductMarketingEmail).to receive(:bulk_insert!).with(records.records)
      records.save!
    end

    it 'resets its records' do
      records.save!
      expect(records.records).to match_array []
    end
  end

  describe '#add' do
    it 'adds a Users::InProductMarketingEmail record to its records' do
      freeze_time do
        records.add(user, :team_short, 0)
        records.add(user, :create, 1)

        first, second = records.records

        expect(first).to be_a Users::InProductMarketingEmail
        expect(first.track.to_sym).to eq :team_short
        expect(first.series).to eq 0
        expect(first.created_at).to eq Time.zone.now
        expect(first.updated_at).to eq Time.zone.now

        expect(second).to be_a Users::InProductMarketingEmail
        expect(second.track.to_sym).to eq :create
        expect(second.series).to eq 1
        expect(second.created_at).to eq Time.zone.now
        expect(second.updated_at).to eq Time.zone.now
      end
    end
  end
end