Class: BootstrapDatepickerSpec::DatePicker

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/bootstrap_datepicker_spec.rb

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ DatePicker

Returns a new instance of DatePicker.



8
9
10
11
12
13
14
15
# File 'lib/bootstrap_datepicker_spec.rb', line 8

def initialize(node)
  @node = super(node)
  expected_class = Capybara::Node::Element

  unless @node.is_a? expected_class
    raise "Expecting node of type: #{expected_class}, but received node of type #{@node.class} "
  end
end

Instance Method Details

#date_stringObject



59
60
61
# File 'lib/bootstrap_datepicker_spec.rb', line 59

def date_string
  @node[:value]
end

#select_date(value_string) ⇒ Object



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
# File 'lib/bootstrap_datepicker_spec.rb', line 17

def select_date(value_string)
  value = Date.parse(value_string)
  @node.click

  page = @node.send(:session)

  picker = page.find('.datepicker')

  picker_years = picker.find('.datepicker-years', visible: false)
  picker_months = picker.find('.datepicker-months', visible: false)
  picker_days = picker.find('.datepicker-days', visible: false)

  picker_current_decade = picker_years.find('th.datepicker-switch', visible: false)
  picker_current_year = picker_months.find('th.datepicker-switch', visible: false)
  picker_current_month = picker_days.find('th.datepicker-switch', visible: false)

  picker_current_month.trigger("click") if picker_days.visible?
  picker_current_year.trigger("click") if picker_months.visible?

  decade_start, decade_end = picker_current_decade.text.split('-').map(&:to_i)

  if value.year < decade_start.to_i
    gap = decade_start / 10 - value.year / 10
    gap.times { picker_years.find('th.prev').trigger("click") }
  elsif value.year > decade_end
    gap = value.year / 10 - decade_end / 10
    gap.times { picker_years.find('th.next').trigger("click") }
  end

  picker_years.find('.year', text: value.year).trigger("click")
  picker_months.find('.month', text: value.strftime('%b')).trigger("click")
  day_xpath = "      .//*[contains(concat(' ', @class, ' '), ' day ')\n      and not(contains(concat(' ', @class, ' '), ' old '))\n      and not(contains(concat(' ', @class, ' '), ' new '))\n      and normalize-space(text())='\#{value.day}']\n  eos\n  picker_days.find(:xpath, day_xpath).trigger :click\n\n  # fail unless page.has_no_css? '.datepicker'\nend\n"