summaryrefslogtreecommitdiff
path: root/spec/support/helpers/features/web_ide_spec_helpers.rb
blob: c51116b55b20277384dc08113167c19055d02b05 (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
# frozen_string_literal: true

# These helpers help you interact within the Web IDE.
#
# Usage:
#   describe "..." do
#     include Features::WebIdeSpecHelpers
#     ...
#
#     ide_visit(project)
#     ide_commit
module Features
  module WebIdeSpecHelpers
    include Features::SourceEditorSpecHelpers

    # Open the IDE from anywhere by first visiting the given project's page
    def ide_visit(project)
      visit project_path(project)

      ide_visit_from_link
    end

    # Open the IDE from the current page by clicking the Web IDE link
    def ide_visit_from_link(link_sel = 'Web IDE')
      new_tab = window_opened_by { click_link(link_sel) }

      switch_to_window new_tab
    end

    def ide_tree_body
      page.find('.ide-tree-body')
    end

    def ide_tree_actions
      page.find('.ide-tree-actions')
    end

    def ide_tab_selector(mode)
      ".js-ide-#{mode}-mode"
    end

    def ide_folder_row_open?(row)
      row.matches_css?('.folder.is-open')
    end

    # Deletes a file by traversing to `path`
    # then clicking the 'Delete' action.
    #
    # - Throws an error if the file is not found
    def ide_delete_file(path)
      container = ide_traverse_to_file(path)

      click_file_action(container, 'Delete')
    end

    # Opens parent directories until the file at `path`
    # is exposed.
    #
    # - Returns a reference to the file row at `path`
    # - Throws an error if the file is not found
    def ide_traverse_to_file(path)
      paths = path.split('/')
      container = nil

      paths.each_with_index do |path, index|
        ide_open_file_row(container) if container
        container = find_file_child(container, path, level: index)
      end

      container
    end

    def ide_open_file_row(row)
      return if ide_folder_row_open?(row)

      row.click
    end

    def ide_set_editor_value(value)
      editor_set_value(value)
    end

    def ide_commit_tab_selector
      ide_tab_selector('commit')
    end

    def ide_commit
      find(ide_commit_tab_selector).click

      commit_to_current_branch
    end

    private

    def file_row_container(row)
      row ? row.find(:xpath, '..') : ide_tree_body
    end

    def find_file_child(row, name, level: nil)
      container = file_row_container(row)
      container.find(".file-row[data-level=\"#{level}\"]", text: name)
    end

    def click_file_action(row, text)
      row.hover
      dropdown = row.find('.ide-new-btn')
      dropdown.find('button').click
      dropdown.find('button', text: text).click
    end

    def commit_to_current_branch(option: 'Commit to master branch', message: '')
      within '.multi-file-commit-form' do
        fill_in('commit-message', with: message) if message

        choose(option)

        click_button('Commit')

        wait_for_requests
      end
    end
  end
end