summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/graphql/deprecation_spec.rb
blob: c9b47219198504051c7058bd96797bee7f0c7acf (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
117
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true

require 'fast_spec_helper'
require 'active_model'

RSpec.describe ::Gitlab::Graphql::Deprecation do
  let(:options) { {} }

  subject(:deprecation) { described_class.new(**options) }

  describe '.parse' do
    subject(:parsed_deprecation) { described_class.parse(**options) }

    context 'with no arguments' do
      it 'returns nil' do
        expect(parsed_deprecation).to be_nil
      end
    end

    context 'with an incomplete `deprecated` argument' do
      let(:options) { { deprecated: {} } }

      it 'parses as an invalid deprecation' do
        expect(parsed_deprecation).not_to be_valid
        expect(parsed_deprecation).to eq(described_class.new)
      end
    end

    context 'with a `deprecated` argument' do
      let(:options) { { deprecated: { reason: :renamed, milestone: '10.10' } } }

      it 'parses as a deprecation' do
        expect(parsed_deprecation).to be_valid
        expect(parsed_deprecation).to eq(
          described_class.new(reason: 'This was renamed', milestone: '10.10')
        )
      end
    end

    context 'with an `alpha` argument' do
      let(:options) { { alpha: { milestone: '10.10' } } }

      it 'parses as an alpha' do
        expect(parsed_deprecation).to be_valid
        expect(parsed_deprecation).to eq(
          described_class.new(reason: :alpha, milestone: '10.10')
        )
      end
    end

    context 'with both `deprecated` and `alpha` arguments' do
      let(:options) do
        { alpha: { milestone: '10.10' }, deprecated: { reason: :renamed, milestone: '10.10' } }
      end

      it 'raises an error' do
        expect { parsed_deprecation }.to raise_error(ArgumentError,
          '`alpha` and `deprecated` arguments cannot be passed at the same time'
        )
      end
    end
  end

  describe 'validations' do
    let(:options) { { reason: :renamed, milestone: '10.10' } }

    it { is_expected.to be_valid }

    context 'when the milestone is absent' do
      before do
        options.delete(:milestone)
      end

      it { is_expected.not_to be_valid }
    end

    context 'when the milestone is not milestone-ish' do
      before do
        options[:milestone] = 'next year'
      end

      it { is_expected.not_to be_valid }
    end

    context 'when the milestone is not a string' do
      before do
        options[:milestone] = 10.01
      end

      it { is_expected.not_to be_valid }
    end

    context 'when the reason is absent' do
      before do
        options.delete(:reason)
      end

      it { is_expected.not_to be_valid }
    end

    context 'when the reason is not a known reason' do
      before do
        options[:reason] = :not_stylish_enough
      end

      it { is_expected.not_to be_valid }
    end

    context 'when the reason is a string' do
      before do
        options[:reason] = 'not stylish enough'
      end

      it { is_expected.to be_valid }
    end

    context 'when the reason is a string ending with a period' do
      before do
        options[:reason] = 'not stylish enough.'
      end

      it { is_expected.not_to be_valid }
    end
  end

  describe '#deprecation_reason' do
    context 'when there is a replacement' do
      let(:options) { { reason: :renamed, milestone: '10.10', replacement: 'X.y' } }

      it 'renders as reason-replacement-milestone' do
        expect(deprecation.deprecation_reason).to eq('This was renamed. Please use `X.y`. Deprecated in 10.10.')
      end
    end

    context 'when there is no replacement' do
      let(:options) { { reason: :renamed, milestone: '10.10' } }

      it 'renders as reason-milestone' do
        expect(deprecation.deprecation_reason).to eq('This was renamed. Deprecated in 10.10.')
      end
    end

    describe 'processing of reason' do
      described_class::REASONS.each_key do |known_reason|
        context "when the reason is a known reason such as #{known_reason.inspect}" do
          let(:options) { { reason: known_reason } }

          it 'renders the reason_text correctly' do
            expect(deprecation.deprecation_reason).to start_with(described_class::REASONS[known_reason])
          end
        end
      end

      context 'when the reason is any other string' do
        let(:options) { { reason: 'unhelpful' } }

        it 'appends a period' do
          expect(deprecation.deprecation_reason).to start_with('unhelpful.')
        end
      end
    end
  end

  describe '#edit_description' do
    let(:options) { { reason: :renamed, milestone: '10.10' } }

    it 'appends milestone:reason with a leading space if there is a description' do
      desc = deprecation.edit_description('Some description.')

      expect(desc).to eq('Some description. Deprecated in 10.10: This was renamed.')
    end

    it 'returns nil if there is no description' do
      desc = deprecation.edit_description(nil)

      expect(desc).to be_nil
    end
  end

  describe '#original_description' do
    it 'records the description passed to it' do
      deprecation.edit_description('Some description.')

      expect(deprecation.original_description).to eq('Some description.')
    end
  end

  describe '#markdown' do
    context 'when there is a replacement' do
      let(:options) { { reason: :renamed, milestone: '10.10', replacement: 'X.y' } }

      context 'when the context is :inline' do
        it 'renders on one line' do
          expectation = '**Deprecated** in 10.10. This was renamed. Use: [`X.y`](#xy).'

          expect(deprecation.markdown).to eq(expectation)
          expect(deprecation.markdown(context: :inline)).to eq(expectation)
        end
      end

      context 'when the context is :block' do
        it 'renders a warning note' do
          expectation = <<~MD.chomp
            WARNING:
            **Deprecated** in 10.10.
            This was renamed.
            Use: [`X.y`](#xy).
          MD

          expect(deprecation.markdown(context: :block)).to eq(expectation)
        end
      end
    end

    context 'when there is no replacement' do
      let(:options) { { reason: 'Removed', milestone: '10.10' } }

      context 'when the context is :inline' do
        it 'renders on one line' do
          expectation = '**Deprecated** in 10.10. Removed.'

          expect(deprecation.markdown).to eq(expectation)
          expect(deprecation.markdown(context: :inline)).to eq(expectation)
        end
      end

      context 'when the context is :block' do
        it 'renders a warning note' do
          expectation = <<~MD.chomp
            WARNING:
            **Deprecated** in 10.10.
            Removed.
          MD

          expect(deprecation.markdown(context: :block)).to eq(expectation)
        end
      end
    end
  end

  describe '#alpha?' do
    let(:options) { { milestone: '10.10', reason: reason } }

    context 'when `reason` is `:alpha`' do
      let(:reason) { described_class::REASON_ALPHA }

      it { is_expected.to be_alpha }
    end

    context 'when `reason` is not `:alpha`' do
      let(:reason) { described_class::REASON_RENAMED }

      it { is_expected.not_to be_alpha }
    end
  end
end