Add flatpickr_spec.rb to spec/system and add tests for datepicker in it. Use nth-last-child in tests. Modify test for datetimepicker to also select a datetime before closing and then check the value is correct after close occurred

This commit is contained in:
Thierry Jet
2021-10-26 20:01:50 +02:00
parent 8eff2716ed
commit c8340d5c3b
2 changed files with 56 additions and 3 deletions

View File

@@ -150,22 +150,28 @@ describe '
it "correctly opens the datetimepicker and closes it using the last button (the 'Close' one)" do
login_as_admin_and_visit admin_order_cycles_path
test_value = Time.parse("2022-12-22 00:00")
# Opens a datetimepicker
within("tr.order-cycle-#{oc_pt.id}") do
find('input.datetimepicker', match: :first).click
end
# Looks for the close button and click it
# Sets the value to test_value then looks for the close button and click it
within(".flatpickr-calendar.open") do
expect(page).to have_selector '.shortcut-buttons-flatpickr-buttons'
fp_buttons_count = find_all('.shortcut-buttons-flatpickr-button').count
find(".shortcut-buttons-flatpickr-buttons > button:nth-child(#{fp_buttons_count})").click
select_datetime_from_datepicker test_value
find(".shortcut-buttons-flatpickr-buttons > button:nth-last-child(1)").click
end
# Should no more have opened flatpickr
expect(page).not_to have_selector '.flatpickr-calendar.open'
# Check the value is correct
within("tr.order-cycle-#{oc_pt.id}") do
expect(find('input.datetimepicker', match: :first).value).to eq test_value.to_datetime.strftime("%Y-%m-%d %H:%M")
end
end
end
end

View File

@@ -0,0 +1,47 @@
# frozen_string_literal: true
require "system_helper"
describe "Test Flatpickr", js: true do
include AuthenticationHelper
include WebHelper
context "orders" do
it "opens the datepicker and closes it using the last button (the 'Close' one)" do
login_as_admin_and_visit 'admin/orders'
open_datepicker('#q_completed_at_gteq')
# Looks for the close button and click it
within(".flatpickr-calendar.open") do
expect(page).to have_selector '.shortcut-buttons-flatpickr-buttons'
find(".shortcut-buttons-flatpickr-buttons > button:nth-last-child(1)").click
end
# Should no more have opened flatpickr
expect(page).not_to have_selector '.flatpickr-calendar.open'
end
it "opens the datepicker and sets date to today" do
login_as_admin_and_visit 'admin/orders'
open_datepicker('#q_completed_at_gteq')
choose_today_from_datepicker
check_fielddate('#q_completed_at_gteq', Date.today())
end
it "opens the datepicker and closes it by clicking outside" do
login_as_admin_and_visit 'admin/orders'
open_datepicker('#q_completed_at_gteq')
find("#admin-menu").click
# Should no more have opened flatpickr
expect(page).not_to have_selector '.flatpickr-calendar.open'
end
end
private
def open_datepicker(field)
# Opens a datepicker
find(field).click
# Should have opened flatpickr
expect(page).to have_selector '.flatpickr-calendar.open'
end
def check_fielddate(field, date)
# Check the value is correct
expect(find(field, match: :first).value).to eq date.to_datetime.strftime("%Y-%m-%d")
end
end