-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstories_manage_spec.rb
More file actions
396 lines (303 loc) · 12.4 KB
/
stories_manage_spec.rb
File metadata and controls
396 lines (303 loc) · 12.4 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
require "rails_helper"
RSpec.describe "managing stories", js: true do
let(:user) { FactoryBot.create(:user) }
let(:project) { FactoryBot.create(:project) }
let!(:story) { FactoryBot.create(:story, project: project) }
before do
login_as(user, scope: :user)
end
it "allows me to add a story" do
visit project_path(id: project.id)
click_link "Add a Story"
fill_in "story[title]", with: "As a user, I want to add stories"
fill_in "story[description]", with: "This story allows users to add stories."
fill_in "story[extra_info]", with: "This story allows users to add extra details."
default_status = find(:option, "Pending")
expect(default_status).to be_selected
click_button "Create"
expect(Story.count).to eq 2
story = Story.last
expect(story.pending?).to be true
end
context "within the new story page" do
it "allows me to select a status other than pending" do
visit project_path(id: project.id)
click_link "Add a Story"
fill_in "story[title]", with: "As a user, I want to add stories"
fill_in "story[description]", with: "This story allows users to add stories."
fill_in "story[extra_info]", with: "This story allows users to add extra details."
select "Approved", from: "Status"
status = find(:option, "Approved")
expect(status).to be_selected
click_button "Create"
story = Story.last
expect(story.approved?).to be true
end
it "doesn't have a Rejected option for the status" do
visit project_path(id: project.id)
click_link "Add a Story"
expect { find(:option, "Rejected") }.to raise_error(Capybara::ElementNotFound)
end
end
it "allows me to clone a story" do
visit project_path(id: project.id)
within_story_row(story) do
click_button "More actions"
click_link "Clone"
end
expect(page.find("#story_title").value).to eq story.title
expect(page.find("#story_description").value).to eq story.description
click_button "Create"
expect(Story.count).to eq 2
end
it "allows me to edit a story" do
visit project_path(id: project.id)
click_button "More actions"
click_link "Edit"
fill_in "story[title]", with: "As a user, I want to edit stories"
click_button "Save Changes"
expect(page).to have_content "Story updated!"
end
it "alerts me when I try to navigate away from the page without saving my edits", js: true do
visit project_path(id: project.id)
click_button "More actions"
click_link "Edit"
click_link "Back"
assert_current_path project_path(id: project.id)
click_button "More actions"
click_link "Edit"
fill_in "story[title]", with: "As a user, I want to edit stories"
dismiss_confirm("You have unsaved changes. Are you sure you want to go back?") do
find("#logo").click
end
assert_current_path edit_project_story_path(project, story)
accept_confirm("You have unsaved changes. Are you sure you want to go back?") do
click_link "Back"
end
assert_current_path project_path(id: project.id)
end
it "allows me to delete a story" do
visit project_path(id: project.id)
expect(page).to have_text story.title
accept_confirm do
click_button "More actions"
click_link "Delete"
end
expect(page).not_to have_text story.title
expect(Story.count).to eq 0
end
it "allows me to delete a story from show page" do
visit project_story_path(project, story)
expect(page).to have_text story.title
accept_confirm do
click_link "Delete"
end
expect(page).not_to have_text story.title
expect(Story.count).to eq 0
end
it "does not allow me to bulk delete stories when there are none selected" do
visit project_path(id: project.id)
expect(page).to have_selector("#bulk_delete[aria-disabled='true']")
expect(page).to have_selector("#bulk_delete[disabled]")
end
it "allows me to bulk delete stories when one or more stories are selected" do
visit project_path(id: project.id)
within_story_row(story) { check(option: story.id.to_s) }
expect(page).to have_no_selector("#bulk_delete[aria-disabled='true']")
expect(page).to have_no_selector("#bulk_delete[disabled]")
expect(page).to have_button("Bulk Delete (1 Story)")
page.accept_confirm "Are you sure you want to delete 1 story?" do
click_button("Bulk Delete (1 Story)")
end
# Make sure tbody empty
expect(find("tbody")).to have_no_css("*")
expect(Story.count).to eq 0
end
it "does not bulk delete stories when confirmation is dismissed" do
visit project_path(id: project.id)
within_story_row(story) { check(option: story.id.to_s) }
expect(page).to have_no_selector("#bulk_delete[aria-disabled='true']")
expect(page).to have_no_selector("#bulk_delete[disabled]")
expect(page).to have_button("Bulk Delete (1 Story)")
page.dismiss_confirm "Are you sure you want to delete 1 story?" do
click_button("Bulk Delete (1 Story)")
end
expect(Story.count).to eq 1
end
context "previews" do
it "shows a preview of the description while typing", js: true do
visit project_path(id: project.id)
click_link "Add a Story"
fill_in "story[title]", with: "As a user, I want to add stories"
desc = <<~DESC
This story allows users to add stories.
some
code
DESC
expect(page).to have_text("Description Preview")
fill_in "story[description]", with: desc
expect(find("#story_description").value).to have_text("This story allows users to add stories.\n\n some\n code\n\n")
within(".story_preview .content") do
expect(page).to have_text("This story allows users to add stories.")
expect(page).to have_selector("pre", text: "some\ncode")
end
click_button "Create"
expect(page).to have_text(project.title)
story = Story.last
within_story_row(story) do
click_button "More actions"
click_link "Edit"
end
expect(page).to have_text("Edit Story")
within(".story_preview .content") do
expect(page).to have_text("This story allows users to add stories.")
expect(page).to have_selector("pre", text: "some\ncode")
end
end
it "shows a preview of the extra information while typing" do
visit project_path(id: project.id)
click_link "Add a Story"
fill_in "story[title]", with: "As a user, I want to add stories"
desc = <<~DESC
This story allows users to add extra information.
some
codes
DESC
expect(page).to have_text("Extra Info Preview")
fill_in "story[extra_info]", with: desc
within(".extra_info_preview .content") do
expect(page).to have_selector("p", text: "This story allows users to add extra information.")
expect(page).to have_selector("pre", text: "some\ncodes")
end
click_button "Create"
expect(page).to have_text(project.title)
story = Story.last
within_story_row(story) do
click_button "More actions"
click_link "Edit"
end
expect(page).to have_text("Edit Story")
within(".extra_info_preview .content") do
expect(page).to have_selector("p", text: "This story allows users to add extra information.")
expect(page).to have_selector("pre", text: "some\ncodes")
end
end
it "debounces the requests to update the preview to not flood the server" do
renderer = double(:markdown_renderer)
allow(renderer).to receive(:render).and_return("<span>preview</span>")
allow(Redcarpet::Markdown).to receive(:new).and_return(renderer)
visit project_path(id: project.id)
click_link "Add a Story"
fill_in "story[title]", with: "As a user, I want to add stories"
fill_in "story[description]", with: "desc 1"
fill_in "story[description]", with: "desc 2"
fill_in "story[description]", with: "desc 3"
# wait until requests are triggered
sleep(0.5)
fill_in "story[description]", with: "desc 4"
sleep(0.5)
# twice for the initial render of the page (description and extra info)
# once for the preview of the 3 consecutive inputs
# once for the preview of the 4th input
expect(renderer).to have_received(:render).exactly(4).times
end
end
it "can move stories between siblings" do
project2 = FactoryBot.create(:project, parent: project)
project3 = FactoryBot.create(:project, parent: project)
story = FactoryBot.create(:story, project: project2)
visit project_path(id: project2.id)
within_story_row(story) do
# move to options are hidden
expect(page).not_to have_text project3.title
click_button "More actions"
click_button "Move to"
# only one option is to move to since there's only one sibling
expect(page).to have_selector ".move-story .dropdown > form", count: 1
# confirm moving the story
accept_confirm do
click_button project3.title
end
end
expect(page).to have_text "Story moved"
expect(page).not_to have_text story.title
expect(story.reload.project).to eq project3
end
# see issue #9 on github
it "preserves order of stories when editing" do
empty_project = FactoryBot.create(:project)
visit project_path(id: empty_project.id)
click_link "Add a Story"
fill_in "Title", with: "Story 1"
fill_in "Description (Markdown)", with: "desc"
click_button "Create"
# check that it adds a position for new stories
story1 = Story.last
expect(story1.position).to be 1
click_link "Add a Story"
fill_in "Title", with: "Story 2"
fill_in "Description (Markdown)", with: "desc"
click_button "Create"
story2 = Story.last
expect(story2.position).to be 2
# check that the order is not broken after edit for stories with no position
story1.update_attribute(:position, nil)
story2.update_attribute(:position, nil)
within("#story_#{story1.id}") do
click_button "More actions"
click_link "Edit"
end
expect(page).to have_text("Edit Story")
fill_in "Description (Markdown)", with: "desc2"
click_button "Save Changes"
expect(page).to have_text("Story updated!")
within("#stories") do
expect(find("tr:nth-child(1)")).to have_text story1.title
expect(find("tr:nth-child(2)")).to have_text story2.title
end
# check that stories with nil position are listed first
click_link "Add a Story"
fill_in "Title", with: "Story 3"
fill_in "Description (Markdown)", with: "desc"
click_button "Create"
story3 = Story.last
expect(story3.position).to be 1
within("#stories") do
expect(find("tr:nth-child(1)")).to have_text story1.title
expect(find("tr:nth-child(2)")).to have_text story2.title
expect(find("tr:nth-child(3)")).to have_text story3.title
end
end
it "allows sorting stories", js: true do
FactoryBot.create(:story, project: project, title: "Juan and Aysan Code!")
story3 = FactoryBot.create(:story, project: project, title: "Last story")
visit project_path(project)
first = find(".project-table__cell", text: story.title)
last = find(".project-table__cell", text: story3.title)
# The use expect_any_instance_of is discouraged by the RSpec team, but the drag_to
# method is not always consistent on the distance it drags elements and the order
# is not always the expected. So I'm using expect_any_instance_of on purpose here.
expect_any_instance_of(ProjectsController).to receive(:sort_stories)
last.drag_to(first, delay: 0, html5: false)
within("#stories") do
expect(find("tr:nth-child(1)")).to have_text story3.title
expect(find("tr:nth-child(2)")).to have_text story.title
end
expect(page).not_to have_selector(".project-table.sorting")
end
it "filter stories by title or ID", js: true do
story4 = FactoryBot.create(:story, project: project, title: "Deprecation warning XYZ")
story5 = FactoryBot.create(:story, project: project, title: "Dangerous query method")
visit project_path(id: project.id)
fill_in "title_contains", with: "XYZ"
within("#stories") do
expect(find("td:nth-child(1)")).to have_text story4.title
expect(all("#stories > tr").count).to eq(1)
end
fill_in "title_contains", with: story5.id
within("#stories") do
expect(find("td:nth-child(1)")).to have_text story5.title
expect(all("#stories > tr").count).to eq(1)
end
end
end