Best JavaScript code snippet using sinon
wrap-method.js
Source:wrap-method.js
...4var hasOwn = Object.prototype.hasOwnProperty;5function isFunction(obj) {6 return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);7}8function mirrorProperties(target, source) {9 for (var prop in source) {10 if (!hasOwn.call(target, prop)) {11 target[prop] = source[prop];12 }13 }14}15// Cheap way to detect if we have ES5 support.16var hasES5Support = "keys" in Object;17module.exports = function wrapMethod(object, property, method) {18 if (!object) {19 throw new TypeError("Should wrap property of object");20 }21 if (typeof method !== "function" && typeof method !== "object") {22 throw new TypeError("Method wrapper should be a function or a property descriptor");23 }24 function checkWrappedMethod(wrappedMethod) {25 var error;26 if (!isFunction(wrappedMethod)) {27 error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +28 valueToString(property) + " as function");29 } else if (wrappedMethod.restore && wrappedMethod.restore.sinon) {30 error = new TypeError("Attempted to wrap " + valueToString(property) + " which is already wrapped");31 } else if (wrappedMethod.calledBefore) {32 var verb = wrappedMethod.returns ? "stubbed" : "spied on";33 error = new TypeError("Attempted to wrap " + valueToString(property) + " which is already " + verb);34 }35 if (error) {36 if (wrappedMethod && wrappedMethod.stackTraceError) {37 error.stack += "\n--------------\n" + wrappedMethod.stackTraceError.stack;38 }39 throw error;40 }41 }42 var error, wrappedMethod, i;43 function simplePropertyAssignment() {44 wrappedMethod = object[property];45 checkWrappedMethod(wrappedMethod);46 object[property] = method;47 method.displayName = property;48 }49 // Firefox has a problem when using hasOwn.call on objects from other frames.50 var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property);51 if (hasES5Support) {52 var methodDesc = (typeof method === "function") ? {value: method} : method;53 var wrappedMethodDesc = getPropertyDescriptor(object, property);54 if (!wrappedMethodDesc) {55 error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +56 property + " as function");57 } else if (wrappedMethodDesc.restore && wrappedMethodDesc.restore.sinon) {58 error = new TypeError("Attempted to wrap " + property + " which is already wrapped");59 }60 if (error) {61 if (wrappedMethodDesc && wrappedMethodDesc.stackTraceError) {62 error.stack += "\n--------------\n" + wrappedMethodDesc.stackTraceError.stack;63 }64 throw error;65 }66 var types = Object.keys(methodDesc);67 for (i = 0; i < types.length; i++) {68 wrappedMethod = wrappedMethodDesc[types[i]];69 checkWrappedMethod(wrappedMethod);70 }71 mirrorProperties(methodDesc, wrappedMethodDesc);72 for (i = 0; i < types.length; i++) {73 mirrorProperties(methodDesc[types[i]], wrappedMethodDesc[types[i]]);74 }75 Object.defineProperty(object, property, methodDesc);76 // catch failing assignment77 // this is the converse of the check in `.restore` below78 if ( typeof method === "function" && object[property] !== method ) {79 // correct any wrongdoings caused by the defineProperty call above,80 // such as adding new items (if object was a Storage object)81 delete object[property];82 simplePropertyAssignment();83 }84 } else {85 simplePropertyAssignment();86 }87 method.displayName = property;88 // Set up an Error object for a stack trace which can be used later to find what line of89 // code the original method was created on.90 method.stackTraceError = (new Error("Stack Trace for original"));91 method.restore = function () {92 // For prototype properties try to reset by delete first.93 // If this fails (ex: localStorage on mobile safari) then force a reset94 // via direct assignment.95 if (!owned) {96 // In some cases `delete` may throw an error97 try {98 delete object[property];99 } catch (e) {} // eslint-disable-line no-empty100 // For native code functions `delete` fails without throwing an error101 // on Chrome < 43, PhantomJS, etc.102 } else if (hasES5Support) {103 Object.defineProperty(object, property, wrappedMethodDesc);104 }105 if (hasES5Support) {106 var descriptor = getPropertyDescriptor(object, property);107 if (descriptor && descriptor.value === method) {108 object[property] = wrappedMethod;109 }110 }111 else {112 // Use strict equality comparison to check failures then force a reset113 // via direct assignment.114 if (object[property] === method) {115 object[property] = wrappedMethod;116 }117 }118 };119 method.wrappedMethod = wrappedMethod;120 method.restore.sinon = true;121 if (!hasES5Support) {122 mirrorProperties(method, wrappedMethod);123 }124 return method;...
mapper-editor.js
Source:mapper-editor.js
1import DefineMap from 'can-define/map/map'2import Component from 'can-component'3import template from './mapper-editor.stache'4const mirrorProperties = [5 'name'6]7export const MapperEditorVM = DefineMap.extend('MapperEditorVM', {8 // passed in via mapper.stache9 selectedPage: {},10 selectedNodeId: {},11 updateCanvas: {},12 // open the legacy modal editor13 openQDE () {14 const pageName = this.selectedPage.name15 window.gotoPageEdit(pageName)16 },17 connectedCallback (el) {18 const vm = this19 const proxyHandler = (ev, newVal, oldVal) => {20 const guideAttr = ev.type21 // handle page name changes22 if (guideAttr === 'name') {23 const page = window.gGuide.pages[oldVal]24 window.pageRename(page, newVal) // pageRename from A2J_Pages.js line#21525 }26 // update any rendered props in the canvas27 vm.updateCanvas(this.selectedNodeId, guideAttr, newVal)28 }29 const proxySelectedPageProperties = (selectedPage) => {30 mirrorProperties.forEach((prop) => {31 // using the notify queue updates global gGuide instantly32 selectedPage.listenTo(prop, proxyHandler, 'notify')33 })34 }35 const tearDownSelectedPageProxy = (selectedPage) => {36 mirrorProperties.forEach((prop) => {37 selectedPage.stopListening(prop, proxyHandler, 'notify')38 })39 }40 // first proxy setup41 proxySelectedPageProperties(this.selectedPage)42 const selectedPageHandler = (event, newVal, oldVal) => {43 if (oldVal) {44 tearDownSelectedPageProxy(oldVal)45 }46 if (newVal) {47 proxySelectedPageProperties(newVal)48 }49 }50 this.listenTo('selectedPage', selectedPageHandler)51 return function () {52 this.stopListening('selectedPage', selectedPageHandler)53 }54 }55})56export default Component.extend('MapperEditorComponent', {57 view: template,58 tag: 'mapper-editor',59 ViewModel: MapperEditorVM,60 leakScope: false...
proxy-guide-changes.js
Source:proxy-guide-changes.js
1// proxy app-state level `guide` map changes to global window.gGuide2const handler = (ev, newVal, oldVal) => {3 const guideAttr = ev.type4 window.gGuide[guideAttr] = newVal5}6export default (guide, mirrorProperties) => {7 // no guide loaded, return noop teardown handler8 if (!guide) { return () => {} }9 mirrorProperties.forEach((prop) => {10 // using the notify queue updates global gGuide instantly11 guide.listenTo(prop, handler, 'notify')12 })13 // return the tearDown function to call in `removed` event or `connectedCallback()` teardown function14 return () => {15 mirrorProperties.forEach((prop) => {16 // using the notify queue updates global gGuide instantly17 guide.stopListening(prop, handler, 'notify')18 })19 }...
Using AI Code Generation
1var foo = {2 setBar: function(value) {3 return this.bar = value;4 }5};6var bar = {7 setFoo: function(value) {8 return this.foo = value;9 }10};11sinon.spy(foo, "setBar");12sinon.spy(bar, "setFoo");13foo.setBar(123);14bar.setFoo(456);15sinon.assert.calledWith(foo.setBar, 123);16sinon.assert.calledWith(bar.setFoo, 456);
Using AI Code Generation
1var sinon = require('sinon');2var obj = {3};4var mirror = sinon.stub().returnsArg(0);5sinon.stub(obj, "a", mirror);6sinon.stub(obj, "b", mirror);7obj.a = 3;8obj.b = 4;
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var obj = {4 method: function () { }5};6var mock = sinon.mock(obj);7mock.expects("method").once();8obj.method();9mock.verify();10var obj = {11 method: function () { }12};13var mock = sinon.mock(obj);14mock.expects("method").once();15obj.method();16mock.verify();17var obj = {18 method: function () { }19};20var mock = sinon.mock(obj);21mock.expects("method").once();22obj.method();23mock.verify();24var obj = {25 method: function () { }26};27var mock = sinon.mock(obj);28mock.expects("method").once();29obj.method();30mock.verify();31var obj = {32 method: function () { }33};34var mock = sinon.mock(obj);35mock.expects("method").once();36obj.method();37mock.verify();38var obj = {39 method: function () { }40};41var mock = sinon.mock(obj);42mock.expects("method").once();43obj.method();44mock.verify();45var obj = {46 method: function () { }47};48var mock = sinon.mock(obj);49mock.expects("method").once();50obj.method();51mock.verify();52var obj = {53 method: function () { }54};55var mock = sinon.mock(obj);56mock.expects("method").once();57obj.method();58mock.verify();
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var obj = {4 method: function() {}5};6var spy = sinon.spy();7sinon.stub(obj, 'method').callsFake(spy);8obj.method(42);9assert(spy.calledWith(42));10var sinon = require('sinon');11var assert = require('assert');12var obj = {13 method: function() {}14};15var spy = sinon.spy();16sinon.stub(obj, 'method').callsFake(spy);17obj.method(42);18assert(spy.calledWith(42));19var sinon = require('sinon');20var assert = require('assert');21var obj = {22 method: function() {}23};24var spy = sinon.spy();25sinon.stub(obj, 'method').callsFake(spy);26obj.method(42);27assert(spy.calledWith(42));28var sinon = require('sinon');29var assert = require('assert');30var obj = {31 method: function() {}32};33var spy = sinon.spy();34sinon.stub(obj, 'method').callsFake(spy);35obj.method(42);36assert(spy.calledWith(42));37var sinon = require('sinon');38var assert = require('assert');39var obj = {40 method: function() {}41};42var spy = sinon.spy();43sinon.stub(obj, 'method').callsFake(spy);44obj.method(42);45assert(spy.calledWith(42));46var sinon = require('sinon');47var assert = require('assert');48var obj = {49 method: function()
Using AI Code Generation
1var sinon = require('sinon');2var obj = {3 method: function() {4 return 42;5 }6};7var mock = sinon.mock(obj);8mock.expects("method").returns(43);9mock.restore();10var sinon = require('sinon');11var obj = {12 method: function() {13 return 42;14 }15};16var mock = sinon.mock(obj);17mock.expects("method").returns(43);18mock.restore();19var sinon = require('sinon');20var obj = {21 method: function() {22 return 42;23 }24};25var mock = sinon.mock(obj);26mock.expects("method").returns(43);27mock.restore();28var sinon = require('sinon');29var obj = {30 method: function() {31 return 42;32 }33};34var mock = sinon.mock(obj);35mock.expects("method").returns(43);36mock.restore();37var sinon = require('sinon');38var obj = {39 method: function() {40 return 42;41 }42};43var mock = sinon.mock(obj);44mock.expects("method").returns(43);45mock.restore();46var sinon = require('sinon');47var obj = {48 method: function() {49 return 42;50 }51};52var mock = sinon.mock(obj);53mock.expects("method").returns(43);54mock.restore();
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var obj = {4 method: function () {}5};6var mock = sinon.mock(obj);7mock.expects('method').once();8obj.method();9mock.verify();10var sinon = require('sinon');11var assert = require('assert');12var obj = {13 method: function () {}14};15var mock = sinon.mock(obj);16mock.expects('method').once();17obj.method();18mock.verify();19var sinon = require('sinon');20var assert = require('assert');21var obj = {22 method: function () {}23};24var mock = sinon.mock(obj);25mock.expects('method').once();26obj.method();27mock.verify();28var sinon = require('sinon');29var assert = require('assert');30var obj = {31 method: function () {}32};33var mock = sinon.mock(obj);34mock.expects('method').once();35obj.method();36mock.verify();37var sinon = require('sinon');38var assert = require('assert');39var obj = {40 method: function () {}41};42var mock = sinon.mock(obj);43mock.expects('method').once();44obj.method();45mock.verify();46var sinon = require('sinon');47var assert = require('assert');48var obj = {49 method: function () {}50};51var mock = sinon.mock(obj);52mock.expects('method').once();53obj.method();54mock.verify();55var sinon = require('sinon');56var assert = require('assert');57var obj = {58 method: function () {}59};60var mock = sinon.mock(obj);61mock.expects('method').once();62obj.method();63mock.verify();
Using AI Code Generation
1var sinon = require('sinon');2var obj = {3};4var mirror = sinon.stub().returnsArg(0);5var proxy = sinon.stub().returnsArg(0);6var spy = sinon.spy();7var fake = sinon.fake();8var stub = sinon.stub();9var mock = sinon.mock();10var sandbox = sinon.createSandbox();11var clock = sinon.useFakeTimers();12var server = sinon.fakeServer.create();13var xhr = sinon.useFakeXMLHttpRequest();14var request = sinon.useFakeXMLHttpRequest();15var request2 = sinon.useFakeXMLHttpRequest();16var request3 = sinon.useFakeXMLHttpRequest();17var request4 = sinon.useFakeXMLHttpRequest();18var request5 = sinon.useFakeXMLHttpRequest();19var request6 = sinon.useFakeXMLHttpRequest();20var request7 = sinon.useFakeXMLHttpRequest();21var request8 = sinon.useFakeXMLHttpRequest();22var request9 = sinon.useFakeXMLHttpRequest();23var request10 = sinon.useFakeXMLHttpRequest();24var request11 = sinon.useFakeXMLHttpRequest();25var request12 = sinon.useFakeXMLHttpRequest();26var request13 = sinon.useFakeXMLHttpRequest();27var request14 = sinon.useFakeXMLHttpRequest();28var request15 = sinon.useFakeXMLHttpRequest();29var request16 = sinon.useFakeXMLHttpRequest();30var request17 = sinon.useFakeXMLHttpRequest();31var request18 = sinon.useFakeXMLHttpRequest();32var request19 = sinon.useFakeXMLHttpRequest();33var request20 = sinon.useFakeXMLHttpRequest();34var request21 = sinon.useFakeXMLHttpRequest();35var request22 = sinon.useFakeXMLHttpRequest();36var request23 = sinon.useFakeXMLHttpRequest();37var request24 = sinon.useFakeXMLHttpRequest();38var request25 = sinon.useFakeXMLHttpRequest();39var request26 = sinon.useFakeXMLHttpRequest();40var request27 = sinon.useFakeXMLHttpRequest();41var request28 = sinon.useFakeXMLHttpRequest();42var request29 = sinon.useFakeXMLHttpRequest();43var request30 = sinon.useFakeXMLHttpRequest();44var request31 = sinon.useFakeXMLHttpRequest();45var request32 = sinon.useFakeXMLHttpRequest();46var request33 = sinon.useFakeXMLHttpRequest();47var request34 = sinon.useFakeXMLHttpRequest();48var request35 = sinon.useFakeXMLHttpRequest();49var request36 = sinon.useFakeXMLHttpRequest();50var request37 = sinon.useFakeXMLHttpRequest();51var request38 = sinon.useFakeXMLHttpRequest();52var request39 = sinon.useFakeXMLHttpRequest();53var request40 = sinon.useFakeXMLHttpRequest();
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var myObj = {4};5sinon.mirrorProperties(myObj, myObj);6assert.ok(myObj.hasOwnProperty('foo'));7assert.ok(myObj.hasOwnProperty('baz'));8var sinon = require('sinon');9var assert = require('assert');10var myObj = {11};12var mock = sinon.mock(myObj);13mock.expects('foo').once().returns('quux');14assert.equal(myObj.foo, 'quux');15mock.verify();16var sinon = require('sinon');17var assert = require('assert');18var myObj = {19};20var mock = sinon.mock(myObj);21mock.expects('foo').once().returns('quux');22assert.equal(myObj.foo, 'quux');23mock.verify();24var sinon = require('sinon');25var assert = require('assert');26var myObj = {27};28var mock = sinon.mock(myObj);29mock.expects('foo').once().returns('quux');30assert.equal(myObj.foo, 'quux');31mock.verify();32var sinon = require('sinon');33var assert = require('assert');34var myObj = {35};36var mock = sinon.mock(myObj);37mock.expects('foo').once().returns('quux');38assert.equal(myObj.foo, 'quux');39mock.verify();40var sinon = require('sinon');41var assert = require('assert');42var myObj = {43};44var mock = sinon.mock(myObj);45mock.expects('foo').once().returns('quux');46assert.equal(myObj.foo, 'quux');47mock.verify();48var sinon = require('sinon');
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var stub = sinon.stub();4stub.withArgs('first').returns(1);5stub.withArgs('second').returns(2);6sinon.stub(obj, 'method').callsFake(stub);7assert.equal(obj.method('first'), 1);8assert.equal(obj.method('second'), 2);9obj.method.restore();10var stub = sinon.stub();11stub.withArgs('first').returns(1);12stub.withArgs('second').returns(2);13var obj = {14};15assert.equal(obj.method('first'), 1);16assert.equal(obj.method('second'), 2);17obj.method.restore();18var sinon = require('sinon');19var assert = require('assert');20var stub = sinon.stub();21stub.withArgs('first').returns(1);22stub.withArgs('second').returns(2);23sinon.stub(obj, 'method').callsFake(stub);24assert.equal(obj.method('first'), 1);25assert.equal(obj.method('second'), 2);26obj.method.restore();27var stub = sinon.stub();28stub.withArgs('first').returns(1);29stub.withArgs('second').returns(2);30var obj = {31};32assert.equal(obj.method('first'), 1);33assert.equal(obj.method('second'), 2);34obj.method.restore();35var sinon = require('sinon');36var assert = require('assert');37var stub = sinon.stub();38stub.withArgs('first').returns(1);39stub.withArgs('second').returns(2);40sinon.stub(obj, 'method').callsFake(stub);41assert.equal(obj.method('first'), 1);42assert.equal(obj.method('second'), 2);43obj.method.restore();44var stub = sinon.stub();45stub.withArgs('first').returns(1);46stub.withArgs('second').returns(2);47var obj = {48};
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!