summaryrefslogtreecommitdiff
path: root/spec/helpers/whats_new_helper_spec.rb
blob: 1c8684de75c8a1076e2c06799fadb94dd4ded51d (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
56
57
58
59
60
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WhatsNewHelper do
  let(:fixture_dir_glob) { Dir.glob(File.join('spec', 'fixtures', 'whats_new', '*.yml')) }

  describe '#whats_new_storage_key' do
    subject { helper.whats_new_storage_key }

    context 'when version exist' do
      before do
        allow(Dir).to receive(:glob).with(Rails.root.join('data', 'whats_new', '*.yml')).and_return(fixture_dir_glob)
      end

      it { is_expected.to eq('display-whats-new-notification-01.05') }
    end

    context 'when recent release items do NOT exist' do
      before do
        allow(helper).to receive(:whats_new_release_items).and_return(nil)
      end

      it { is_expected.to be_nil }
    end
  end

  describe '#whats_new_most_recent_release_items_count' do
    subject { helper.whats_new_most_recent_release_items_count }

    context 'when recent release items exist' do
      it 'returns the count from the most recent file' do
        expect(Dir).to receive(:glob).with(Rails.root.join('data', 'whats_new', '*.yml')).and_return(fixture_dir_glob)

        expect(subject).to eq(1)
      end
    end

    context 'when recent release items do NOT exist' do
      before do
        allow(YAML).to receive(:safe_load).and_raise

        expect(Gitlab::ErrorTracking).to receive(:track_exception)
      end

      it 'fails gracefully and logs an error' do
        expect(subject).to be_nil
      end
    end
  end

  # Testing this important private method here because the request spec required multiple confusing mocks and felt wrong and overcomplicated
  describe '#whats_new_items_cache_key' do
    it 'returns a key containing the most recent file name and page parameter' do
      allow(Dir).to receive(:glob).with(Rails.root.join('data', 'whats_new', '*.yml')).and_return(fixture_dir_glob)

      expect(helper.send(:whats_new_items_cache_key, 2)).to eq('whats_new:release_items:file-20201225_01_05:page-2')
    end
  end
end