summaryrefslogtreecommitdiff
path: root/spec/tooling/lib/tooling/view_to_js_mappings_spec.rb
blob: b09df2a920077062af1fc3a96247c2fe8e6babf8 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# frozen_string_literal: true

require 'tempfile'
require_relative '../../../../tooling/lib/tooling/view_to_js_mappings'

RSpec.describe Tooling::ViewToJsMappings, feature_category: :tooling do
  # We set temporary folders, and those readers give access to those folder paths
  attr_accessor :view_base_folder, :js_base_folder

  around do |example|
    Dir.mktmpdir do |tmp_js_base_folder|
      Dir.mktmpdir do |tmp_views_base_folder|
        self.js_base_folder   = tmp_js_base_folder
        self.view_base_folder = tmp_views_base_folder

        example.run
      end
    end
  end

  describe '#execute' do
    let(:instance) do
      described_class.new(
        view_base_folder: view_base_folder,
        js_base_folder: js_base_folder
      )
    end

    let(:changed_files) { %W[#{view_base_folder}/index.html] }

    subject { instance.execute(changed_files) }

    context 'when no view files have been changed' do
      before do
        allow(instance).to receive(:view_files).and_return([])
      end

      it 'returns nothing' do
        expect(subject).to match_array([])
      end
    end

    context 'when some view files have been changed' do
      before do
        File.write("#{view_base_folder}/index.html", index_html_content)
      end

      context 'when they do not contain the HTML attribute value we search for' do
        let(:index_html_content) do
          <<~FILE
            Beginning of file
            End of file
          FILE
        end

        it 'returns nothing' do
          expect(subject).to match_array([])
        end
      end

      context 'when they contain the HTML attribute value we search for' do
        let(:index_html_content) do
          <<~FILE
            Beginning of file

            <a id="js-some-id">A link</a>

            End of file
          FILE
        end

        context 'when no matching JS files are found' do
          it 'returns nothing' do
            expect(subject).to match_array([])
          end
        end

        context 'when some matching JS files are found' do
          let(:index_js_content) do
            <<~FILE
              Beginning of file

              const isMainAwardsBlock = votesBlock.closest('#js-some-id.some_class').length;

              End of file
            FILE
          end

          before do
            File.write("#{js_base_folder}/index.js", index_js_content)
          end

          it 'returns the matching JS files' do
            expect(subject).to match_array(["#{js_base_folder}/index.js"])
          end
        end
      end
    end

    context 'when rails partials are included in the file' do
      before do
        File.write("#{view_base_folder}/index.html", index_html_content)
        File.write("#{view_base_folder}/_my-partial.html.haml", partial_file_content)
        File.write("#{js_base_folder}/index.js", index_js_content)
      end

      let(:index_html_content) do
        <<~FILE
          Beginning of file

          = render 'my-partial'

          End of file
        FILE
      end

      let(:partial_file_content) do
        <<~FILE
          Beginning of file

          <a class="js-some-class">A link with class</a>

          End of file
        FILE
      end

      let(:index_js_content) do
        <<~FILE
          Beginning of file

          const isMainAwardsBlock = votesBlock.closest('.js-some-class').length;

          End of file
        FILE
      end

      it 'scans those partials for the HTML attribute value' do
        expect(subject).to match_array(["#{js_base_folder}/index.js"])
      end
    end
  end

  describe '#view_files' do
    subject { described_class.new(view_base_folder: view_base_folder).view_files(changed_files) }

    before do
      File.write("#{js_base_folder}/index.js", "index.js")
      File.write("#{view_base_folder}/index.html", "index.html")
    end

    context 'when no files were changed' do
      let(:changed_files) { [] }

      it 'returns an empty array' do
        expect(subject).to match_array([])
      end
    end

    context 'when no view files were changed' do
      let(:changed_files) { ["#{js_base_folder}/index.js"] }

      it 'returns an empty array' do
        expect(subject).to match_array([])
      end
    end

    context 'when view files were changed' do
      let(:changed_files) { ["#{js_base_folder}/index.js", "#{view_base_folder}/index.html"] }

      it 'returns the path to the view files' do
        expect(subject).to match_array(["#{view_base_folder}/index.html"])
      end
    end

    context 'when view files are deleted' do
      let(:changed_files) { ["#{js_base_folder}/index.js", "#{view_base_folder}/deleted.html"] }

      it 'returns an empty array' do
        expect(subject).to match_array([])
      end
    end
  end

  describe '#folders_for_available_editions' do
    let(:base_folder_path) { 'app/views' }

    subject { described_class.new.folders_for_available_editions(base_folder_path) }

    context 'when FOSS' do
      before do
        allow(GitlabEdition).to receive(:ee?).and_return(false)
        allow(GitlabEdition).to receive(:jh?).and_return(false)
      end

      it 'returns the correct paths' do
        expect(subject).to match_array([base_folder_path])
      end
    end

    context 'when EE' do
      before do
        allow(GitlabEdition).to receive(:ee?).and_return(true)
        allow(GitlabEdition).to receive(:jh?).and_return(false)
      end

      it 'returns the correct paths' do
        expect(subject).to eq([base_folder_path, "ee/#{base_folder_path}"])
      end
    end

    context 'when JiHu' do
      before do
        allow(GitlabEdition).to receive(:ee?).and_return(true)
        allow(GitlabEdition).to receive(:jh?).and_return(true)
      end

      it 'returns the correct paths' do
        expect(subject).to eq([base_folder_path, "ee/#{base_folder_path}", "jh/#{base_folder_path}"])
      end
    end
  end

  describe '#find_partials' do
    subject { described_class.new(view_base_folder: view_base_folder).find_partials(file_path) }

    let(:file_path) { "#{view_base_folder}/my_html_file.html" }

    before do
      File.write(file_path, file_content)
    end

    context 'when the file includes a partial' do
      context 'when the partial is in the same folder as the view file' do
        before do
          File.write("#{view_base_folder}/_my-partial.html.haml", 'Hello from partial')
        end

        let(:file_content) do
          <<~FILE
            Beginning of file

            = render "my-partial"

            End of file
          FILE
        end

        it "returns the partial file path" do
          expect(subject).to match_array(["#{view_base_folder}/_my-partial.html.haml"])
        end
      end

      context 'when the partial is in a subfolder' do
        before do
          FileUtils.mkdir_p("#{view_base_folder}/subfolder")

          (1..12).each do |i|
            FileUtils.touch "#{view_base_folder}/subfolder/_my-partial#{i}.html.haml"
          end
        end

        let(:file_content) do
          <<~FILE
            Beginning of file

            = render("subfolder/my-partial1")
            = render "subfolder/my-partial2"
            = render(partial: "subfolder/my-partial3")
            = render partial: "subfolder/my-partial4"
            = render(partial:"subfolder/my-partial5", path: 'else')
            = render partial:"subfolder/my-partial6"
            = render_if_exist("subfolder/my-partial7", path: 'else')
            = render_if_exist "subfolder/my-partial8"
            = render_if_exist(partial: "subfolder/my-partial9", path: 'else')
            = render_if_exist partial: "subfolder/my-partial10"
            = render_if_exist(partial:"subfolder/my-partial11", path: 'else')
            = render_if_exist partial:"subfolder/my-partial12"

            End of file
          FILE
        end

        it "returns the partials file path" do
          expect(subject).to match_array([
            "#{view_base_folder}/subfolder/_my-partial1.html.haml",
            "#{view_base_folder}/subfolder/_my-partial2.html.haml",
            "#{view_base_folder}/subfolder/_my-partial3.html.haml",
            "#{view_base_folder}/subfolder/_my-partial4.html.haml",
            "#{view_base_folder}/subfolder/_my-partial5.html.haml",
            "#{view_base_folder}/subfolder/_my-partial6.html.haml",
            "#{view_base_folder}/subfolder/_my-partial7.html.haml",
            "#{view_base_folder}/subfolder/_my-partial8.html.haml",
            "#{view_base_folder}/subfolder/_my-partial9.html.haml",
            "#{view_base_folder}/subfolder/_my-partial10.html.haml",
            "#{view_base_folder}/subfolder/_my-partial11.html.haml",
            "#{view_base_folder}/subfolder/_my-partial12.html.haml"
          ])
        end
      end

      context 'when the file does not include a partial' do
        let(:file_content) do
          <<~FILE
            Beginning of file
            End of file
          FILE
        end

        it 'returns an empty array' do
          expect(subject).to match_array([])
        end
      end
    end
  end

  describe '#find_pattern_in_file' do
    let(:subject) { described_class.new.find_pattern_in_file(file.path, /pattern/) }
    let(:file)    { Tempfile.new('find_pattern_in_file') }

    before do
      file.write(file_content)
      file.close
    end

    context 'when the file contains the pattern' do
      let(:file_content) do
        <<~FILE
          Beginning of file

          pattern
          pattern
          pattern

          End of file
        FILE
      end

      it 'returns the pattern once' do
        expect(subject).to match_array(%w[pattern])
      end
    end

    context 'when the file does not contain the pattern' do
      let(:file_content) do
        <<~FILE
          Beginning of file
          End of file
        FILE
      end

      it 'returns an empty array' do
        expect(subject).to match_array([])
      end
    end
  end
end