UNPKG

chai-spies-augment

Version:

Additions to chai-spies, adding utilities to inspect args and check if a spy was called with a partial object

77 lines (63 loc) 3.09 kB
'use strict'; var _chai = require('chai'); var _chai2 = _interopRequireDefault(_chai); var _spyArgs = require('./spy-args'); var _spyArgs2 = _interopRequireDefault(_spyArgs); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } describe('Spy Args', function () { var originalSpy = void 0; var spy = void 0; beforeEach(function () { originalSpy = _chai2.default.spy; (0, _spyArgs2.default)(); spy = _chai2.default.spy(); }); afterEach(function () { _chai2.default.spy = originalSpy; }); it('leaves chai.spy methods intact', function () { (0, _chai.expect)(_chai2.default.spy, 'chai.spy.on method should still be available').to.have.property('on'); (0, _chai.expect)(_chai2.default.spy, 'chai.spy.sandbox method should still be available').to.have.property('sandbox'); (0, _chai.expect)(_chai2.default.spy, 'chai.spy.interface method should still be available').to.have.property('interface'); (0, _chai.expect)(_chai2.default.spy, 'chai.spy.restore method should still be available').to.have.property('restore'); (0, _chai.expect)(_chai2.default.spy, 'chai.spy.returns method should still be available').to.have.property('returns'); }); describe('args', function () { it('returns argument array when called with single argument', function () { spy('a'); (0, _chai.expect)(spy.args()).to.eql(['a'], 'should return arg array when spy has been called once'); spy('b'); (0, _chai.expect)(spy.args()).to.eql(['b'], 'should return most recent arg array when spy has been called more than once'); }); it('returns argument array when called with multiple arguments', function () { spy('a', 'b', 'c'); (0, _chai.expect)(spy.args()).to.eql(['a', 'b', 'c'], 'should return arg array when spy has been called once'); spy('d', 'e', 'f'); (0, _chai.expect)(spy.args()).to.eql(['d', 'e', 'f'], 'should return most recent arg array when spy has been called more than once'); }); it('throws useful error when no calls have been made', function () { (0, _chai.expect)(function () { return spy.args(); }).to.throw('Spy has not been called'); }); }); describe('argsFor', function () { it('returns arguments array for specified call index', function () { spy('a'); spy(1, 2, 3); spy(['b', 'c', 'd']); (0, _chai.expect)(spy.argsFor(0)).to.eql(['a'], 'should return argument array for call index 0'); (0, _chai.expect)(spy.argsFor(1)).to.eql([1, 2, 3], 'should return argument array for call index 1'); (0, _chai.expect)(spy.argsFor(2)).to.eql([['b', 'c', 'd']], 'should return argument array for call index 2'); }); it('throws useful error if passed an invalid call index', function () { (0, _chai.expect)(function () { return spy.argsFor(0); }).to.throw('Invalid call index for spy'); spy('a'); (0, _chai.expect)(function () { return spy.argsFor(1); }).to.throw('Invalid call index for spy'); }); }); });