konacha 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.gitignore +19 -0
  2. data/Gemfile +7 -0
  3. data/LICENSE +90 -0
  4. data/README.md +199 -0
  5. data/Rakefile +8 -0
  6. data/app/controllers/konacha/specs_controller.rb +22 -0
  7. data/app/models/konacha/spec.rb +25 -0
  8. data/app/views/konacha/specs/index.html.erb +1 -0
  9. data/app/views/konacha/specs/show.html.erb +1 -0
  10. data/app/views/layouts/konacha/specs.html.erb +31 -0
  11. data/config/routes.rb +4 -0
  12. data/konacha.gemspec +30 -0
  13. data/lib/konacha.rb +41 -0
  14. data/lib/konacha/engine.rb +31 -0
  15. data/lib/konacha/runner.rb +151 -0
  16. data/lib/konacha/server.rb +7 -0
  17. data/lib/tasks/konacha.rake +11 -0
  18. data/spec/controllers/specs_controller_spec.rb +46 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  20. data/spec/dummy/app/assets/javascripts/array_sum.js.coffee +4 -0
  21. data/spec/dummy/config.ru +4 -0
  22. data/spec/dummy/config/application.rb +17 -0
  23. data/spec/dummy/config/boot.rb +6 -0
  24. data/spec/dummy/config/environment.rb +5 -0
  25. data/spec/dummy/config/environments/test.rb +29 -0
  26. data/spec/dummy/config/initializers/konacha.rb +3 -0
  27. data/spec/dummy/spec/javascripts/array_sum_cs_spec.js.coffee +8 -0
  28. data/spec/dummy/spec/javascripts/array_sum_js_spec.js +11 -0
  29. data/spec/dummy/spec/javascripts/assert_spec.js.coffee +9 -0
  30. data/spec/dummy/spec/javascripts/failing_spec.js +5 -0
  31. data/spec/dummy/spec/javascripts/spec_helper.js +3 -0
  32. data/spec/dummy/spec/javascripts/spec_helper_spec.js +7 -0
  33. data/spec/dummy/spec/javascripts/subdirectory/subdirectory_spec.js +5 -0
  34. data/spec/dummy/spec/javascripts/templates/hello.jst.ejs +1 -0
  35. data/spec/dummy/spec/javascripts/templating_spec.js +8 -0
  36. data/spec/dummy/spec/javascripts/transactions_spec.js.coffee +7 -0
  37. data/spec/konacha_spec.rb +40 -0
  38. data/spec/models/spec_spec.rb +45 -0
  39. data/spec/runner_spec.rb +36 -0
  40. data/spec/server_spec.rb +41 -0
  41. data/spec/spec_helper.rb +18 -0
  42. data/spec/views/specs/index.html.erb_spec.rb +12 -0
  43. data/spec/views/specs/show.html.erb_spec.rb +10 -0
  44. data/spec/views/specs/specs.html.erb_spec.rb +15 -0
  45. data/vendor/assets/images/konacha.jpg +0 -0
  46. data/vendor/assets/javascripts/chai.js +2007 -0
  47. data/vendor/assets/javascripts/konacha/runner.js +70 -0
  48. data/vendor/assets/javascripts/konacha/server.js +0 -0
  49. data/vendor/assets/javascripts/mocha.js +3290 -0
  50. data/vendor/assets/stylesheets/mocha.css +133 -0
  51. metadata +203 -0
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ node_modules
19
+ spec/dummy/log
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ # This is a runtime dependency in the gemspec, so it shouldn't be necessary to
5
+ # specify it again, but bundler is buggy:
6
+ # https://github.com/carlhuda/bundler/issues/1041
7
+ gem "jquery-rails"
data/LICENSE ADDED
@@ -0,0 +1,90 @@
1
+ Copyright (c) 2012 John Firebaugh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ Portions derived from Evergreen (https://github.com/jnicklas/evergreen):
25
+
26
+ Copyright (c) 2009 Jonas Nicklas
27
+
28
+ Permission is hereby granted, free of charge, to any person obtaining
29
+ a copy of this software and associated documentation files (the
30
+ 'Software'), to deal in the Software without restriction, including
31
+ without limitation the rights to use, copy, modify, merge, publish,
32
+ distribute, sublicense, and/or sell copies of the Software, and to
33
+ permit persons to whom the Software is furnished to do so, subject to
34
+ the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be
37
+ included in all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
40
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
43
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46
+
47
+ Portions derived from Mocha (https://github.com/visionmedia/mocha)
48
+
49
+ Copyright (c) 20011-2012 TJ Holowaychuk <tj@vision-media.ca>
50
+
51
+ Permission is hereby granted, free of charge, to any person obtaining
52
+ a copy of this software and associated documentation files (the
53
+ 'Software'), to deal in the Software without restriction, including
54
+ without limitation the rights to use, copy, modify, merge, publish,
55
+ distribute, sublicense, and/or sell copies of the Software, and to
56
+ permit persons to whom the Software is furnished to do so, subject to
57
+ the following conditions:
58
+
59
+ The above copyright notice and this permission notice shall be
60
+ included in all copies or substantial portions of the Software.
61
+
62
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
63
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
64
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
65
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
66
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
67
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
68
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
69
+
70
+ Portions derived from Chai (https://github.com/logicalparadox/chai)
71
+
72
+ Copyright (c) 2011 Jake Luer <jake@alogicalparadox.com>
73
+
74
+ Permission is hereby granted, free of charge, to any person obtaining a copy
75
+ of this software and associated documentation files (the "Software"), to deal
76
+ in the Software without restriction, including without limitation the rights
77
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78
+ copies of the Software, and to permit persons to whom the Software is
79
+ furnished to do so, subject to the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be included in
82
+ all copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
87
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
88
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
89
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
90
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,199 @@
1
+ # Konacha
2
+
3
+ Konacha is a Rails engine that allows you to test your JavaScript with the
4
+ [mocha](http://visionmedia.github.com/mocha/) test framework and [chai](http://chaijs.com/)
5
+ assertion library.
6
+
7
+ [![Konacha][2]][1]
8
+
9
+ [1]: http://en.wikipedia.org/wiki/Konacha_tea
10
+ [2]: vendor/assets/images/konacha.jpg
11
+
12
+ It is similar to [Jasmine](https://github.com/pivotal/jasmine-gem) and
13
+ [Evergreen](https://github.com/jnicklas/evergreen), but does not attempt to be framework
14
+ agnostic. By sticking with Rails, Konacha can take full advantage of features such as
15
+ the asset pipeline and engines.
16
+
17
+ ## Installation
18
+
19
+ Add konacha to the `:test` and `:development` groups in the Gemfile:
20
+
21
+ group :test, :development do
22
+ gem "konacha"
23
+ end
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install konacha
32
+
33
+ ## Usage
34
+
35
+ Create a `spec/javascripts` directory and name the files in it with a `_spec` suffix.
36
+ You can write the specs in either JavaScript or CoffeeScript, using a `.js` or
37
+ `.js.coffee` extension respectively, like you would any other script asset.
38
+
39
+ Require the assets under test and any other dependencies using Sprockets directives.
40
+ For example, suppose you wanted to test your cool JavaScript `Array#sum` method, which
41
+ you placed in `app/assets/javascripts/array_sum.js`. Write the specs in JavaScript in
42
+ the file `spec/javascripts/array_sum_spec.js`:
43
+
44
+ //= require array_sum
45
+
46
+ describe("Array#sum", function(){
47
+ it("returns 0 when the Array is empty", function(){
48
+ [].sum().should.equal(0);
49
+ });
50
+
51
+ it("returns the sum of numeric elements", function(){
52
+ [1,2,3].sum().should.equal(6);
53
+ });
54
+ });
55
+
56
+ Or, if you prefer CoffeeScript, in `spec/javascripts/array_sum_spec.js.coffee`:
57
+
58
+ #= require array_sum
59
+
60
+ describe "Array#sum", ->
61
+ it "returns 0 when the Array is empty", ->
62
+ [].sum().should.equal(0)
63
+
64
+ it "returns the sum of numeric elements", ->
65
+ [1,2,3].sum().should.equal(6)
66
+
67
+ The `konacha:server` rake task starts a server for your tests. You can go to the root
68
+ page to run all specs (e.g. `http://localhost:8888/`), or a sub page to run an individual
69
+ spec file (e.g. `http://localhost:8888/array_sum_spec`).
70
+
71
+ Alternatively, you can run the specs headlessly with the `konacha:ci` task.
72
+
73
+ ## Spec Helper
74
+
75
+ Since Konacha integrates with the asset pipeline, using setup helpers in your specs is
76
+ easy. Just create a `spec_helper.js` or `spec_helper.js.coffee` file in `specs/javascripts`
77
+ and require it in your tests:
78
+
79
+ //= require spec_helper
80
+ //= require array_sum
81
+
82
+ describe("Array#sum", function(){
83
+ ...
84
+ });
85
+
86
+ ## Directives and Asset Bundling
87
+
88
+ We suggest that you explicitly require just the assets necessary for each spec. In CI
89
+ mode, Konacha will run each spec in isolation, and requiring things explicitly will help
90
+ ensure your scripts don't accumulate hidden dependencies and tight coupling.
91
+
92
+ However, you are free to ignore this advice and require the entire application.js asset
93
+ bundle in your specs or spec helper, or a bundled subset of assets. Requiring bundled
94
+ assets works like it does in Rails development mode -- Konacha will detect the complete
95
+ set of dependencies and generate a separate script tag for each one. You won't have to
96
+ search through a many thousand line application.js bundle to debug a spec failure.
97
+
98
+ ## Configuration
99
+
100
+ Konacha can be configured in an initializer, e.g. `config/initializers/konacha.rb`:
101
+
102
+ Konacha.configure do |config|
103
+ config.spec_dir = "spec/javascripts"
104
+ config.interface = :bdd
105
+ config.driver = :selenium
106
+ end if defined?(Konacha)
107
+
108
+ The `defined?` check is necessary to avoid a dependency on Konacha in the production
109
+ environment.
110
+
111
+ The `spec_dir` option tells Konacha where to find JavaScript specs. The `interface`
112
+ option specifies the test interface used by Mocha (see below). `driver` names a
113
+ Capybara driver used for the CI task (try `:webkit`, after installing
114
+ [capybara-webkit](https://github.com/thoughtbot/capybara-webkit)).
115
+
116
+ The values above are the defaults.
117
+
118
+ ## Test Interface and Assertions
119
+
120
+ Konacha includes a vendored copy of mocha.js and the [chai](http://chaijs.com/)
121
+ assertion libraries.
122
+
123
+ By default, it will assume that you want to use Mocha's "BDD" test interface, which
124
+ provides `describe()`, `it()`, `before()`, `after()`, `beforeEach()`, and `afterEach()`.
125
+ If you want to use the TDD, Exports, or QUnit interfaces instead, set the `interface`
126
+ configuration option in an initializer:
127
+
128
+ Konacha.configure do |config|
129
+ config.interface = :tdd # Or :exports or :qunit
130
+ end if defined?(Konacha)
131
+
132
+ Konacha will make all three of chai's assertion styles available to you: `expect`,
133
+ `should`, and `assert`. See the chai documentation for the details.
134
+
135
+ If you use jQuery, you may want to check out [chai-jquery](https://github.com/jfirebaugh/chai-jquery)
136
+ for some jQuery-specific assertions.
137
+
138
+ ## Transactions
139
+
140
+ One problem often faced when writing unit tests for client side code is that changes
141
+ to the page are not reverted for the next example, so that successive examples become
142
+ dependent on each other. Konacha adds a special div to your page with an id of `test`.
143
+ This div is automatically emptied before each example. You should avoid appending markup
144
+ to the page body and instead append it to this test div:
145
+
146
+ describe "transactions", ->
147
+ it "should add stuff in one test...", ->
148
+ $('#test').append('<h1 id="added">New Stuff</h1>')
149
+ $('#test h1#added').length.should.equal(1)
150
+
151
+ it "... should have been removed before the next starts", ->
152
+ $('#test h1#added').length.should.equal(0)
153
+
154
+ Note: this functionality is available only for the "BDD" (default) and "TDD" mocha interfaces,
155
+ and not for the "exports" or "QUnit" interfaces.
156
+
157
+ ## Templates / Fixtures
158
+
159
+ Konacha has no template (a.k.a. HTML fixture) support of its own. Instead, we suggest you use
160
+ Sprocket's built in support for JavaScript template (`.jst`) files. Add a `spec/javascripts/templates`
161
+ directory, place template files there (using any JS template language supported by Sprockets),
162
+ require them in your spec or spec_helper, and render them into the `#test` div.
163
+
164
+ For example, in `spec/javascripts/templates/hello.jst.ejs`:
165
+
166
+ <h1>Hello Konacha!</h1>
167
+
168
+ In `spec_helper.js`:
169
+
170
+ //= require_tree ./templates
171
+
172
+ And your spec:
173
+
174
+ //= require spec_helper
175
+
176
+ describe("templating", function(){
177
+ it("is built in to Sprockets", function(){
178
+ $('#test').html(JST['templates/hello']());
179
+ $('#test h1').text().should.equal('Hello Konacha!');
180
+ });
181
+ });
182
+
183
+ ## Contributing
184
+
185
+ 1. Fork it
186
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
187
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
188
+ 4. Push to the branch (`git push origin my-new-feature`)
189
+ 5. Create new Pull Request
190
+
191
+ ## License
192
+
193
+ Copyright (c) 2012 John Firebaugh
194
+
195
+ MIT License (see the LICENSE file)
196
+
197
+ Portions: Copyright (c) 2009 Jonas Nicklas, Copyright (c) 20011-2012 TJ Holowaychuk
198
+ <tj@vision-media.ca>, Copyright (c) 2011 Jake Luer <jake@alogicalparadox.com>. See
199
+ LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new :spec
7
+
8
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ module Konacha
2
+ class SpecsController < ActionController::Base
3
+ before_filter :set_interface, :set_mode
4
+
5
+ def set_interface
6
+ @interface = Konacha.interface
7
+ end
8
+
9
+ def set_mode
10
+ @mode = Konacha.mode
11
+ end
12
+
13
+ def index
14
+ @specs = Konacha::Spec.all
15
+ end
16
+
17
+ def show
18
+ @spec = Konacha::Spec.find(params[:spec])
19
+ @spec or render :text => "Not Found", :status => 404
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ module Konacha
2
+ class Spec
3
+ def self.all
4
+ Konacha.spec_paths.map { |path| new(path) }
5
+ end
6
+
7
+ def self.find(basename)
8
+ all.find { |spec| spec.basename == basename }
9
+ end
10
+
11
+ attr_accessor :path
12
+
13
+ def initialize(path)
14
+ @path = path
15
+ end
16
+
17
+ def url
18
+ "/#{basename}"
19
+ end
20
+
21
+ def basename
22
+ path[/.*(?=\.js.*$)/]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1 @@
1
+ <%= javascript_include_tag *@specs.map(&:basename) %>
@@ -0,0 +1 @@
1
+ <%= javascript_include_tag @spec.basename %>
@@ -0,0 +1,31 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html;charset=utf-8" />
5
+ <title>Mocha Tests</title>
6
+ <%= stylesheet_link_tag "mocha" %>
7
+ <%= javascript_include_tag "jquery", "mocha", "chai", "konacha/#{@mode}" %>
8
+ <script>
9
+ mocha.setup(<%= raw @interface.to_json %>);
10
+ var expect = chai.expect,
11
+ should = chai.should(),
12
+ assert = chai.assert;
13
+ window.onload = mocha.run;
14
+
15
+ <% if @interface == :bdd %>
16
+ beforeEach(function() {
17
+ document.getElementById('test').innerHTML = "";
18
+ });
19
+ <% elsif @interface == :tdd %>
20
+ setup(function() {
21
+ document.getElementById('test').innerHTML = "";
22
+ });
23
+ <% end %>
24
+ </script>
25
+ <%= yield %>
26
+ </head>
27
+ <body>
28
+ <div id="mocha"></div>
29
+ <div id="test"></div>
30
+ </body>
31
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Konacha::Engine.routes.draw do
2
+ match "/" => "konacha/specs#index"
3
+ match "/*spec" => "konacha/specs#show"
4
+ end
data/konacha.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["John Firebaugh"]
5
+ gem.email = ["john.firebaugh@gmail.com"]
6
+ gem.description = %q{Unit test your Rails JavaScript with the mocha test framework and chai assertion library}
7
+ gem.summary = %q{Konacha is a Rails engine that allows you to test your JavaScript with the
8
+ mocha test framework and chai assertion library.
9
+
10
+ It is similar to Jasmine and Evergreen, but does not attempt to be framework
11
+ agnostic. By sticking with Rails, Konacha can take full advantage of features such as
12
+ the asset pipeline and engines.}
13
+ gem.homepage = "http://github.com/jfirebaugh/konacha"
14
+
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ gem.name = "konacha"
19
+ gem.require_paths = ["lib"]
20
+ gem.version = "0.9.0"
21
+
22
+ gem.add_dependency "jquery-rails"
23
+ gem.add_dependency "rails", "~> 3.1"
24
+ gem.add_dependency "capybara"
25
+
26
+ gem.add_development_dependency "rspec-rails"
27
+ gem.add_development_dependency "capybara-firebug", "~> 1.1"
28
+ gem.add_development_dependency "coffee-script"
29
+ gem.add_development_dependency "ejs"
30
+ end