How to use catpants method in sinon

Best JavaScript code snippet using sinon

stateMachineSpec.js

Source:stateMachineSpec.js Github

copy

Full Screen

1/*global require: true, global: true, describe: true, beforeEach: true,2 it: true */3var assert = require('assert');4var sinon = require('sinon');5// Fake the Impact global namespace with good enough definitions.6var ig = global.ig = {7 // Impact module definition stuff.8 module: function() {9 return this;10 },11 requires: function() {12 return this;13 },14 defines: function(definition) {15 definition();16 }17};18// The module declares StateMachine globally.19var StateMachine = require('../stateMachine.js').StateMachine;20describe('StateMachine', function() {21 var sm;22 beforeEach(function() {23 sm = new StateMachine();24 });25 it('lets you define states', function() {26 assert(!sm.states.catpants);27 sm.state('catpants', {28 update: function() {}29 });30 assert(sm.states.catpants);31 });32 it('retrieves states by name', function() {33 var definition = {34 update: function() {}35 };36 sm.state('catpants', definition);37 assert(sm.state('catpants') === definition);38 });39 it('sets first defined state as initialState', function() {40 assert(!sm.initialState);41 sm.state('catpants', {42 update: function() {}43 });44 sm.state('doggyhat', {45 update: function() {}46 });47 assert(sm.initialState === 'catpants');48 });49 it('lets you define transitions', function() {50 assert(!sm.transitions.catpants);51 // Must define states before defining transition.52 sm.state('doggyhat', {});53 sm.state('horsepoo', {});54 var transition = sm.transition('catpants', 'doggyhat', 'horsepoo', function() {});55 assert(transition);56 assert(sm.transitions.catpants);57 });58 it('does not require named transitions', function() {59 var counter = 0;60 for (var key in sm.transitions) {61 counter += 1;62 }63 assert(!counter);64 sm.state('catpants', {});65 sm.state('doggyhat', {});66 var transition = sm.transition('catpants', 'doggyhat', function() {});67 // We got somebaseitem back, right?68 assert(transition);69 counter = 0;70 for (key in sm.transitions) {71 counter += 1;72 }73 assert(counter === 1);74 assert(!sm.transitions.catpants);75 });76 it('retrieves transitions by name', function() {77 // Must define states before defining transition.78 sm.state('fromState', {});79 sm.state('toState', {});80 var predicate = function() {};81 sm.transition('catpants', 'fromState', 'toState', predicate);82 var transition = sm.transition('catpants');83 assert(transition);84 assert(transition.fromState === 'fromState');85 assert(transition.toState === 'toState');86 assert(transition.predicate === predicate);87 });88 it('verifies states exist before creating transition', function() {89 assert(!sm.states.fromState);90 assert(!sm.states.toState);91 assert.throws(function() {92 sm.transition('catpants', 'fromState', 'toState', function() {});93 }, /fromState/);94 // Define fromState.95 sm.state('fromState', {});96 assert.throws(function() {97 sm.transition('catpants', 'fromState', 'toState', function() {});98 }, /toState/);99 // Define toState.100 sm.state('toState', {});101 assert.doesNotThrow(function() {102 sm.transition('catpants', 'fromState', 'toState', function() {});103 });104 });105 describe('update', function() {106 var definition;107 beforeEach(function() {108 definition = {109 enter: sinon.spy(),110 update: sinon.spy(),111 exit: sinon.spy()112 };113 sm.state('catpants', definition);114 });115 it('calls initialState.enter only on first call to update', function() {116 sm.update();117 sm.update();118 assert(definition.enter.calledOnce);119 assert(definition.update.calledTwice);120 });121 it('does not call enter on state if it does not exist', function() {122 definition.enter = null;123 sm.update();124 assert(definition.update.called);125 });126 it('does not call update on state if it does not exist', function() {127 definition.update = null;128 sm.update();129 assert(definition.enter.called);130 });131 it('iterates through transitions, seeing if they should fire', function() {132 // Add another couple of states to play with.133 sm.state('doggyhat', {});134 sm.state('horsepoo', {});135 var predicate1 = sinon.stub().returns(false);136 var predicate2 = sinon.stub().returns(false);137 sm.transition('one', 'catpants', 'doggyhat', predicate1);138 sm.transition('two', 'catpants', 'horsepoo', predicate2);139 sm.update();140 sm.update();141 assert(predicate1.calledTwice);142 assert(predicate2.calledTwice);143 });144 it('only calls predicates when they match the fromState', function() {145 // Add a couple of states to play with.146 sm.state('doggyhat', {});147 sm.state('horsepoo', {});148 var predicate1 = sinon.stub().returns(false);149 var predicate2 = sinon.stub().returns(false);150 sm.transition('one', 'catpants', 'doggyhat', predicate1);151 sm.transition('two', 'doggyhat', 'horsepoo', predicate2);152 sm.update();153 assert(predicate1.calledOnce);154 assert(!predicate2.called);155 });156 it('transitions to new state if predicate returns true', function() {157 var definition2 = {158 enter: sinon.spy(),159 update: sinon.spy()160 };161 sm.state('doggyhat', definition2);162 var predicate1 = sinon.stub().returns(true);163 sm.transition('one', 'catpants', 'doggyhat', predicate1);164 sm.update();165 assert(definition.enter.called);166 assert(definition.update.called);167 assert(definition.exit.called);168 assert(!definition2.enter.called);169 sm.update();170 assert(sm.currentState === 'doggyhat');171 assert(definition2.enter.called);172 assert(definition2.update.called);173 });174 it('does not call exit on state if it does not exist', function() {175 sm.state('doggyhat', {});176 var predicate1 = sinon.stub().returns(true);177 sm.transition('one', 'catpants', 'doggyhat', predicate1);178 definition.exit = null;179 sm.update();180 assert(definition.enter.called);181 assert(definition.update.called);182 });183 it('early exits after finding one transition', function() {184 var definition2 = {185 enter: sinon.spy(),186 update: sinon.spy()187 };188 var definition3 = {189 enter: sinon.spy(),190 update: sinon.spy()191 };192 sm.state('doggyhat', definition2);193 sm.state('horsepoo', definition3);194 var predicate1 = sinon.stub().returns(true);195 var predicate2 = sinon.stub().returns(true);196 sm.transition('one', 'catpants', 'doggyhat', predicate1);197 sm.transition('two', 'catpants', 'horsepoo', predicate2);198 // This call should transition to doggyhat.199 sm.update();200 assert(sm.currentState === 'doggyhat');201 });202 it('does not double-transition', function() {203 // Brief bug where it would jump from state1->state2->state3 because204 // of how the transition logic worked. Related to early exit above.205 var definition2 = {206 enter: sinon.spy(),207 update: sinon.spy()208 };209 var definition3 = {210 enter: sinon.spy(),211 update: sinon.spy()212 };213 sm.state('doggyhat', definition2);214 sm.state('horsepoo', definition3);215 var predicate1 = sinon.stub().returns(true);216 var predicate2 = sinon.stub().returns(true);217 sm.transition('one', 'catpants', 'doggyhat', predicate1);218 sm.transition('two', 'doggyhat', 'horsepoo', predicate2);219 // This call should transition to doggyhat.220 sm.update();221 assert(sm.currentState === 'doggyhat');222 });223 });224 describe('end-to-end', function() {225 it('works for simple case', function() {226 var sm = new StateMachine();227 // Define attacking state.228 var counter = 0;229 var fleeing = false;230 sm.state('attacking', {231 enter: function() {232 counter = 42;233 },234 update: function() {235 counter += 1;236 },237 exit: function() {238 fleeing = true;239 }240 });241 // Define running state.242 var distance = 0;243 sm.state('running', {244 enter: function() {245 counter = 0;246 },247 update: function() {248 distance += 1;249 }250 });251 // Define transition.252 sm.transition('cowardice', 'attacking', 'running', function() {253 return counter > 44;254 });255 // Step-by-step verification.256 assert(counter === 0);257 assert(distance === 0);258 sm.update();259 assert(counter === 43);260 assert(!fleeing);261 sm.update();262 assert(counter === 44);263 assert(!fleeing);264 sm.update();265 assert(counter === 45);266 assert(fleeing);267 sm.update();268 assert(counter === 0);269 assert(distance === 1);270 assert(fleeing);271 });272 });...

Full Screen

Full Screen

comboManagerSpec.js

Source:comboManagerSpec.js Github

copy

Full Screen

1/*global require: true, global: true, describe: true, beforeEach: true,2 it: true, ComboManager: true */3var chai = require('chai');4var sinon = require('sinon');5var sinonChai = require('sinon-chai');6var _ = require('underscore');7// Get Chai to use the sinon adapter.8chai.use(sinonChai);9var expect = chai.expect;10// Expose underscore globally.11global._ = _;12// Fake the Impact global namespace with good enough definitions.13var ig = global.ig = {14 // The ComboManager overrides bind and bindTouch, so we need to fake up15 // the extend method on Input to verify that it is intercepting the16 // right stuff.17 Input: {18 inject: sinon.stub()19 },20 // Fake input stuff.21 input: {22 pressed: function() {}23 },24 // Fake Timer stuff.25 Timer: function() {},26 // Impact module definition stuff.27 module: function() {28 return this;29 },30 requires: function() {31 return this;32 },33 defines: function(definition) {34 definition();35 }36};37// The module declares ComboManager globally.38var ComboManager = require('../comboManager.js').ComboManager;39describe('ComboManager', function() {40 describe('setup', function() {41 it('should be defined', function() {42 expect(ComboManager).to.be.a('function');43 });44 it('should have well-defined initial state', function() {45 var cm = new ComboManager();46 expect(cm.actions).to.be.a('array');47 expect(cm.combos).to.be.a('object');48 expect(cm.timer).to.be.a('object');49 expect(cm.inputStream).to.be.a('array');50 });51 it('should intercept bind and bindTouch', function() {52 expect(ig.Input.inject.called).to.equal(true);53 var call = ig.Input.inject.getCall(0);54 var definition = call.args[0];55 // Verify that it is trying to do something with bind and bindTouch.56 expect(definition.bind).to.be.a('function');57 expect(definition.bindTouch).to.be.a('function');58 });59 });60 describe('method', function() {61 var comboManager;62 var moves = ['up', 'up', 'down', 'down'];63 var cb;64 var deltaStub;65 var fakeTimer;66 beforeEach(function() {67 // Fake the timer as well.68 deltaStub = sinon.stub();69 fakeTimer = {70 delta: deltaStub71 };72 sinon.stub(ig, 'Timer').returns(fakeTimer);73 comboManager = new ComboManager();74 cb = sinon.spy();75 // Register those moves.76 comboManager.actions = _.uniq(moves);77 });78 afterEach(function() {79 ig.Timer.restore();80 });81 describe('add', function() {82 it('should be a function', function() {83 expect(comboManager.add).to.be.a('function');84 });85 it('should return a handle after adding', function() {86 var handle = comboManager.add(moves, 1000, cb);87 expect(handle).to.be.ok;88 });89 it('should register a combo', function() {90 expect(_.size(comboManager.combos)).to.equal(0);91 var handle = comboManager.add(moves, 1000, cb);92 expect(_.size(comboManager.combos)).to.equal(1);93 });94 });95 describe('remove', function() {96 it('should be a function', function() {97 expect(comboManager.remove).to.be.a('function');98 });99 it('should remove combos added by add', function() {100 var handle = comboManager.add(moves, 1000, cb);101 expect(_.size(comboManager.combos)).to.equal(1);102 comboManager.remove(handle);103 expect(_.size(comboManager.combos)).to.equal(0);104 });105 it('should not blow up if given invalid handle', function() {106 comboManager.remove('thing');107 });108 });109 describe('update', function() {110 it('should be a function', function() {111 expect(comboManager.update).to.be.a('function');112 });113 describe('input tracking', function() {114 it('should register keystrokes on update', function() {115 // Track some actions.116 comboManager.actions = ['catpants', 'doggyhat', 'horsepoo'];117 ig.input.pressed = sinon.stub();118 // Inputs interspersed with updates.119 ig.input.pressed.withArgs('doggyhat').returns(true);120 comboManager.update();121 ig.input.pressed.withArgs('doggyhat').returns(false);122 ig.input.pressed.withArgs('catpants').returns(true);123 comboManager.update();124 // catpants * 2125 comboManager.update();126 ig.input.pressed.withArgs('catpants').returns(false);127 ig.input.pressed.withArgs('horsepoo').returns(true);128 comboManager.update();129 // Validate the input stream.130 var actions = _.pluck(comboManager.inputStream, 'action');131 expect(actions).to.deep.equal(132 ['doggyhat', 'catpants', 'catpants', 'horsepoo']);133 });134 it('should not grow unboundedly', function() {135 // Track some actions.136 comboManager.actions = ['catpants', 'doggyhat', 'horsepoo'];137 // Invent a max size.138 comboManager.comboMaxSize = 6;139 ig.input.pressed = sinon.stub();140 var lotsOfActions = _.map(_.range(600), function(i) {141 return comboManager.actions[i % comboManager.actions.length];142 });143 _.each(lotsOfActions, function(action) {144 ig.input.pressed.withArgs(action).returns(true);145 comboManager.update();146 ig.input.pressed.withArgs(action).returns(false);147 });148 expect(comboManager.inputStream).to.have.length.below(101);149 });150 });151 describe('with one combo', function() {152 var handle;153 var interval;154 var doCombo = function() {155 _.each(moves, function(move, index) {156 if (index > 0) {157 ig.input.pressed.withArgs(moves[index - 1]).returns(false);158 }159 ig.input.pressed.withArgs(moves[index]).returns(true);160 comboManager.update();161 });162 };163 beforeEach(function() {164 deltaStub.returns(0);165 interval = 0.5;166 handle = comboManager.add(moves, interval, cb);167 ig.input.pressed = sinon.stub();168 });169 it('should call callback if inputs match combo', function() {170 doCombo();171 expect(cb.called).to.be.ok;172 });173 it('should not call callback if combo took too long', function() {174 fakeTimer.delta = function() {};175 var counter = 0;176 sinon.stub(fakeTimer, 'delta', function() {177 counter += 0.3;178 return counter;179 });180 doCombo();181 expect(cb.called).to.not.be.ok;182 });183 it('should only call callback once on match', function() {184 doCombo();185 ig.input.pressed.withArgs(moves[moves.length - 1]).returns(false);186 comboManager.update();187 expect(cb.calledOnce).to.be.ok;188 });189 });190 });191 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require('chai');2var sinonChai = require('sinon-chai');3chai.use(sinonChai);4var chaiAsPromised = require('chai-as-promised');5chai.use(chaiAsPromised);6var chaiThings = require('chai-things');7chai.use(chaiThings);8var chaiSpies = require('chai-spies');9chai.use(chaiSpies);10var chaiJquery = require('chai-jquery');11chai.use(chaiJquery);12var chaiDatetime = require('chai-datetime');13chai.use(chaiDatetime);14var chaiArrays = require('chai-arrays');15chai.use(chaiArrays);16var chaiEnzyme = require('chai-enzyme');17chai.use(chaiEnzyme);18var chaiUuid = require('chai-uuid');19chai.use(chaiUuid);20var chaiFuzzy = require('chai-fuzzy');21chai.use(chaiFuzzy);22var chaiXml = require('chai-xml');23chai.use(chaiXml);24var chaiSubset = require('chai-subset');25chai.use(chaiSubset);26var chaiJsonEqual = require('chai-json-equal');27chai.use(chaiJsonEqual);28var chaiJsonSchema = require('chai-json-schema');29chai.use(chaiJsonSchema);30var chaiHttp = require('chai-http');31chai.use(chaiHttp);32var chaiJsonPattern = require('chai-json-pattern');33chai.use(chaiJsonPattern);34var chaiLike = require('chai-like');35chai.use(chaiLike);36var chaiAsPromised = require('chai-as-promised');37chai.use(chaiAsPromised);38var chaiChange = require('chai-change');39chai.use(chaiChange);40var chaiFs = require('

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require('chai');2var sinon = require('sinon');3var sinonChai = require('sinon-chai');4chai.use(sinonChai);5var expect = chai.expect;6var spy = sinon.spy();7var obj = { method: spy };8obj.method();9expect(spy).to.have.been.called;10var chai = require('chai');11var sinon = require('sinon');12var sinonChai = require('sinon-chai');13chai.use(sinonChai);14var expect = chai.expect;15var spy = sinon.spy();16var obj = { method: spy };17obj.method();18expect(spy).to.have.been.called;19var chai = require('chai');20var sinon = require('sinon');21var sinonChai = require('sinon-chai');22chai.use(sinonChai);23var expect = chai.expect;24var spy = sinon.spy();25var obj = { method: spy };26obj.method();27expect(spy).to.have.been.called;28var chai = require('chai');29var sinon = require('sinon');30var sinonChai = require('sinon-chai');31chai.use(sinonChai);32var expect = chai.expect;33var spy = sinon.spy();34var obj = { method: spy };35obj.method();36expect(spy).to.have.been.called;37var chai = require('chai');38var sinon = require('sinon');39var sinonChai = require('sinon-chai');40chai.use(sinonChai);41var expect = chai.expect;42var spy = sinon.spy();43var obj = { method: spy };44obj.method();45expect(spy).to.have.been.called;46var chai = require('chai');47var sinon = require('sinon');48var sinonChai = require('sinon-chai');49chai.use(sinonChai);

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require('chai');2var sinon = require('sinon');3var sinonChai = require('sinon-chai');4chai.use(sinonChai);5var expect = chai.expect;6var should = chai.should();7var assert = chai.assert;8var spy = sinon.spy();9var assert = chai.assert;10var stub = sinon.stub();11var assert = chai.assert;12var mock = sinon.mock();13var assert = chai.assert;14var sandbox = sinon.sandbox.create();15var assert = chai.assert;16var clock = sinon.useFakeTimers();17var assert = chai.assert;18var server = sinon.fakeServer.create();19var assert = chai.assert;20var xhr = sinon.useFakeXMLHttpRequest();21var assert = chai.assert;22var xhr = sinon.useFakeXMLHttpRequest();23var assert = chai.assert;24var xhr = sinon.useFakeXMLHttpRequest();25var assert = chai.assert;26var xhr = sinon.useFakeXMLHttpRequest();27var assert = chai.assert;28var xhr = sinon.useFakeXMLHttpRequest();29var assert = chai.assert;30var xhr = sinon.useFakeXMLHttpRequest();31var assert = chai.assert;32var xhr = sinon.useFakeXMLHttpRequest();33var assert = chai.assert;34var xhr = sinon.useFakeXMLHttpRequest();35var assert = chai.assert;36var xhr = sinon.useFakeXMLHttpRequest();37var assert = chai.assert;38var xhr = sinon.useFakeXMLHttpRequest();39var assert = chai.assert;

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var catpants = require('catpants');3var assert = require('assert');4var sinonCatpants = catpants(sinon);5var myModule = require('myModule');6describe('myModule', function() {7 var stub;8 before(function() {9 stub = sinonCatpants.stub(myModule, 'myFunction');10 });11 it('should call myFunction', function() {12 myModule.myFunction();13 assert(stub.called);14 });15});16### catpants(sinon)17### sinonCatpants.stub(object, method)18### sinonCatpants.spy(object, method)19### sinonCatpants.mock(object, method)

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var catpants = require('catpants');3var test = require('tap').test;4var myModule = require('./myModule');5test('myModule', function(t) {6 var myModuleStub = sinon.stub(myModule, 'myMethod');7 myModuleStub.yields(null, 'result');8 myModule.myMethod('test', function(err, result) {9 t.equal(result, 'result');10 myModuleStub.restore();11 t.end();12 });13});

Full Screen

Using AI Code Generation

copy

Full Screen

1var catpants = require('catpants');2var sinon = require('sinon');3var assert = require('assert');4function add(a,b){5 return a+b;6}7var addStub = sinon.stub(add);8addStub.withArgs(1,2).returns(3);9assert.equal(addStub(1,2),3);10assert.equal(addStub(2,3),5);11assert.equal(addStub(1,2),3);12assert.equal(addStub(2,3),5);13var catpants = require('catpants');14var sinon = require('sinon');15var assert = require('assert');16function add(a,b){17 return a+b;18}19var addStub = sinon.stub(add);20addStub.withArgs(1,2).returns(3);21assert.equal(addStub(1,2),3);22assert.equal(addStub(2,3),5);23assert.equal(addStub(1,2),3);24assert.equal(addStub(2,3),5);25var catpants = require('catpants');26var sinon = require('sinon');27var assert = require('assert');28function add(a,b){29 return a+b;30}31var addStub = sinon.stub(add);32addStub.withArgs(1,2).returns(3);33assert.equal(addStub(1,2),3);34assert.equal(addStub(2,3),5);35assert.equal(addStub(1,2),3);36assert.equal(addStub(2,3),5);

Full Screen

Using AI Code Generation

copy

Full Screen

1chai.use(require('sinon-chai'));2expect(myFunction).to.have.been.calledWith('test');3chai.use(require('sinon-chai'));4expect(myFunction).to.have.been.calledWith('test');5chai.use(require('sinon-chai'));6expect(myFunction).to.have.been.calledWith('test');7chai.use(require('sinon-chai'));8expect(myFunction).to.have.been.calledWith('test');9chai.use(require('sinon-chai'));10expect(myFunction).to.have.been.calledWith('test');11chai.use(require('sinon-chai'));12expect(myFunction).to.have.been.calledWith('test');13chai.use(require('sinon-chai'));14expect(myFunction).to.have.been.calledWith('test');15chai.use(require('sinon-chai'));16expect(myFunction).to.have.been.calledWith('test');17chai.use(require('sinon-chai'));18expect(myFunction).to.have.been.calledWith('test');19chai.use(require('sinon-chai'));20expect(myFunction).to.have.been.calledWith('test');21chai.use(require('sinon-chai'));22expect(myFunction).to.have.been.calledWith('test');

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var catpants = require('catpants');3var expect = require('chai').expect;4var test = function() {5 var testObj = {6 testFunc: function() {7 return 'test';8 }9 };10 var spy = sinon.spy(testObj, 'testFunc');11 var result = testObj.testFunc();12 expect(result).to.equal('test');13};14catpants(test);15var sinon = require('sinon');16var expect = require('chai').expect;17var test = function() {18 var sandbox = sinon.sandbox.create();19 var testObj = {20 testFunc: function() {21 return 'test';22 }23 };24 var spy = sandbox.spy(testObj, 'testFunc');25 var result = testObj.testFunc();26 expect(result).to.equal('test');27 sandbox.restore();28};29test();

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run sinon automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful