Skip to content

Commit

Permalink
refactor((app-factory.test.js)): create assertCalledWithFunctionAsArg…
Browse files Browse the repository at this point in the history
… helper

check if a sinon.spy function/method was called with another function
  • Loading branch information
christian-hawk committed Nov 19, 2020
1 parent 56b5056 commit d5ecfb0
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions test/app-factory.test.js
Expand Up @@ -10,23 +10,25 @@ const sinon = require('sinon')
* @param {*} spyFn sinon.Spy function
* @param {*} argFn callback / function param
*/
function getCallWithArgs (spyFn, argFn) {
function assertCalledWithFunctionAsArg (spyFn, argFn) {
const calls = spyFn.getCalls()
const argFnString = argFn.toString()
let foundCall
let foundMatch = false
for (const call in calls) {
const arg = spyFn.getCall(call).args[0]
if (arg.toString() === argFnString) {
foundCall = spyFn.getCall(call)
// foundCall = spyFn.getCall(call)
foundMatch = true
}
}
return foundCall
assert(foundMatch === true, 'Spy function/method was not called with expected function')
}

describe('csurf middleware', () => {
const rewiredCsurf = appFactoryRewire.__get__('csurf')

it('should exist', () => {
// eslint-disable-next-line security/detect-non-literal-fs-filename
assert.exists(rewiredCsurf)
})

Expand All @@ -47,10 +49,7 @@ describe('csurf middleware', () => {

appInstance.createApp()

const call = getCallWithArgs(appUseSpy, csurf({ cookie: true }))
const arg = call.args[0]
assert(arg.toString === csurf({ cookie: true }).toString)

assertCalledWithFunctionAsArg(appUseSpy, csurf({ cookies: true }))
sinon.restore()
})
})

0 comments on commit d5ecfb0

Please sign in to comment.