chai-backbone-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chai-backbone-rails.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Matthijs Groen
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.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Backbone::Chai
2
+
3
+ Some matchers to help testing backbone structures.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'chai-backbone-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chai-backbone-rails
18
+
19
+ ## Usage
20
+
21
+ #= require chai-backbone
22
+
23
+ ### Triggers
24
+
25
+ model.should.trigger("change", with: [model]).when -> model.set attribute: "value"
26
+
27
+ this can also be chained further:
28
+
29
+ model.should.trigger("change").and.trigger("change:attribute").when -> model.set attribute: "value"
30
+ model.should.trigger("change").and.not.trigger("reset").when -> model.set attribute: "value"
31
+
32
+ ### Routing (will be ported soon!)
33
+
34
+ "page/3".should.route_to myRouter, "openPage", arguments: ["3"]
35
+ "page/3".should.route_to myRouter, "openPage", considering: [conflictingRouter]
36
+
37
+ ### Sinon (will be ported soon!)
38
+
39
+ Matchers have also been added for sinonjs.
40
+
41
+ #= require sinon-chai
42
+ chai.use sinonChai
43
+
44
+ These are not complete yet, see tests and code for details.
45
+
46
+ spy.should.have.been.called.exactly(x).times
47
+ spy.should.have.been.called.before otherSpy
48
+ spy.should.have.been.called.after otherSpy
49
+ spy.should.have.been.called.with "argument1", 2, "argument3"
50
+ spy.should.have.been.not_called
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/chai-backbone-rails/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Matthijs Groen"]
6
+ gem.email = ["matthijs.groen@gmail.com"]
7
+ gem.description = %q{Chai.js matchers for Backbone.js framework}
8
+ gem.summary = %q{A set of assertion matchers to test Backbone code using Konacha in Rails}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "chai-backbone-rails"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Chai::Backbone::Rails::VERSION
17
+ end
@@ -0,0 +1,10 @@
1
+ require "chai-backbone-rails/version"
2
+
3
+ module Chai
4
+ module Backbone
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Chai
2
+ module Backbone
3
+ module Rails
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,96 @@
1
+ #= require spec_helper
2
+
3
+ describe "Sinon Chai Matchers", ->
4
+
5
+ callback = null
6
+
7
+ beforeEach -> callback = sinon.spy()
8
+
9
+ describe "#not_called", ->
10
+
11
+ it "passes if not called", ->
12
+ expect(-> callback.should.have.been.not_called).to.not.throw()
13
+
14
+ it "raises AsserionError if called", ->
15
+ callback()
16
+ expect(-> callback.should.have.been.not_called).throw(/been called/)
17
+
18
+ describe "called", ->
19
+
20
+ describe "#exactly", ->
21
+ it "raises AssertionError if not called", ->
22
+ expect(-> callback.should.have.been.called.exactly(2).times).to.throw(/been called exactly twice, but it was called 0 times/)
23
+
24
+ it "passes if matches", ->
25
+ callback()
26
+ expect(-> callback.should.have.been.called.exactly(1).time).to.not.throw()
27
+
28
+ it "raises AssertionError if called more than specified", ->
29
+ callback()
30
+ callback()
31
+ callback()
32
+ callback()
33
+ expect(-> callback.should.have.been.called.exactly(3).times).to.throw(/been called exactly thrice, but it was called 4 times/)
34
+
35
+ describe "#before", ->
36
+ second = null
37
+
38
+ beforeEach -> second = sinon.spy()
39
+
40
+ it "raises AssertionError if first not called", ->
41
+ second()
42
+ expect(-> callback.should.have.been.called.before(second)).to.throw(/been called before/)
43
+
44
+ it "passes if second not called", ->
45
+ callback()
46
+ expect(-> callback.should.have.been.called.before(second)).to.not.throw()
47
+
48
+ it "raises AssertionError if order is wrong", ->
49
+ second()
50
+ callback()
51
+ expect(-> callback.should.have.been.called.before(second)).to.throw(/been called before/)
52
+
53
+ it "passes if second if spies are called in correct order", ->
54
+ callback()
55
+ second()
56
+ expect(-> callback.should.have.been.called.before(second)).to.not.throw()
57
+
58
+ describe "#after", ->
59
+ second = null
60
+
61
+ beforeEach -> second = sinon.spy()
62
+
63
+ it "raises AssertionError if first not called", ->
64
+ second()
65
+ expect(-> callback.should.have.been.called.after(second)).to.throw(/been called before/)
66
+
67
+ it "raises AssertionError if second not called", ->
68
+ callback()
69
+ expect(-> callback.should.have.been.called.after(second)).to.throw(/been called before/)
70
+
71
+ it "raises AssertionError if order is wrong", ->
72
+ callback()
73
+ second()
74
+ expect(-> callback.should.have.been.called.after(second)).to.throw(/been called before/)
75
+
76
+ it "passes if second if spies are called in correct order", ->
77
+ second()
78
+ callback()
79
+ expect(-> callback.should.have.been.called.after(second)).to.not.throw()
80
+
81
+ describe "#with", ->
82
+
83
+ it "raises AssertionError if callback not called", ->
84
+ expect(-> callback.should.have.been.called.with(1)).to.throw(/been called with arguments/)
85
+
86
+ it "passes if parameters match exactly", ->
87
+ callback(1)
88
+ expect(-> callback.should.have.been.called.with(1)).to.not.throw()
89
+
90
+ it "raises AssertionError if parameters do not match", ->
91
+ callback(4)
92
+ expect(-> callback.should.have.been.called.with(5)).to.throw(/been called with arguments/)
93
+
94
+ it "passes if more than specified parameters are passed", ->
95
+ callback(1,2,3)
96
+ expect(-> callback.should.have.been.called.with(1,2)).to.not.throw()
@@ -0,0 +1,113 @@
1
+ #= require underscore
2
+
3
+ ((chaiBackbone) ->
4
+ # Module systems magic dance.
5
+ if (typeof require == "function" && typeof exports == "object" && typeof module == "object")
6
+ # NodeJS
7
+ module.exports = chaiBackbone
8
+ else if (typeof define == "function" && define.amd)
9
+ # AMD
10
+ define -> chaiBackbone
11
+ else
12
+ # Other environment (usually <script> tag): plug in to global chai instance directly.
13
+ chai.use chaiBackbone
14
+ )((chai, utils) ->
15
+ inspect = utils.inspect
16
+ flag = utils.flag
17
+
18
+ # Verifies if the subject fires a trigger 'when' events happen
19
+ #
20
+ # Examples:
21
+ # model.should.trigger("change", with: [model]).when -> model.set attribute: "value"
22
+ # model.should.not.trigger("change:thing").when -> model.set attribute: "value"
23
+ # model.should.trigger("change").and.not.trigger("change:thing").when -> model.set attribute: "value"
24
+ #
25
+ # @param trigger the trigger expected to be fired
26
+ chai.Assertion.addMethod 'trigger', (trigger, options = {}) ->
27
+ definedActions = flag(this, 'whenActions') || []
28
+
29
+ # Add a around filter to the when actions
30
+ definedActions.push
31
+ negate: flag(this, 'negate')
32
+
33
+ # set up the callback to trigger
34
+ before: (context) ->
35
+ @callback = sinon.spy()
36
+ flag(context, 'object').on trigger, @callback
37
+
38
+ # verify if our callback is triggered
39
+ after: (context) ->
40
+ negate = flag(context, 'negate')
41
+ flag(context, 'negate', @negate)
42
+ context.assert @callback.calledOnce,
43
+ "expected to trigger #{trigger}",
44
+ "expected not to trigger #{trigger}"
45
+
46
+ if options.with?
47
+ context.assert @callback.calledWith(options.with...),
48
+ "expected trigger to be called with #{inspect options.with}, but was called with #{inspect @callback.args[0]}.",
49
+ "expected trigger not to be called with #{inspect options.with}, but was"
50
+ flag(context, 'negate', negate)
51
+ flag(this, 'whenActions', definedActions)
52
+
53
+ chai.Assertion.addMethod 'when', (val) ->
54
+ definedActions = flag(this, 'whenActions') || []
55
+
56
+ action.before?(this) for action in definedActions
57
+ val() # execute the 'when'
58
+ action.after?(this) for action in definedActions
59
+ )
60
+
61
+ ## Verify if a url fragment is routed to a certain method on the router
62
+ ## Options:
63
+ ## - you can consider multiple routers to test routing priorities
64
+ ## - you can indicate expected arguments to test url extractions
65
+ ##
66
+ ## Examples:
67
+ ##
68
+ ## class MyRouter extends Backbone.Router
69
+ ## routes:
70
+ ## "home/:page/:other": "homeAction"
71
+ ##
72
+ ## myRouter = new MyRouter
73
+ ##
74
+ ## "home/stuff/thing".should.route_to(myRouter, "homeAction")
75
+ ## "home/stuff/thing".should.route_to(myRouter, "homeAction", arguments: ["stuff", "thing"])
76
+ ## "home/stuff/thing".should.route_to(myRouter, "homeAction", consider: [otherRouterWithPossiblyConflictingRoute])
77
+ ##
78
+ #route_to: (router, methodName, options = {}) ->
79
+ ## move possible active Backbone history out of the way temporary
80
+ #current_history = Backbone.history
81
+
82
+ ## reset history to clear active routes
83
+ #Backbone.history = new Backbone.History
84
+
85
+ #spy = sinon.spy router, methodName # spy on our expected method call
86
+ #router._bindRoutes() # inject router routes into our history
87
+ #if options.considering? # if multiple routers are provided load their routes aswell
88
+ #consideredRouter._bindRoutes() for consideredRouter in options.considering
89
+
90
+ ## manually set the root option to prevent calling Backbone.history.start() which is global
91
+ #Backbone.history.options =
92
+ #root: '/'
93
+
94
+ ## fire our route to test
95
+ #Backbone.history.loadUrl @obj
96
+
97
+ ## set back our history. The spy should have our collected info now
98
+ #Backbone.history = current_history
99
+ ## restore the router method
100
+ #router[methodName].restore()
101
+
102
+ ## now assert if everything went according to spec
103
+ #@assert spy.calledOnce,
104
+ #"expected '#{@obj}' to route to #{methodName}",
105
+ #"expected '#{@obj}' not to route to #{methodName}"
106
+
107
+ ## verify arguments if they were provided
108
+ #if options.arguments?
109
+ #@assert spy.calledWith(options.arguments...),
110
+ #"expected '#{methodName}' to be called with #{chai.inspect options.arguments}, but was called with #{chai.inspect spy.args[0]} instead",
111
+ #"expected '#{methodName}' not to be called with #{chai.inspect options.arguments}, but was"
112
+
113
+
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chai-backbone-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthijs Groen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Chai.js matchers for Backbone.js framework
15
+ email:
16
+ - matthijs.groen@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - chai-backbone-rails.gemspec
27
+ - lib/chai-backbone-rails.rb
28
+ - lib/chai-backbone-rails/version.rb
29
+ - test/sinon-chai_spec.js.coffee
30
+ - vendor/assets/javascripts/chai-backbone.js.coffee
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.15
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: A set of assertion matchers to test Backbone code using Konacha in Rails
55
+ test_files:
56
+ - test/sinon-chai_spec.js.coffee