Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deep equality in calledWith() to allow object/array arguments to match #38

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ for details.

`npm install bondjs` -> `bond = require 'bondjs'`

`<script src="bond.js">` -> `window.bond(...)`

**with mocha, qunit, jasmine**: These frameworks should work with bond as is. Bond looks for a global function named either `afterEach` or `testDone` to implement its spy/stub restore functionality. If those exist, as they should when using these frameworks, it should work fine.

**with some other test runner**: You may need to implement your own `cleanup` method for bond to work properly. This might look like the following.
Expand Down
13 changes: 3 additions & 10 deletions bond.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_ = require 'underscore'

isFunction = (obj) ->
typeof obj == 'function'

Expand Down Expand Up @@ -48,21 +50,13 @@ enhanceSpy = (spy, original, bondApi) ->
spy.calledArgs = []
spy.calledWith = (args...) ->
for calledArgs in spy.calledArgs
return true if arrayEqual(args, calledArgs)
return true if _.isEqual(args, calledArgs)
false

spy[k] = v for k, v of bondApi if bondApi

spy

arrayEqual = (A, B) ->
for a, i in A
b = B[i]
return false if a != b

true


nextTick = do ->
return process.nextTick if isFunction(process?.nextTick)
return setImmediate if setImmediate? && isFunction(setImmediate)
Expand Down Expand Up @@ -170,5 +164,4 @@ bond = (obj, property) ->
'restore': restore
}

window.bond = bond if window?
module.exports = bond if module?.exports
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to the following?

module.exports = bond

18 changes: 3 additions & 15 deletions lib/bond.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"devDependencies": {
"mocha": "~1.6.0",
"coffee-script-redux": "~2.0.0"
},
"dependencies": {
"underscore": "^1.8.3"
}
}
10 changes: 10 additions & 0 deletions test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ describe 'bond', ->
math.add(33, 44)
expect math.add.calledWith(11, 22)

it 'matches objects with deep equality', ->
bond(math, 'add').return(666)
math.add(a: { b: 'c' })
expect math.add.calledWith(a: { b: 'c' })

it 'matches arrays with deep equality', ->
bond(math, 'add').return(666)
math.add([1, 2, 3])
expect math.add.calledWith([1, 2, 3])

it 'exposes argsForCall', ->
bond(math, 'add').return(555)

Expand Down