-
Notifications
You must be signed in to change notification settings - Fork 90
/
images_test.rb
206 lines (161 loc) · 5.72 KB
/
images_test.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
require "application_system_test_case"
class ConversationMessagesImagesTest < ApplicationSystemTestCase
setup do
preprocess_all_variants!
@user = users(:keith)
login_as @user
@conversation = conversations(:attachments)
end
test "images render in messages WHEN pre-processed, clicking opens modal" do
visit_and_scroll_wait conversation_messages_path(@conversation)
image_msg = find_messages.third
image_btn = image_msg.find_role("image-preview")
loader = image_btn.find_role("image-loader")
img = image_btn.find("img", visible: :all)
modal = image_msg.find_role("image-modal")
assert image_btn
assert img
refute loader.visible?, "loader should NEVER be visible in this test"
wait_for_images_to_load
refute modal.visible?
image_btn.click
2.times do
sleep 0.1
sleep 0.5 if !modal.visible?
sleep 0.1
image_btn.click if !modal.visible?
end # TODO: sometimes modal has not popped up after clicking, why?? Try 2x times before failing the test.
assert_true "modal image should have been visible" do
modal.visible?
end
send_keys "esc"
assert_false "modal image should have closed/hidden itself" do
modal.visible?
end
end
test "images eventually render in messages WHEN NOT pre-processed, clicking opens modal" do
stimulate_image_variant_processing do
visit_and_scroll_wait conversation_messages_path(@conversation)
image_msg = find_messages.third
image_btn = image_msg.find_role("image-preview")
img = image_btn.find("img", visible: false)
modal = image_msg.find_role("image-modal")
assert image_btn
assert img
assert modal
assert_true wait: 5 do
img.visible?
end
assert img.visible?
wait_for_images_to_load
refute modal.visible?
image_btn.click
2.times do
sleep 0.1
sleep 0.5 if !modal.visible?
sleep 0.1
image_btn.click if !modal.visible?
end # TODO: sometimes modal has not popped up after clicking, why?? Try 2x times before failing the test.
assert_true "modal image should have been visible", wait: 0.6 do
modal.visible?
end
send_keys "esc"
assert_false "modal image should have closed/hidden itself" do
modal.visible?
end
end
end
test "ensure images display a spinner initially if they get a 404 and then eventually get replaced with the image" do
stimulate_image_variant_processing do
visit_and_scroll_wait conversation_messages_path(@conversation)
image_msg = find_messages.third
image_btn = image_msg.find_role("image-preview")
loader = image_btn.find_role("image-loader")
img = image_btn.find("img", visible: :all)
modal_container = image_msg.find_role("image-modal")
modal_loader = modal_container.find_role("image-loader")
modal_img = modal_container.find("img", visible: :all)
assert_true "image loader should be visible", wait: 0.6 do
loader.visible?
end
refute img.visible?
image_btn.click
2.times do
sleep 0.1
sleep 0.5 if !modal_loader.visible?
sleep 0.1
image_btn.click if !modal_loader.visible?
end # TODO: sometimes modal has not popped up after clicking, why?? Try 2x times before failing the test.
assert_true "modal image loader should be visible", wait: 0.6 do
modal_loader.visible?
end
refute modal_img.visible?
send_keys "esc"
assert_false "image loader should have eventually disappeared", wait: 10 do
loader.visible?
end
assert img.visible?
wait_for_images_to_load
image_btn.click
assert_true "modal image should be visible" do
modal_img.visible?
end
refute modal_loader.visible?
end
end
test "ensure page scrolls back down to the bottom after an image pops in late" do
stimulate_image_variant_processing do
visit_and_scroll_wait conversation_messages_path(@conversation)
image_msg = find_messages.third
image_container = image_msg.find_role("image-preview")
img = image_container.find("img", visible: :all)
assert_at_bottom
assert_scrolled_down do
wait_for_images_to_load
assert_true do
img.visible?
end
end
assert_at_bottom
end
end
test "images in previous messages remain after submitting a new message, they should not display a new spinner" do
image_msg = img = nil
stimulate_image_variant_processing do
visit_and_scroll_wait conversation_messages_path(@conversation)
image_msg = find_messages.third
img = image_msg.find_role("image-preview").find("img", visible: :all)
assert_true wait: 5 do
img.visible?
end
end
send_keys "hello?"
send_keys "enter"
assert_composer_blank
assert img.visible?
send_keys "hello?"
send_keys "enter"
assert_composer_blank
assert img.visible?
end
private
def preprocess_all_variants!
Document.all.each do |d|
d.send(:wait_for_file_variant_to_process!, :small)
d.send(:wait_for_file_variant_to_process!, :large)
end
end
def stimulate_image_variant_processing(&block)
Document.stub_any_instance(:has_file_variant_processed?, false) do
ActiveStorage::PostgresqlController.stub_any_instance(:decode_verified_key, simulate_not_preprocessed) do
yield block
end
end
end
def simulate_not_preprocessed
->() do
return nil if params[:retry_count].to_i < 8
ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key)&.symbolize_keys
end
end
end