simple_javascript_testing 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35ce285f684371f303456c73f2646a46d244cf15
4
- data.tar.gz: 862f64dab962ca84b955d0c2e08e2bf9a3b1d7a7
3
+ metadata.gz: 3c971bc18303bd8ba9c5156255d8f19d69d2e7be
4
+ data.tar.gz: 6dd7d1d54630dccc74c7cafdd51f0150c2890466
5
5
  SHA512:
6
- metadata.gz: 1b9855c6e332397017de35bf9eb17882695d133f0dbd82a4a125d751e7daa403ab273b30850f49613a4dd2af59e325f4d88a4cd9ab1c3b9b2377b27fecd3b973
7
- data.tar.gz: bd9251f3c8e1faf8190da20c5f7fa8d75ca7a2e9c51806ad1f04b36a94794f5e5af7d365d57654b95fb81d68e366b1a8876a2530c6853446a0fe199e587dd0a8
6
+ metadata.gz: 2f1ce314f9ebc5a415e7ebedd24ad30bce78ea1c52f03a0165b2722353c6aba09a6f834e77e48afab45651adfa38f3c7e0a8ee13c9d2d7981b296c73d95b39b7
7
+ data.tar.gz: f5cf13f3421daafd2a86845abd455408c6db1fa9d25964bf026c514244c9a51babd328d0d09c584ba5d01af983757821eba0d1e5b048b6ed57c522188672acd6
data/.gitignore CHANGED
@@ -2,3 +2,6 @@ npm-debug.log
2
2
  log
3
3
  *.swp
4
4
  *.gem
5
+ node_modules
6
+ mkmf.log
7
+ test/html/*
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_javascript_testing (0.0.1)
4
+ simple_javascript_testing (0.0.4)
5
5
  rails (>= 3.0.7)
6
+ render_anywhere
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,64 @@
1
- simple_javascript_testing
1
+ Simple Javascript Testing
2
2
  =========================
3
3
 
4
- simple javascript testing with rails and phantomjs
4
+ Simple javascript testing for rails templates with phantomjs.
5
+
6
+ This library adds the method call_template_with_js to your tests, if you include SimpleJavascriptTesting.
7
+ This method builds the template you specify (ie users/index) and runs the javascript test file (test/javascript/users/index.js) against it. It stubs out any AJAX calls.
8
+
9
+ example ruby test test/phantomjs/template_test.rb:
10
+ ```ruby
11
+
12
+ require 'test_helper'
13
+ require 'simple_javascript_testing'
14
+
15
+ class TemplateTest < ActiveSupport::TestCase
16
+ include SimpleJavascriptTesting
17
+
18
+ def test_users_index
19
+ call_template_with_js "users/index"
20
+ end
21
+
22
+ def test_dwarves_show
23
+ civilization = Civilization.find_or_create_by :id => 100
24
+ dwarf = civilization.dwarves.find_or_create_by :id => 10
25
+ call_template_with_js "dwarves/show" do |data|
26
+ data.set_instance_variable "dwarf", dwarf
27
+ end
28
+ end
29
+ end
30
+ ```
31
+
32
+ and example js test file test/javascript/users/index.js:
33
+ ```js
34
+ var simple = require('simple_javascript_testing')
35
+
36
+ simple.runTest("test file", function(page) {
37
+ var checked = page.evaluate(function() {
38
+ triggerRequest("/df/artery/user.json", {Id: 1, Name: "Hey", Thoughts: "hey"})
39
+ triggerRequest("/df/artery/user/get_unit_labors.json", [{Labor: 1}])
40
+ assertAllRequestsRun()
41
+ return document.querySelector('#labor_1').checked
42
+ })
43
+ this.assert_equal(true, checked)
44
+ })
45
+
46
+ ```
47
+ triggerRequest will manually trigger the callback for an AJAX request with the response you specify.
48
+ page is the phantomjs object
49
+
50
+ Install
51
+ ====
52
+ To run, install the gem and the npm module into your project
53
+
54
+ `gem install simple_javascript_testing`
55
+
56
+ `npm install simple_javascript_testing`
57
+
58
+ and add simple_javascript_testing to your Gemfile
59
+
60
+
61
+ To run the tests:
62
+
63
+ npm install --force simple_javascript_testing
64
+ ruby test/simple_javascript_testing_test.rb
data/index.js CHANGED
@@ -40,6 +40,7 @@ var testObject = {
40
40
  assert_equal: function(a, b){
41
41
  if (a != b){
42
42
  console.log("assertion failed:", a, "not equal to", b)
43
+ phantom.exit(-1)
43
44
  }
44
45
  }
45
46
  }
@@ -48,7 +49,7 @@ exports.runTest = function(name, test){
48
49
  page.open("file:///" + args[1], function(status){
49
50
  try{
50
51
  test.bind(testObject)(page)
51
- console.log("test succeeded: " + name)
52
+ //console.log("test succeeded: " + name)
52
53
  } catch(e){
53
54
  console.log(e.stack)
54
55
  console.log("test failed: " + name)
@@ -7,6 +7,14 @@ class AnyClass
7
7
 
8
8
  class << self
9
9
  attr_accessor :prefix
10
+
11
+ def test_directory
12
+ @test_directory || "test"
13
+ end
14
+
15
+ def test_directory=(dir)
16
+ @test_directory = dir
17
+ end
10
18
  end
11
19
 
12
20
  def build_html(template)
@@ -43,16 +51,34 @@ module SimpleJavascriptTesting
43
51
  FileUtils.mkdir_p("test/html/#{File.dirname template}")
44
52
  FileUtils.mkdir_p("test/javascript/#{File.dirname template}")
45
53
  html = data.build_html(template)
46
- html.gsub!("<head>", "<head><script src='#{File.expand_path('node_modules')}/simple_javascript_testing/lib/stub_ajax.js'></script>")
47
- html.gsub!("<script src='#{AnyClass.prefix}", "<script src='#{File.expand_path('public')}")
54
+ run_javascript_test(html, template)
55
+ end
56
+
57
+ def run_javascript_test(html, template)
58
+ html.gsub! /<script src=\"#{AnyClass.prefix}([^\"]*)\"/ do |full|
59
+ #puts $1
60
+ #puts File.expand_path("public/assets/#{$1}")
61
+ if File.exists?(File.expand_path("public/assets/#{$1}"))
62
+ "<script src=\"#{File.expand_path("public/assets/#{$1}")}\""
63
+ elsif File.exists?(File.expand_path("vendor/assets/javascripts/#{$1}"))
64
+ "<script src=\"#{File.expand_path("vendor/assets/javascripts/#{$1}")}\""
65
+ else
66
+ full
67
+ end
68
+ end
69
+ #html.gsub!("<script src=\"#{AnyClass.prefix}", "<script src=\"#{File.expand_path('public/assets')}")
70
+ html.gsub!("</head>", "<script src='#{File.expand_path('node_modules')}/simple_javascript_testing/lib/stub_ajax.js'></script></head>")
48
71
  File.write("test/html/#{template}.html", html)
49
- thing = "#{File.expand_path('html', 'test')}/#{template}"
50
- thing2 = "#{File.expand_path('javascript', 'test')}/#{template}"
72
+ thing = "#{File.expand_path('html', AnyClass.test_directory)}/#{template}"
73
+ thing2 = "#{File.expand_path('javascript', AnyClass.test_directory)}/#{template}"
51
74
  binary = if find_executable 'phantomjs'
52
75
  "phantomjs"
53
76
  else
54
77
  "node_modules/simple_javascript_testing/node_modules/phantomjs/lib/phantom/bin/phantomjs"
55
78
  end
56
- system "#{binary} #{thing2}.js #{thing}.html"
79
+ result = system "#{binary} #{thing2}.js #{thing}.html"
80
+ if !result
81
+ raise "Javascript test failed"
82
+ end
57
83
  end
58
84
  end
@@ -21,3 +21,35 @@ function assertAllRequestsRun(){
21
21
  }
22
22
 
23
23
  XMLHttpRequest.prototype.send = function(){ }
24
+
25
+ function triggerRequest(url, data){
26
+ var request = all_requests[url]
27
+ all_requests_length -= 1
28
+ request.response = JSON.stringify(data)
29
+ request.status = 200
30
+ request.onload(request)
31
+ }
32
+
33
+ MockChannel = function(name){
34
+ this.initialize(name)
35
+ }
36
+
37
+ var all_channels = {}
38
+ MockChannel.prototype = {
39
+ events: {},
40
+ initialize: function(name){
41
+ this.name = name
42
+ },
43
+ bind: function(event, callback){
44
+ this.events[event] = callback
45
+ },
46
+ triggerEvent: function(event, data){
47
+ this.events[event](data)
48
+ }
49
+ }
50
+
51
+ WebSocketRails.prototype.subscribe = function(channel){
52
+ var new_channel = new MockChannel("channel")
53
+ all_channels[channel] = new_channel
54
+ return new_channel
55
+ }
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "simple_javascript_testing",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "simple javascript testing for rails",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
9
- "dependencies" : {
10
- "phantomjs": "latest"
11
- },
12
9
  "repository": {
13
10
  "type": "git",
14
11
  "url": "https://github.com/towski/simple_javascript_testing.git"
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'simple_javascript_testing'
3
- s.version = '0.0.3'
3
+ s.version = '0.0.4'
4
4
  s.date = '2014-10-31'
5
5
  s.summary = "Simple Javascript Testing"
6
6
  s.description = "simple javascript testing for rails with phantomjs"
@@ -0,0 +1,11 @@
1
+ var simple = require('simple_javascript_testing')
2
+
3
+ simple.runTest("test file", function(page) {
4
+ var checked = page.evaluate(function() {
5
+ //triggerRequest("/df/artery/user.json", {Id: 1, Name: "Hey", Thoughts: "hey"})
6
+ //triggerRequest("/df/artery/user/get_unit_labors.json", [{Labor: 1}])
7
+ //assertAllRequestsRun()
8
+ //return document.querySelector('#labor_1').checked
9
+ })
10
+ this.assert_equal(true, true)
11
+ })
@@ -30,6 +30,6 @@ end
30
30
  class SimpleJavascriptTestingTest < Minitest::Test
31
31
  include SimpleJavascriptTesting
32
32
  def test_call_template_with_js
33
- call_template_with_js "users/index"
33
+ run_javascript_test "<html><head></head></html>", "users/index"
34
34
  end
35
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_javascript_testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - towski
@@ -55,7 +55,6 @@ files:
55
55
  - lib/stub_ajax.js
56
56
  - package.json
57
57
  - simple_javascript_testing.gemspec
58
- - test/html/users/index.html
59
58
  - test/javascript/users/index.js
60
59
  - test/simple_javascript_testing_test.rb
61
60
  homepage: http://rubygems.org/gems/simple_javascript_testing
File without changes