module SimpleJavascriptTesting

Public Instance Methods

call_template_with_js(template) { |data| ... } click to toggle source
# File lib/simple_javascript_testing.rb, line 46
def call_template_with_js(template, &block)
  data = AnyClass.new
  if block_given?
    yield data
  end
  FileUtils.mkdir_p("test/html/#{File.dirname template}")
  FileUtils.mkdir_p("test/javascript/#{File.dirname template}")
  html = data.build_html(template)
  run_javascript_test(html, template)
end
run_javascript_test(html, template) click to toggle source
# File lib/simple_javascript_testing.rb, line 57
def run_javascript_test(html, template)
  html.gsub! /<script src=\"#{AnyClass.prefix}([^\"]*)\"/ do |full|
    #puts $1
    #puts File.expand_path("public/assets/#{$1}")
    if File.exists?(File.expand_path("public/assets/#{$1}"))
      "<script src=\"#{File.expand_path("public/assets/#{$1}")}\""
    elsif File.exists?(File.expand_path("vendor/assets/javascripts/#{$1}"))
      "<script src=\"#{File.expand_path("vendor/assets/javascripts/#{$1}")}\""
    else
      full
    end
  end
  #html.gsub!("<script src=\"#{AnyClass.prefix}", "<script src=\"#{File.expand_path('public/assets')}")
  html.gsub!("</head>", "<script src='#{File.expand_path('node_modules')}/simple_javascript_testing/lib/stub_ajax.js'></script></head>")
  File.write("test/html/#{template}.html", html)
  thing = "#{File.expand_path('html', AnyClass.test_directory)}/#{template}"
  thing2 = "#{File.expand_path('javascript', AnyClass.test_directory)}/#{template}"
  binary = if find_executable 'phantomjs'
    "phantomjs"
  else
    "node_modules/simple_javascript_testing/node_modules/phantomjs/lib/phantom/bin/phantomjs"
  end
  result = system "#{binary} #{thing2}.js #{thing}.html"
  if !result
    raise "Javascript test failed"
  end
end