/
index_page_spec.rb
177 lines (132 loc) · 4.3 KB
/
index_page_spec.rb
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
require "rails_helper"
search_input_selector = ".search__input"
describe "customer index page" do
it "displays customers' name and email" do
customer = create(:customer)
visit admin_customers_path
expect(page).to have_header("Customers")
expect(page).to have_content(customer.name)
expect(page).to have_content(customer.email)
end
it "links to the customer show page", :js do
customer = create(:customer)
visit admin_customers_path
click_row_for(customer)
expect(page).to have_header(displayed(customer))
expect(page).to have_content(customer.name)
expect(page).to have_content(customer.email)
end
it "links to the customer show page without javascript", js: false do
customer = create(:customer)
visit admin_customers_path
click_show_link_for(customer)
expect(page).to have_header(displayed(customer))
end
it "links to the edit page" do
customer = create(:customer)
visit admin_customers_path
click_on "Edit"
expect(current_path).to eq(edit_admin_customer_path(customer))
end
it "links to the new page" do
visit admin_customers_path
click_on("New customer")
expect(current_path).to eq(new_admin_customer_path)
end
it "displays translated labels" do
custom_label = "Newsletter Subscriber"
translations = {
helpers: {
label: {
customer: {
email_subscriber: custom_label,
},
},
},
}
with_translations(:en, translations) do
visit admin_customers_path
expect(page).to have_table_header(custom_label)
end
end
it "paginates records based on a constant" do
customers = create_list(:customer, 2)
visit admin_customers_path(per_page: 1)
expect(page).not_to have_content(customers.last.name)
click_on "Next"
expect(page).to have_content(customers.last.name)
end
describe "sorting" do
def expect_to_appear_in_order(*elements)
positions = elements.map { |e| page.body.index(e) }
expect(positions).to eq(positions.sort)
end
it "allows sorting by columns" do
create(:customer, name: "unique name two")
create(:customer, name: "unique name one")
visit admin_customers_path
click_on "Name"
expect_to_appear_in_order("unique name one", "unique name two")
end
it "allows clicking through after sorting", :js do
customer = create(:customer)
create(:order, customer: customer)
visit admin_customers_path
click_on "Name"
find("[data-url]").click
expect(page).to have_header("Show #{customer.name}")
end
it "allows reverse sorting" do
create(:customer, name: "unique name one")
create(:customer, name: "unique name two")
visit admin_customers_path
2.times { click_on "Name" }
expect_to_appear_in_order("unique name two", "unique name one")
end
it "toggles the order" do
create(:customer, name: "unique name one")
create(:customer, name: "unique name two")
visit admin_customers_path
3.times { click_on "Name" }
expect_to_appear_in_order("unique name one", "unique name two")
end
it "preserves search" do
query = "bar@baz.com"
visit admin_customers_path(search: query)
click_on "Name"
expect(find(search_input_selector).value).to eq(query)
end
end
end
describe "search input" do
context "when resource has searchable fields" do
let(:index_with_searchable_fields) { admin_log_entries_path }
context "but none of them are displayed" do
before do
allow_any_instance_of(LogEntryDashboard).
to receive(:collection_attributes) { [] }
visit(index_with_searchable_fields)
end
it "is shown" do
expect(page).to have_selector(search_input_selector)
end
end
context "and some of them are displayed" do
before do
visit(index_with_searchable_fields)
end
it "is shown" do
expect(page).to have_selector(search_input_selector)
end
end
end
context "when resource does not have searchable fields" do
let(:index_without_searchable_fields) { admin_line_items_path }
before do
visit(index_without_searchable_fields)
end
it "is hidden" do
expect(page).to_not have_selector(search_input_selector)
end
end
end