diff --git a/Gemfile b/Gemfile index ef01d33..78b2bb5 100644 --- a/Gemfile +++ b/Gemfile @@ -24,4 +24,9 @@ group :test do gem 'capybara' gem 'cuprite' gem 'webrick', require: false + + # The page objects (active_admin_datetimepicker/page_objects) need it, and it needs ActiveAdmin 4. + if Gem::Version.new(activeadmin_version) >= Gem::Version.new('4.0.0.beta1') + gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', ref: '8ec1677d4118559af117421fbf6576cdd5e33721', require: false + end end diff --git a/README.md b/README.md index 1d6432f..20aa632 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,35 @@ Note: in dark mode the current/today cells use XDSoft's built-in dark skin colors, not `$aa-datetimepicker-accent-color`. +## Page objects + +Page objects for tests, built on [active_admin_page_objects](https://github.com/activeadmin-plugins/active_admin_page_objects) 2.x. They are optional and need ActiveAdmin 4. 2.x is not on RubyGems yet, so add it to your test group from GitHub: + +```ruby +gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', branch: 'restructure-vanilla-v4' +``` + +Then require the file from your spec helper: + +```ruby +require 'active_admin_datetimepicker/page_objects' +``` + +`filters.filter(:created_at)` then returns a `DateTimeRangeFilter` for a `date_time_range` filter, with no declaration. A spec that declares this filter as `ActiveAdminPageObjects::DateRangeFilter` now raises `DeclaredClassMismatch`: declare `ActiveAdminDatetimepicker::PageObjects::DateTimeRangeFilter` instead, or drop the declaration. Remove any override of `filter_range_from` and `filter_range_to` you added for these inputs: the filter finds its inputs by `data-datetimepicker-range-input`, and the gem's own range filter is untouched. + +```ruby +filter = index.filters.filter(:created_at) +filter.fill_in_range(from: Time.zone.parse('2024-01-20 10:00'), to: '2024-01-20 12:00') +index.filters.submit + +filter.from # => Sat, 20 Jan 2024 10:00:00 UTC +00:00 +filter.to +filter.range # => [from, to] +expect(filter).to have_range('2024-01-20 10:00', '2024-01-20 12:00') +``` + +`fill_in_range` takes a `Time`, a `Date` (midnight) or a `String` that `Time.zone` parses, and nil or `""` clears that end. The value is written in the format the input takes: `YYYY-MM-DDTHH:MM` for `datetime-local`, `YYYY-MM-DD` for a date column. The readers return a `Time` in the app zone, midnight for a date column. + ## Contributing 1. Fork it ( https://github.com/activeadmin-plugins/active_admin_datetimepicker/fork ) diff --git a/lib/active_admin_datetimepicker/page_objects.rb b/lib/active_admin_datetimepicker/page_objects.rb new file mode 100644 index 0000000..6e8919b --- /dev/null +++ b/lib/active_admin_datetimepicker/page_objects.rb @@ -0,0 +1,28 @@ +# Page objects for the plugin's inputs, built on the active_admin_page_objects +# gem (2.x, which needs ActiveAdmin 4). Test code only: load it from a spec +# helper with +# require 'active_admin_datetimepicker/page_objects' +# The plugin itself never requires this file, and the gemspec does not depend +# on active_admin_page_objects. +begin + require 'active_admin_page_objects' +rescue LoadError => e + raise unless e.path == 'active_admin_page_objects' + + raise LoadError, "active_admin_datetimepicker/page_objects needs the active_admin_page_objects gem (2.x) in your test group; see the active_admin_datetimepicker README" +end + +ActiveAdminPageObjects.check_version!('>= 2.0.0.a', '< 3.0.0.a', plugin: 'active_admin_datetimepicker') + +# The inputs of the range filter carry data-datetimepicker-range-input. The +# gem's own filter_range_from and filter_range_to look for names that end in +# _gteq] and _lteq], which the _datetime_picker predicates do not. +ActiveAdminPageObjects.register_selectors( + datetimepicker_range_from: "input[data-datetimepicker-range-input='from']", + datetimepicker_range_to: "input[data-datetimepicker-range-input='to']" +) + +require 'active_admin_datetimepicker/page_objects/date_time_range_filter' + +# Formtastic puts the input's name, here date_time_range, on its wrapper. +ActiveAdminPageObjects.register_filter_class('date_time_range', ActiveAdminDatetimepicker::PageObjects::DateTimeRangeFilter) diff --git a/lib/active_admin_datetimepicker/page_objects/date_time_range_filter.rb b/lib/active_admin_datetimepicker/page_objects/date_time_range_filter.rb new file mode 100644 index 0000000..c353ca5 --- /dev/null +++ b/lib/active_admin_datetimepicker/page_objects/date_time_range_filter.rb @@ -0,0 +1,115 @@ +module ActiveAdminDatetimepicker + module PageObjects + # The `as: :date_time_range` filter: a pair of native `datetime-local` + # inputs (`date` for a date column) on ActiveAdmin 4. Values are written in + # the format the input takes, in the app's time zone, which is the zone the + # plugin shows them in. `filters.filter(:created_at)` returns it without a + # declaration; declare it to type a filter: + # + # section :filters, ActiveAdminPageObjects::Filters do + # filter :created_at, 'Created At', ActiveAdminDatetimepicker::PageObjects::DateTimeRangeFilter + # end + # + # filter = filters.filter(:created_at) + # filter.fill_in_range(from: Time.zone.parse('2024-01-20 10:00'), to: '2024-01-20 12:00') + # filters.submit + # filter.from # => Sat, 20 Jan 2024 10:00:00 UTC +00:00 + class DateTimeRangeFilter < ActiveAdminPageObjects::DateRangeFilter + LOCAL_FORMAT = '%Y-%m-%dT%H:%M'.freeze + DATE_FORMAT = '%Y-%m-%d'.freeze + + # Sets both ends. +from+ and +to+ are a Time, a Date or a String that + # Time.zone parses; a Date is midnight. nil or "" clears that end. + # @raise [ArgumentError] for a String that holds no date + def fill_in_range(from:, to:) + write(from_input, from) + write(to_input, to) + self + end + + # See #fill_in_range. + def set(from, to = nil) + fill_in_range(from: from, to: to) + end + + # The "from" end as a Time in the app's zone, or nil when it is empty. + # An input without a time, such as a date column's, gives midnight. + # @return [Time, nil] + def from + read(from_input) + end + + # The "to" end, as #from gives it. + # @return [Time, nil] + def to + read(to_input) + end + + # @return [Array] #from and #to + def range + [from, to] + end + + private + + def from_input + find(:active_admin, :datetimepicker_range_from) + end + + def to_input + find(:active_admin, :datetimepicker_range_to) + end + + def zone + Time.zone || Time.find_zone!('UTC') + end + + def parse(text) + zone.parse(text) || raise(ArgumentError, "no date in #{text.inspect}") + end + + def to_time(value) + value.is_a?(String) ? parse(value) : value.in_time_zone(zone) + end + + def blank_end?(value) + value.nil? || value == '' + end + + # The step is in seconds: a sub-minute one makes the input take seconds. + def seconds?(input) + step = input[:step] + step == 'any' || (!step.to_s.empty? && step.to_f % 60 != 0) + end + + def format_of(input) + case input[:type] + when 'datetime-local' then seconds?(input) ? "#{LOCAL_FORMAT}:%S" : LOCAL_FORMAT + when 'date' then DATE_FORMAT + else defined?(ActiveAdminDatetimepicker::Base) ? ActiveAdminDatetimepicker::Base.format : '%Y-%m-%d %H:%M' + end + end + + def written(input, value) + to_time(value).strftime(format_of(input)) + end + + def write(input, value) + input.set(blank_end?(value) ? '' : written(input, value)) + end + + def read(input) + text = input.value.to_s + text.empty? ? nil : parse(text) + end + + # Compares the Times the inputs hold, as the browser may drop zero seconds. + def range_matches?(from, to) + [[from_input, from], [to_input, to]].all? do |input, value| + expected = blank_end?(value) ? nil : parse(written(input, value)) + read(input) == expected + end + end + end + end +end diff --git a/spec/page_objects_spec.rb b/spec/page_objects_spec.rb new file mode 100644 index 0000000..1e3a209 --- /dev/null +++ b/spec/page_objects_spec.rb @@ -0,0 +1,110 @@ +require 'spec_helper' + +# The page objects need active_admin_page_objects 2.x, which needs ActiveAdmin 4. +if ActiveAdminDatetimepicker.aa_v4? + require 'active_admin_datetimepicker/page_objects' + + describe 'page objects for the range filter', type: :feature, aa: 4 do + let(:authors_page) do + Class.new(ActiveAdminPageObjects::Page) do + set_url '/admin/authors' + section :filters, ActiveAdminPageObjects::Filters + end.new + end + let(:filters) { authors_page.filters } + let(:filter_class) { ActiveAdminDatetimepicker::PageObjects::DateTimeRangeFilter } + + before do + add_author_resource + Author.create!(name: 'Ren', last_name: 'before', created_at: Time.zone.parse('2024-01-20 09:00'), updated_at: Time.zone.parse('2024-01-20 09:00')) + Author.create!(name: 'Rey', last_name: 'inside', created_at: Time.zone.parse('2024-01-20 11:00'), updated_at: Time.zone.parse('2024-01-20 11:00')) + Author.create!(name: 'Ron', last_name: 'after', created_at: Time.zone.parse('2024-01-20 13:00'), updated_at: Time.zone.parse('2024-01-20 13:00')) + authors_page.load + end + + it 'returns the filter class for a datetime column, a date column and an untyped ransacker' do + expect(filters.filter(:created_at)).to be_a(filter_class) + expect(filters.filter(:birthday)).to be_a(filter_class) + expect(filters.filter(:last_seen_at)).to be_a(filter_class) + end + + it 'leaves the base gem selectors alone' do + expect(ActiveAdminPageObjects.selector(:filter_range_from)).to eq 'input[name$="_gteq]"]' + expect(ActiveAdminPageObjects.selector(:filter_range_to)).to eq 'input[name$="_lteq]"]' + end + + it 'fills the range from a Time and a String and filters by it' do + filters.filter(:created_at).fill_in_range(from: Time.zone.parse('2024-01-20 10:00'), to: '2024-01-20 12:00') + filters.submit + + expect(page).to have_text('inside') + expect(page).not_to have_text('before') + expect(page).not_to have_text('after') + expect(find('#q_created_at_gteq_datetime_picker').value).to eq '2024-01-20T10:00' + expect(find('#q_created_at_lteq_datetime_picker').value).to eq '2024-01-20T12:00' + end + + it 'reads the ends as Times in the app zone' do + filter = filters.filter(:last_seen_at) + filter.fill_in_range(from: '2024-01-20T10:00', to: Time.utc(2024, 1, 20, 12, 30).in_time_zone) + + expect(filter.from).to eq Time.zone.parse('2024-01-20 10:00') + expect(filter.to).to eq Time.zone.parse('2024-01-20 12:30') + expect(filter.range).to eq [filter.from, filter.to] + expect(filter).to have_range('2024-01-20 10:00', Time.zone.parse('2024-01-20 12:30')) + expect(filter).to have_no_range('2024-01-20 10:00', '2024-01-20 12:00', wait: 0) + end + + it 'writes a Date as midnight, and a date column as a date' do + created = filters.filter(:created_at) + created.fill_in_range(from: Date.new(2024, 1, 20), to: Date.new(2024, 1, 21)) + expect(find('#q_created_at_gteq_datetime_picker').value).to eq '2024-01-20T00:00' + expect(created.to).to eq Time.zone.parse('2024-01-21 00:00') + + birthday = filters.filter(:birthday) + birthday.fill_in_range(from: Time.zone.parse('1990-05-17 15:30'), to: '1990-06-01') + expect(find('input[name="q[birthday_gteq]"]').value).to eq '1990-05-17' + expect(find('input[name="q[birthday_lteq]"]').value).to eq '1990-06-01' + expect(birthday.from).to eq Time.zone.parse('1990-05-17 00:00') + end + + it 'clears an end with nil' do + filter = filters.filter(:created_at) + filter.fill_in_range(from: '2024-01-20 10:00', to: '2024-01-20 12:00') + filter.fill_in_range(from: nil, to: '2024-01-20 12:00') + + expect(filter.from).to be_nil + expect(filter.to).to eq Time.zone.parse('2024-01-20 12:00') + end + + it 'rejects a String without a date' do + expect { filters.filter(:created_at).fill_in_range(from: 'soon', to: nil) }.to raise_error(ArgumentError) + end + + it 'fills the inputs of a browser', js: true do + filter = filters.filter(:created_at) + filter.fill_in_range(from: Time.zone.parse('2024-01-20 10:00'), to: '2024-01-20 12:00') + + expect(filter.range).to eq [Time.zone.parse('2024-01-20 10:00'), Time.zone.parse('2024-01-20 12:00')] + filters.submit + expect(page).to have_text('inside') + expect(page).not_to have_text('after') + end + + # The plugin writes whole-minute steps; an app's input_html can set a finer one. + %w[1 any].each do |step| + it "keeps seconds when the input's step is #{step}", js: true do + page.execute_script("document.querySelectorAll('[data-datetimepicker-range-input]').forEach(input => { input.step = '#{step}' })") + filter = filters.filter(:created_at) + filter.fill_in_range(from: Time.zone.parse('2024-01-20 10:00:15'), to: '2024-01-20 12:00:00') + + expect(find('#q_created_at_gteq_datetime_picker').value).to eq '2024-01-20T10:00:15' + expect(find('#q_created_at_lteq_datetime_picker').value).to start_with '2024-01-20T12:00' + expect(filter.from).to eq Time.zone.parse('2024-01-20 10:00:15') + expect(filter.to).to eq Time.zone.parse('2024-01-20 12:00') + expect(filter).to have_range('2024-01-20 10:00:15', '2024-01-20 12:00') + expect(filter).to have_no_range('2024-01-20 10:00', '2024-01-20 12:00', wait: 0) + end + end + end +end