1# frozen_string_literal: true
2
3RSpec.shared_examples 'packages list' do |check_project_name: false|
4  it 'shows a list of packages' do
5    wait_for_requests
6
7    packages.each_with_index do |pkg, index|
8      package_row = package_table_row(index)
9
10      expect(package_row).to have_content(pkg.name)
11      expect(package_row).to have_content(pkg.version)
12      expect(package_row).to have_content(pkg.project.name) if check_project_name
13    end
14  end
15
16  def package_table_row(index)
17    page.all("#{packages_table_selector} > [data-qa-selector=\"package_row\"]")[index].text # rubocop:disable QA/SelectorUsage
18  end
19end
20
21RSpec.shared_examples 'package details link' do |property|
22  let(:package) { packages.first }
23
24  it 'navigates to the correct url' do
25    page.within(packages_table_selector) do
26      click_link package.name
27    end
28
29    expect(page).to have_current_path(project_package_path(package.project, package))
30
31    expect(page).to have_css('.packages-app h2[data-testid="title"]', text: package.name)
32
33    expect(page).to have_content('Installation')
34    expect(page).to have_content('Registry setup')
35  end
36end
37
38RSpec.shared_examples 'when there are no packages' do
39  it 'displays the empty message' do
40    expect(page).to have_content('There are no packages yet')
41  end
42end
43
44RSpec.shared_examples 'correctly sorted packages list' do |order_by, ascending: false|
45  context "ordered by #{order_by} and ascending #{ascending}" do
46    before do
47      click_sort_option(order_by, ascending)
48    end
49
50    it_behaves_like 'packages list'
51  end
52end
53
54RSpec.shared_examples 'shared package sorting' do
55  it_behaves_like 'correctly sorted packages list', 'Type' do
56    let(:packages) { [package_two, package_one] }
57  end
58
59  it_behaves_like 'correctly sorted packages list', 'Type', ascending: true do
60    let(:packages) { [package_one, package_two] }
61  end
62
63  it_behaves_like 'correctly sorted packages list', 'Name' do
64    let(:packages) { [package_two, package_one] }
65  end
66
67  it_behaves_like 'correctly sorted packages list', 'Name', ascending: true do
68    let(:packages) { [package_one, package_two] }
69  end
70
71  it_behaves_like 'correctly sorted packages list', 'Version' do
72    let(:packages) { [package_one, package_two] }
73  end
74
75  it_behaves_like 'correctly sorted packages list', 'Version', ascending: true do
76    let(:packages) { [package_two, package_one] }
77  end
78
79  it_behaves_like 'correctly sorted packages list', 'Published' do
80    let(:packages) { [package_two, package_one] }
81  end
82
83  it_behaves_like 'correctly sorted packages list', 'Published', ascending: true do
84    let(:packages) { [package_one, package_two] }
85  end
86end
87
88def packages_table_selector
89  '[data-qa-selector="packages-table"]' # rubocop:disable QA/SelectorUsage
90end
91
92def click_sort_option(option, ascending)
93  wait_for_requests
94
95  # Reset the sort direction
96  if page.has_selector?('button[aria-label="Sorting Direction: Ascending"]', wait: 0) && !ascending
97    click_button 'Sort direction'
98
99    wait_for_requests
100  end
101
102  find('button.gl-dropdown-toggle').click
103
104  page.within('.dropdown-menu') do
105    click_button option
106  end
107
108  if ascending
109    wait_for_requests
110
111    click_button 'Sort direction'
112  end
113end
114