Skip to content

Commit

Permalink
Merge pull request #13029 from mkllnk/flaky-order-cycle-spec
Browse files Browse the repository at this point in the history
Add timepicker helper with added expectation to reduce flakiness
  • Loading branch information
dacook authored Jan 2, 2025
2 parents f5856d5 + c1f8d30 commit 7f3b1c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
25 changes: 21 additions & 4 deletions spec/support/features/datepicker_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ def select_dates_from_daterangepicker(from, to)
select_date_from_datepicker(to)
end

def select_datetime_from(element, datetime)
datetime = Time.zone.parse(datetime) if datetime.is_a? String

# Wait for timepicker element to be loaded:
expect(page).to have_css "#{element}.datetimepicker"

find(element).click
select_datetime_from_datepicker(datetime)
close_datepicker
end

def select_date_from_datepicker(date)
within ".flatpickr-calendar.open" do
# Unfortunately, flatpickr doesn't notice a change of year when we do
Expand All @@ -31,11 +42,17 @@ def select_date_from_datepicker(date)
end

def select_datetime_from_datepicker(datetime)
## First of all select date
select_date_from_datepicker(datetime)
# Then select time
find(".flatpickr-calendar.open .flatpickr-hour").set datetime.strftime("%H").to_s.strip
find(".flatpickr-calendar.open .flatpickr-minute").set datetime.strftime("%M").to_s.strip
fill_in "Hour", with: datetime.strftime("%H")
fill_in "Minute", with: datetime.strftime("%M")

# Flatpickr needs time to update the time.
# Otherwise submitting the form may not work.
# CI experimentation: 10ms -> 7% success
# 50ms -> 87% success
# 100ms -> 100% success in 112 runs
# Let's double that to reduce flakiness even further.
sleep 0.2
end

def pick_datetime(calendar_selector, datetime_selector)
Expand Down
55 changes: 21 additions & 34 deletions spec/system/admin/order_cycles/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
expect(page).to have_field 'order_cycle_name', with: "Order cycle name updated"
expect(page).to have_content('Your order cycle has been updated.')

# change date range field value
find('#order_cycle_orders_close_at').click
select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00")
close_datepicker
select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00"
expect(page).to have_content('You have unsaved changes')

# click save to open warning modal
Expand All @@ -61,10 +58,7 @@
login_as_admin
visit edit_admin_order_cycle_path(order_cycle)

# change date range field value
find('#order_cycle_orders_close_at').click
select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00")
close_datepicker
select_datetime_from "#order_cycle_orders_open_at", "2024-03-30 00:00"

# click save to open warning modal
click_button('Save')
Expand All @@ -80,29 +74,28 @@
end
end

context 'with no attached order' do
it "does not show warning modal", retry: 3 do
context "with no attached order" do
it "does not show warning modal" do
login_as_admin
visit edit_admin_order_cycle_path(order_cycle)

# change non-date range field value
fill_in 'order_cycle_name', with: "OC1 name updated"
expect(page).to have_content('You have unsaved changes')
fill_in "order_cycle_name", with: "OC1 name updated"
expect(page).to have_content "You have unsaved changes"

# click save
click_button('Save')
expect(page).to have_field 'order_cycle_name', with: 'OC1 name updated'
expect(page).to have_content('Your order cycle has been updated.')

# Now change date range field value
find('#order_cycle_orders_close_at').click
select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00")
close_datepicker
expect(page).to have_content('You have unsaved changes')

click_button('Save')
expect(page).to have_field 'order_cycle_orders_close_at', with: '2024-03-30 00:00'
expect(page).to have_content('Your order cycle has been updated.')
click_button "Save"
expect(page).not_to have_content "You have unsaved changes"
expect(page).to have_content "Your order cycle has been updated."
expect(page).to have_field "order_cycle_name", with: "OC1 name updated"

select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 01:20"
expect(page).to have_content "You have unsaved changes"

click_button "Save"
expect(page).not_to have_content "You have unsaved changes"
expect(page).to have_content "Your order cycle has been updated."
expect(page).to have_field "order_cycle_orders_close_at", with: "2024-03-30 01:20"
end
end
end
Expand All @@ -126,10 +119,7 @@
expect(page).to have_content('Your order cycle has been updated.')
expect(page).to have_field 'order_cycle_name', with: "Order cycle name updated"

# change date range field value
find('#order_cycle_orders_close_at').click
select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00")
close_datepicker
select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00"

expect(page).to have_content('You have unsaved changes')

Expand All @@ -147,7 +137,7 @@
end

context 'with no attached orders' do
it "does not show warning modal", retry: 3 do
it "does not show warning modal" do
login_as_admin
visit edit_admin_order_cycle_path(order_cycle)

Expand All @@ -161,10 +151,7 @@
expect(page).to have_content('Your order cycle has been updated.')
expect(page.find('#order_cycle_name').value).to eq 'OC1 name updated'

# Now change date range field value
find('#order_cycle_orders_close_at').click
select_datetime_from_datepicker Time.zone.parse("2024-03-30 00:00")
close_datepicker
select_datetime_from "#order_cycle_orders_close_at", "2024-03-30 00:00"
expect(page).to have_content('You have unsaved changes')
sleep(2)

Expand Down

0 comments on commit 7f3b1c0

Please sign in to comment.