-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
show_page_spec.rb
218 lines (167 loc) · 5.49 KB
/
show_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
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
require "rails_helper"
RSpec.describe "customer show page" do
describe "paginates customers' orders" do
it "displays the first page by default, other pages when specified" do
customer = create(:customer)
orders = create_list(:order, 4, customer: customer)
order_ids = orders.map(&:id)
ids_in_page1 = ids_in_page2 = nil
visit admin_customer_path(customer)
within(table_for_attribute(:orders)) do
ids_in_page1 = ids_in_table
expect(ids_in_page1.count).to eq 2
expect(order_ids).to include(*ids_in_page1)
end
click_on("Next ›")
within(table_for_attribute(:orders)) do
ids_in_page2 = ids_in_table
expect(ids_in_page2.count).to eq 2
expect(order_ids).to include(*ids_in_page2)
end
ids_in_table = (ids_in_page1 + ids_in_page2).uniq
expect(ids_in_table).to match_array(order_ids)
end
end
it "displays the customer's title attribute as the header" do
customer = create(:customer)
visit admin_customer_path(customer)
expect(page).to have_header(customer.name)
end
it "displays the customer's other attributes" do
customer = create(:customer)
visit admin_customer_path(customer)
expect(page).to have_content(customer.email)
end
it "displays each of the customer's orders" do
customer = create(:customer)
orders = create_pair(:order, customer: customer)
orders.map.with_index(1) do |order, index|
create(:line_item, order: order, unit_price: 10, quantity: index)
end
visit admin_customer_path(customer)
orders.each do |order|
expect(page).to have_content(order.total_price)
end
end
it "sorts each of the customer's orders" do
customer = create(:customer)
orders = create_list(:order, 4, customer: customer)
orders.map.with_index(1) do |order, index|
create(:line_item, order: order, unit_price: 10, quantity: index)
end
visit admin_customer_path(customer, orders: {
order: :id, direction: :desc
})
order_ids = orders.sort_by(&:id).map(&:id).reverse
within(table_for_attribute(:orders)) do
expect(order_ids.first(2)).to eq(ids_in_table)
end
visit admin_customer_path(customer, orders: {
order: :id, direction: :desc, page: 2
})
within(table_for_attribute(:orders)) do
expect(order_ids.last(2)).to eq(ids_in_table)
end
end
it "sorts each of the customer's orders and log entries independently" do
customer = create(:customer)
orders = create_list(:order, 4, customer: customer)
log_entries = create_list(:log_entry, 4, logeable: customer)
orders.map.with_index(1) do |order, index|
create(:line_item, order: order, unit_price: 10, quantity: index)
end
visit admin_customer_path(
customer,
orders: { order: :id, direction: :desc },
log_entries: { order: :id, direction: :asc },
)
order_ids = orders.sort_by(&:id).map(&:id).reverse
log_entry_ids = log_entries.sort_by(&:id).map(&:id)
within(table_for_attribute(:orders)) do
expect(order_ids.first(2)).to eq(ids_in_table)
end
within(table_for_attribute(:log_entries)) do
expect(log_entry_ids.first(2)).to eq(ids_in_table)
end
visit admin_customer_path(
customer,
orders: {
order: :id,
direction: :desc,
page: 2,
},
)
within(table_for_attribute(:orders)) do
expect(order_ids.last(2)).to eq(ids_in_table)
end
within(table_for_attribute(:log_entries)) do
expect(log_entry_ids.first(2)).to eq(ids_in_table)
end
end
it "links to the customer's orders", :js do
customer = create(:customer)
order = create(:order, customer: customer)
visit admin_customer_path(customer)
click_row_for(order)
expect(page).to have_header(displayed(order))
end
it "link-ifies the email" do
customer = create(:customer)
visit admin_customer_path(customer)
expect(page).to have_link(customer.email)
end
it "links to the edit page" do
customer = create(:customer)
visit admin_customer_path(customer)
click_on "Edit"
expect(page).to have_header("Edit #{displayed(customer)}")
end
it "displays translated labels" do
custom_label = "Newsletter Subscriber"
customer = create(:customer)
translations = {
helpers: {
label: {
customer: {
email_subscriber: custom_label,
},
},
},
}
with_translations(:en, translations) do
visit admin_customer_path(customer)
expect(page).to have_css(".attribute-label", text: custom_label)
end
end
it "displays translated labels in has_many collection partials" do
custom_label = "Time Shipped"
customer = create(:customer)
create(:order, customer: customer)
translations = {
administrate: {
actions: {
edit: "Edit",
destroy: "Destroy",
confirm: "Are you sure?",
},
},
helpers: {
label: {
order: {
shipped_at: custom_label,
},
},
},
}
with_translations(:en, translations) do
visit admin_customer_path(customer)
expect(page).to have_css(".cell-label", text: custom_label)
end
end
def ids_in_table
all("tr td:first-child").map(&:text).map(&:to_i)
end
def table_for_attribute(attr_name)
find("table[aria-labelledby=#{attr_name}]")
end
end