class Lolex

Wrap the Lolex js package

Interface to the Lolex package running on the client side Below we will monkey patch Timecop to call these methods

Public Class Methods

create_ticker() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 50
def create_ticker
  return unless @scale && @scale > 0
  ticker = %x{
    #{@lolex}['_setInterval'].call(
      window,
      function() { #{tick} },
      #{@resolution}
    )
  }
  ticker
end
init(page, client_time_zone, resolution) click to toggle source
# File lib/hyper-spec/time_cop.rb, line 90
def init(page, client_time_zone, resolution)
  @capybara_page = page
  @resolution = resolution || 10
  @client_time_zone = client_time_zone
  run_pending_evaluations
  @initialized = true
end
initialized?() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 98
def initialized?
  @initialized
end
parse_time(str) click to toggle source

to avoid forcing us to require time we make our own Time.parse method copied from opal.rb master branch

# File lib/hyper-spec/time_cop.rb, line 9
def parse_time(str)
  `new Date(Date.parse(str))`
end
pop() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 23
def pop
  update_lolex(*stack.pop) unless stack.empty?
end
push(time, scale = 1, resolution = 10) click to toggle source
# File lib/hyper-spec/time_cop.rb, line 17
def push(time, scale = 1, resolution = 10)
  time = parse_time(time) if time.is_a? String
  stack << [Time.now, @scale, @resolution]
  update_lolex(time, scale, resolution)
end
restore() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 33
def restore
  @stack = @backup_stack
  pop
end
stack() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 13
def stack
  @stack ||= []
end
tick() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 38
def tick
  real_clock = `(new #{@lolex}['_Date']).getTime()`
  mock_clock = Time.now.to_f * 1000
  real_elapsed_time = real_clock - @real_start_time
  mock_elapsed_time = mock_clock - @mock_start_time

  ticks = real_elapsed_time * @scale - mock_elapsed_time

  `#{@lolex}.tick(#{ticks.to_i})`
  nil
end
unmock(time, resolution) click to toggle source
# File lib/hyper-spec/time_cop.rb, line 27
def unmock(time, resolution)
  push(time, 1, resolution)
  @backup_stack = stack
  @stack = []
end
update_lolex(time, scale, resolution) click to toggle source
# File lib/hyper-spec/time_cop.rb, line 62
def update_lolex(time, scale, resolution)
  `#{@lolex}.uninstall()` && return if scale.nil?
  @mock_start_time = time.to_f * 1000

  if @lolex
    `#{@lolex}['_clearInterval'].call(window, #{@ticker})` if @ticker
    @real_start_time = `(new #{@lolex}['_Date']).getTime()`
    `#{@lolex}.tick(#{@mock_start_time - Time.now.to_f * 1000})`
  else
    @real_start_time = Time.now.to_f * 1000
    @lolex = `lolex.install({ now: #{@mock_start_time} })`
  end

  @scale = scale
  @resolution = resolution
  @ticker = create_ticker
  nil # must return nil otherwise we try to return a timer to server!
end

Private Class Methods

evaluate_ruby() { || ... } click to toggle source
# File lib/hyper-spec/time_cop.rb, line 137
def evaluate_ruby(&block)
  if @capybara_page
    @capybara_page.evaluate_ruby(yield)
  else
    pending_evaluations << block
  end
end
pending_evaluations() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 133
def pending_evaluations
  @pending_evaluations ||= []
end
run_pending_evaluations() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 145
def run_pending_evaluations
  return if pending_evaluations.empty?
  @capybara_page.evaluate_ruby(pending_evaluations.collect(&:call).join("\n"))
  @pending_evaluations ||= []
end
time_string_in_zone() click to toggle source
# File lib/hyper-spec/time_cop.rb, line 129
def time_string_in_zone
  Time.now.in_time_zone(@client_time_zone).strftime('%Y/%m/%d %H:%M:%S %z')
end