diff --git a/README.md b/README.md index 37cb127..a72544d 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/index.js b/index.js index c6dce7b..42bd526 100644 --- a/index.js +++ b/index.js @@ -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 = [] } @@ -42,7 +55,7 @@ } mock.restore = _restoreMock.bind(null, mock) - mocks.push(mock) + mocks.unshift(mock) obj[key] = mockValue return mockValue diff --git a/test.js b/test.js index 5e20bb4..80468e7 100644 --- a/test.js +++ b/test.js @@ -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') @@ -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