Skip to content

Commit

Permalink
Add granular mock restore to resolve #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Raubenheimer authored and Pieter Raubenheimer committed Jan 26, 2016
1 parent 42bc528 commit 2ef0aac
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ See [examples](examples) for more common usage patterns.

For `var simple = require('simple-mock')`:

### simple.restore()

Restores all current mocks.

### simple.mock(obj, key, value)

Sets the value on this object. E.g. `mock(config, 'title', 'test')` is the same as `config.title = 'test'`, but restorable with all mocks.
Expand All @@ -120,6 +116,14 @@ Wraps `fn` in a spy and sets this on the `obj`, restorable with all mocks.

If `obj` has already has this function, it is wrapped in a spy. The resulting spy can be turned into a stub by further configuration. Restores with all mocks.

### simple.restore()

Restores all current mocks.

### simple.restore(obj, key)

Use this if you need to restore only a single mock value or function on an object.

### simple.spy(fn) *or* simple.mock(fn)

Wraps `fn` in a spy.
Expand Down
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
/**
* Restore the current simple and create a new one
*
* @param {Object} [obj]
* @param {String} [key]
* @api public
*/
simple.restore = function () {
simple.restore = function (obj, key) {
if (obj && key) {
mocks.some(function (mock, i) {
if (mock.obj !== obj || mock.key !== key) return

mock.restore()
mocks.splice(i, 1)
return true
})
return
}

mocks.forEach(_restoreMock)
mocks = []
}
Expand Down Expand Up @@ -42,7 +55,7 @@
}

mock.restore = _restoreMock.bind(null, mock)
mocks.push(mock)
mocks.unshift(mock)

obj[key] = mockValue
return mockValue
Expand Down
51 changes: 50 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
/*global describe beforeEach it before*/
/*global describe beforeEach afterEach it before*/
var simple = require('./index')
var assert = require('assert')

Expand Down Expand Up @@ -868,6 +868,55 @@ describe('simple', function () {
})
})

describe('restore()', function () {
var objA
var objB
var originalValue
var mockedValue

beforeEach(function () {
originalValue = 'a'
mockedValue = 'b'

objA = {
name: 'objA',
valueMock: originalValue
}

objB = {
name: 'objB',
valueMock: originalValue
}

simple.mock(objA, 'valueMock', mockedValue)
simple.mock(objB, 'valueMock', mockedValue)
})

afterEach(function () {
simple.restore()
})

it('can restore all mocks', function () {
simple.restore()
assert.equal(objA.valueMock, originalValue)
assert.equal(objB.valueMock, originalValue)
})

it('can restore double-mocked values', function () {
simple.mock(objA, 'valueMock', 'ac')
simple.mock(objB, 'valueMock', 'bc')
simple.restore()
assert.equal(objA.valueMock, originalValue)
assert.equal(objB.valueMock, originalValue)
})

it('can restore a single mock', function () {
simple.restore(objA, 'valueMock')
assert.equal(objA.valueMock, originalValue)
assert.equal(objB.valueMock, mockedValue)
})
})

describe('mock()', function () {
var obj

Expand Down

0 comments on commit 2ef0aac

Please sign in to comment.