broadcast_message_spec.rb 4.92 KB
Newer Older
1 2
require 'spec_helper'

3
describe BroadcastMessage do
4
  subject { build(:broadcast_message) }
5

6
  it { is_expected.to be_valid }
7

Robert Speicher's avatar
Robert Speicher committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  describe 'validations' do
    let(:triplet) { '#000' }
    let(:hex)     { '#AABBCC' }

    it { is_expected.to allow_value(nil).for(:color) }
    it { is_expected.to allow_value(triplet).for(:color) }
    it { is_expected.to allow_value(hex).for(:color) }
    it { is_expected.not_to allow_value('000').for(:color) }

    it { is_expected.to allow_value(nil).for(:font) }
    it { is_expected.to allow_value(triplet).for(:font) }
    it { is_expected.to allow_value(hex).for(:font) }
    it { is_expected.not_to allow_value('000').for(:font) }
  end

23
  describe '.current', :use_clean_rails_memory_store_caching do
24
    it 'returns message if time match' do
25 26
      message = create(:broadcast_message)

27
      expect(described_class.current).to include(message)
28 29
    end

30 31 32 33
    it 'returns multiple messages if time match' do
      message1 = create(:broadcast_message)
      message2 = create(:broadcast_message)

34
      expect(described_class.current).to contain_exactly(message1, message2)
35 36 37
    end

    it 'returns empty list if time not come' do
38 39
      create(:broadcast_message, :future)

40
      expect(described_class.current).to be_empty
41 42
    end

43
    it 'returns empty list if time has passed' do
44 45
      create(:broadcast_message, :expired)

46
      expect(described_class.current).to be_empty
47
    end
48 49 50 51 52 53

    it 'caches the output of the query' do
      create(:broadcast_message)

      expect(described_class).to receive(:where).and_call_original.once

54 55 56 57 58
      described_class.current

      Timecop.travel(1.year) do
        described_class.current
      end
59
    end
60

61 62 63 64 65 66
    it 'does not create new records' do
      create(:broadcast_message)

      expect { described_class.current }.not_to change { described_class.count }
    end

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    it 'includes messages that need to be displayed in the future' do
      create(:broadcast_message)

      future = create(
        :broadcast_message,
        starts_at: Time.now + 10.minutes,
        ends_at: Time.now + 20.minutes
      )

      expect(described_class.current.length).to eq(1)

      Timecop.travel(future.starts_at) do
        expect(described_class.current.length).to eq(2)
      end
    end

    it 'does not clear the cache if only a future message should be displayed' do
      create(:broadcast_message, :future)

86
      expect(Rails.cache).not_to receive(:delete).with(described_class::CACHE_KEY)
87 88
      expect(described_class.current.length).to eq(0)
    end
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    it 'clears the legacy cache key' do
      create(:broadcast_message, :future)

      expect(Rails.cache).to receive(:delete).with(described_class::LEGACY_CACHE_KEY)
      expect(described_class.current.length).to eq(0)
    end

    it 'gracefully handles bad cache entry' do
      allow(described_class).to receive(:current_and_future_messages).and_return('{')

      expect(described_class.current).to be_empty
    end

    it 'gracefully handles an empty hash' do
      allow(described_class).to receive(:current_and_future_messages).and_return('{}')

      expect(described_class.current).to be_empty
    end

    it 'gracefully handles unknown attributes' do
      message = create(:broadcast_message)

      allow(described_class).to receive(:current_and_future_messages)
                                 .and_return([{ bad_attr: 1 }, message])

      expect(described_class.current).to eq([message])
    end
117
  end
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  describe '#active?' do
    it 'is truthy when started and not ended' do
      message = build(:broadcast_message)

      expect(message).to be_active
    end

    it 'is falsey when ended' do
      message = build(:broadcast_message, :expired)

      expect(message).not_to be_active
    end

    it 'is falsey when not started' do
      message = build(:broadcast_message, :future)

      expect(message).not_to be_active
    end
  end

  describe '#started?' do
    it 'is truthy when starts_at has passed' do
      message = build(:broadcast_message)

      travel_to(3.days.from_now) do
        expect(message).to be_started
      end
    end

    it 'is falsey when starts_at is in the future' do
      message = build(:broadcast_message)

      travel_to(3.days.ago) do
        expect(message).not_to be_started
      end
    end
  end

  describe '#ended?' do
    it 'is truthy when ends_at has passed' do
      message = build(:broadcast_message)

      travel_to(3.days.from_now) do
        expect(message).to be_ended
      end
    end

    it 'is falsey when ends_at is in the future' do
      message = build(:broadcast_message)

      travel_to(3.days.ago) do
        expect(message).not_to be_ended
      end
    end
  end
174 175 176 177 178 179

  describe '#flush_redis_cache' do
    it 'flushes the Redis cache' do
      message = create(:broadcast_message)

      expect(Rails.cache).to receive(:delete).with(described_class::CACHE_KEY)
180
      expect(Rails.cache).to receive(:delete).with(described_class::LEGACY_CACHE_KEY)
181 182 183 184

      message.flush_redis_cache
    end
  end
185
end