Repository navigation
Add page objects for the datetime range filter #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
115 changes: 115 additions & 0 deletions
115
lib/active_admin_datetimepicker/page_objects/date_time_range_filter.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Time, nil>] #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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.