-
Notifications
You must be signed in to change notification settings - Fork 319
/
subscribe_to_newsletter_spec.rb
99 lines (71 loc) · 3.29 KB
/
subscribe_to_newsletter_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
require 'rails_helper'
feature "Subscribe to newsletter" do
context "in browser with native date input", driver: driver_with(native_date_input: true) do
scenario "subscribes user to newsletter" do
visit_new_subscription
fill_in "Email", with: "buddy@example.tld"
fill_in "Start date", with: "01/01/2015"
click_button "Subscribe"
# be_pending_subscription_page is a custom matcher (see spec/matchers)
expect(page).to be_pending_subscription_page
expect do
visit_emailed_confirm_subscription_link("buddy@example.tld")
expect(page).to be_confirm_subscription_page(Subscription.last)
.with_subscription_starting_on("January 1st, 2015")
end.to change { Subscription.where(confirmed: true).count }.from(0).to(1)
end
end
context "in browser without native date input", driver: driver_with(native_date_input: false) do
scenario "subscribes user to newsletter" do
visit_new_subscription
fill_in "Email", with: "buddy@example.tld"
fill_in_start_date_with_picker
click_button "Subscribe"
expect(page).to be_pending_subscription_page
expect do
visit_emailed_confirm_subscription_link("buddy@example.tld")
expect(page).to be_confirm_subscription_page(Subscription.last)
.with_subscription_starting_on("January 31st, 2015")
end.to change { Subscription.where(confirmed: true).count }.from(0).to(1)
end
end
private
def fill_in_start_date_with_picker
expect(page).not_to have_css("#subscription_start_on + .picker--opened")
find_field("Start date").click # open the date picker
expect(page).to have_css("#subscription_start_on + .picker--opened")
within "#subscription_start_on + .picker" do
find("select[title='Select a year']").select("2015")
find("select[title='Select a month']").select("January")
find("[aria-label='2015-01-31']", text: "31").click
end
expect(page).not_to have_css("#subscription_start_on + .picker--opened")
expect(page).to have_field("subscription_start_on", with: "2015-01-31")
end
def visit_emailed_confirm_subscription_link(recipient)
# Use email_spec helpers to:
# 1. Open the correct email, then
# 2. Visit the confirm link in that email
open_email recipient, with_subject: "Please confirm"
visit_in_email "Confirm your subscription"
end
def visit_new_subscription
visit "/"
# Test that the Subscribe to newsletter link works
click_link "Subscribe to newsletter"
# Test the page title *BEFORE* testing the path when turbolinks
# performs the page load.
#
# Capybara's methods account that some browser operations happen
# asynchronously. Capybara will retry most operations for a few
# seconds before failing a test (Capybara.default_wait_time is 2
# seconds by default). `expect ... eq ...` will not
# retry, whereas `expect ... have_title ...` does retry. Once
# the new title test is passed, we can be confident that the
# browser's current path has updated to the new path.
expect(page).to have_title "Subscribe to our newsletter"
expect(current_path).to eq new_subscription_path
today = Time.zone.today.strftime("%Y-%m-%d") # Formats like: 2015-03-22
expect(page).to have_field "Start date", with: today
end
end