Read about our upcoming Code of Conduct on this issue

template_type_dropdown_spec.rb 3.87 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
require 'spec_helper'

5
describe 'Projects > Files > Template type dropdown selector', :js do
6
  let(:project) { create(:project, :repository) }
Rémy Coutable's avatar
Rémy Coutable committed
7
  let(:user) { project.owner }
8
9

  before do
10
    sign_in user
11
12
13
14
15
16
17
  end

  context 'editing a non-matching file' do
    before do
      create_and_edit_file('.random-file.js')
    end

18
    it 'not displayed' do
19
20
21
      check_type_selector_display(false)
    end

22
    it 'selects every template type correctly' do
23
24
25
26
      fill_in 'file_path', with: '.gitignore'
      try_selecting_all_types
    end

27
    it 'updates template type toggle value when template is chosen' do
28
      fill_in 'file_path', with: '.gitignore'
29
      select_template('gitignore', 'Actionscript')
30
31
32
33
34
35
      check_type_selector_toggle_text('.gitignore')
    end
  end

  context 'editing a matching file' do
    before do
36
      visit project_edit_blob_path(project, File.join(project.default_branch, 'LICENSE'))
37
38
    end

39
    it 'displayed' do
40
41
42
      check_type_selector_display(true)
    end

43
    it 'is displayed when input matches' do
44
45
46
      check_type_selector_display(true)
    end

47
    it 'selects every template type correctly' do
48
49
50
51
52
      try_selecting_all_types
    end

    context 'user previews changes' do
      before do
53
        click_link 'Preview changes'
54
55
      end

56
      it 'type selector is hidden and shown correctly' do
57
58
59
60
61
62
63
64
65
        check_type_selector_display(false)
        click_link 'Write'
        check_type_selector_display(true)
      end
    end
  end

  context 'creating a matching file' do
    before do
66
      visit project_new_blob_path(project, 'master', file_name: '.gitignore')
67
68
    end

69
    it 'is displayed' do
70
71
72
      check_type_selector_display(true)
    end

73
    it 'toggle is set to the correct value' do
74
      select_template('gitignore', 'Actionscript')
75
76
77
      check_type_selector_toggle_text('.gitignore')
    end

78
79
80
81
82
    it 'sets the toggle text when selecting the template type' do
      select_template_type('.gitignore')
      check_type_selector_toggle_text('.gitignore')
    end

83
    it 'selects every template type correctly' do
84
85
86
87
88
89
      try_selecting_all_types
    end
  end

  context 'creating a file' do
    before do
90
      visit project_new_blob_path(project, project.default_branch)
91
92
    end

93
    it 'type selector is shown' do
94
95
96
      check_type_selector_display(true)
    end

97
    it 'toggle is set to the proper value' do
98
      check_type_selector_toggle_text('Select a template type')
99
100
    end

101
    it 'selects every template type correctly' do
102
103
104
105
106
107
108
109
110
111
112
      try_selecting_all_types
    end
  end
end

def check_type_selector_display(is_visible)
  count = is_visible ? 1 : 0
  expect(page).to have_css('.js-template-type-selector', count: count)
end

def try_selecting_all_types
113
114
115
116
  try_selecting_template_type('LICENSE', 'Apply a template')
  try_selecting_template_type('Dockerfile', 'Apply a template')
  try_selecting_template_type('.gitlab-ci.yml', 'Apply a template')
  try_selecting_template_type('.gitignore', 'Apply a template')
117
118
119
120
121
122
123
124
125
126
127
128
end

def try_selecting_template_type(template_type, selector_label)
  select_template_type(template_type)
  check_template_selector_display(selector_label)
end

def select_template_type(template_type)
  find('.js-template-type-selector').click
  find('.dropdown-content li', text: template_type).click
end

129
130
131
132
133
def select_template(type, template)
  find(".js-#{type}-selector-wrap").click
  find('.dropdown-content li', text: template).click
end

134
135
136
137
138
139
140
141
142
143
def check_template_selector_display(content)
  expect(page).to have_content(content)
end

def check_type_selector_toggle_text(template_type)
  dropdown_toggle_button = find('.template-type-selector .dropdown-toggle-text')
  expect(dropdown_toggle_button).to have_content(template_type)
end

def create_and_edit_file(file_name)
144
  visit project_new_blob_path(project, 'master', file_name: file_name)
Jose Ivan Vargas's avatar
Jose Ivan Vargas committed
145
  click_button "Commit changes"
146
  visit project_edit_blob_path(project, File.join(project.default_branch, file_name))
147
end