Module: SimpleJavascriptTesting

Defined in:
lib/simple_javascript_testing.rb

Instance Method Summary collapse

Instance Method Details

#call_template_with_js(template, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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