Best JavaScript code snippet using sinon
sanitizer.js
Source:sanitizer.js
1// Copyright (c) 2009 by Kris Maglione <maglione.k at Gmail>2// Copyright (c) 2009-2010 by Doug Kearns <dougkearns@gmail.com>3//4// This work is licensed for reuse under an MIT license. Details are5// given in the License.txt file included with this file.6// TODO:7// - fix Sanitize autocommand8// - add warning for TIMESPAN_EVERYTHING?9// - respect privacy.clearOnShutdown et al or recommend VimperatorLeave autocommand?10// - add support for :set sanitizeitems=all like 'eventignore'?11// - integrate with the Clear Private Data dialog?12// FIXME:13// - finish 1.9.0+ support if we're going to support sanitizing in Xulmus14const Sanitizer = Module("sanitizer", {15 requires: ["liberator"],16 init: function () {17 const self = this;18 liberator.loadScript("chrome://browser/content/sanitize.js", Sanitizer);19 this.__proto__.__proto__ = new Sanitizer.Sanitizer; // Good enough.20 Sanitizer.getClearRange = Sanitizer.Sanitizer.getClearRange; // XXX21 self.prefDomain = "privacy.cpd.";22 self.prefDomain2 = "extensions.liberator.privacy.cpd.";23 },24 // Largely ripped from from browser/base/content/sanitize.js so we can override25 // the pref strategy without stepping on the global prefs namespace.26 sanitize: function () {27 const prefService = services.get("pref");28 let branch = prefService.getBranch(this.prefDomain);29 let branch2 = prefService.getBranch(this.prefDomain2);30 let errors = null;31 function prefSet(name) {32 try {33 return branch.getBoolPref(name);34 }35 catch (e) {36 return branch2.getBoolPref(name);37 }38 }39 // Cache the range of times to clear40 if (this.ignoreTimespan)41 var range = null; // If we ignore timespan, clear everything42 else43 range = this.range || Sanitizer.getClearRange();44 for (let itemName in this.items) {45 let item = this.items[itemName];46 item.range = range;47 if ("clear" in item && item.canClear && prefSet(itemName)) {48 liberator.echomsg("Sanitizing " + itemName + " items...");49 // Some of these clear() may raise exceptions (see bug #265028)50 // to sanitize as much as possible, we catch and store them,51 // rather than fail fast.52 // Callers should check returned errors and give user feedback53 // about items that could not be sanitized54 try {55 item.clear();56 }57 catch (e) {58 if (!errors)59 errors = {};60 errors[itemName] = e;61 liberator.echoerr("Error sanitizing " + itemName + ": " + e);62 }63 }64 }65 return errors;66 },67 get prefNames() util.Array.flatten([this.prefDomain, this.prefDomain2].map(options.allPrefs))68}, {69 argToPref: function (arg) ["commandLine", "offlineApps", "siteSettings"].filter(function (pref) pref.toLowerCase() == arg)[0] || arg,70 prefToArg: function (pref) pref.toLowerCase().replace(/.*\./, "")71}, {72 commands: function () {73 commands.add(["sa[nitize]"],74 "Clear private data",75 function (args) {76 liberator.assert(!options['private'], "Cannot sanitize items in private mode");77 let timespan = args["-timespan"] || options["sanitizetimespan"];78 sanitizer.range = Sanitizer.getClearRange(timespan);79 sanitizer.ignoreTimespan = !sanitizer.range;80 if (args.bang) {81 liberator.assert(args.length == 0, "Trailing characters");82 liberator.echomsg("Sanitizing all items in 'sanitizeitems'...");83 let errors = sanitizer.sanitize();84 if (errors) {85 for (let item in errors)86 liberator.echoerr("Error sanitizing " + item + ": " + errors[item]);87 }88 }89 else {90 liberator.assert(args.length > 0, "Argument required");91 for (let [, item] in Iterator(args.map(Sanitizer.argToPref))) {92 liberator.echomsg("Sanitizing " + item + " items...");93 if (sanitizer.canClearItem(item)) {94 try {95 sanitizer.items[item].range = sanitizer.range;96 sanitizer.clearItem(item);97 }98 catch (e) {99 liberator.echoerr("Error sanitizing " + item + ": " + e);100 }101 }102 else103 liberator.echomsg("Cannot sanitize " + item);104 }105 }106 },107 {108 argCount: "*", // FIXME: should be + and 0109 bang: true,110 completer: function (context) {111 context.title = ["Privacy Item", "Description"];112 context.completions = options.get("sanitizeitems").completer();113 },114 options: [115 [["-timespan", "-t"],116 commands.OPTION_INT,117 function (arg) /^[0-4]$/.test(arg),118 function () options.get("sanitizetimespan").completer()]119 ]120 });121 },122 options: function () {123 const self = this;124 // add liberator-specific private items125 [126 {127 name: "commandLine",128 action: function () {129 let stores = ["command", "search"];130 if (self.range) {131 stores.forEach(function (store) {132 storage["history-" + store].mutate("filter", function (item) {133 let timestamp = item.timestamp * 1000;134 return timestamp < self.range[0] || timestamp > self.range[1];135 });136 });137 }138 else139 stores.forEach(function (store) { storage["history-" + store].truncate(0); });140 }141 },142 {143 name: "macros",144 action: function () { storage["macros"].clear(); }145 },146 {147 name: "marks",148 action: function () {149 storage["local-marks"].clear();150 storage["url-marks"].clear();151 }152 }153 ].forEach(function (item) {154 let pref = self.prefDomain2 + item.name;155 if (options.getPref(pref) == null)156 options.setPref(pref, false);157 self.items[item.name] = {158 canClear: true,159 clear: item.action160 };161 });162 // call Sanitize autocommand163 for (let [name, item] in Iterator(self.items)) {164 let arg = Sanitizer.prefToArg(name);165 if (item.clear) {166 let func = item.clear;167 item.clear = function () {168 autocommands.trigger("Sanitize", { name: arg });169 func.call(item);170 };171 }172 }173 options.add(["sanitizeitems", "si"],174 "The default list of private items to sanitize",175 "stringlist", "cache,commandline,cookies,formdata,history,marks,sessions",176 {177 setter: function (values) {178 for (let [, pref] in Iterator(sanitizer.prefNames)) {179 options.setPref(pref, false);180 for (let [, value] in Iterator(this.parseValues(values))) {181 if (Sanitizer.prefToArg(pref) == value) {182 options.setPref(pref, true);183 break;184 }185 }186 }187 return values;188 },189 getter: function () sanitizer.prefNames.filter(function (pref) options.getPref(pref)).map(Sanitizer.prefToArg).join(","),190 completer: function (value) [191 ["cache", "Cache"],192 ["commandline", "Command-line history"],193 ["cookies", "Cookies"],194 ["downloads", "Download history"],195 ["formdata", "Saved form and search history"],196 ["history", "Browsing history"],197 ["macros", "Saved macros"],198 ["marks", "Local and URL marks"],199 ["offlineapps", "Offline website data"],200 ["passwords", "Saved passwords"],201 ["sessions", "Authenticated sessions"],202 ["sitesettings", "Site preferences"]203 ]204 });205 options.add(["sanitizetimespan", "sts"],206 "The default sanitizer time span",207 "number", 1,208 {209 setter: function (value) {210 options.setPref("privacy.sanitize.timeSpan", value);211 return value;212 },213 getter: function () options.getPref("privacy.sanitize.timeSpan", this.defaultValue),214 completer: function (value) [215 ["0", "Everything"],216 ["1", "Last hour"],217 ["2", "Last two hours"],218 ["3", "Last four hours"],219 ["4", "Today"]220 ]221 });222 }223});...
walk-test.js
Source:walk-test.js
...123 var child = sinon.create(parent);124 child.func = function childFunc() {};125 var iterator = sinon.spy();126 sinon.walk(child, iterator);127 var propertyNames = iterator.args.map(function (call) {128 return call[1];129 });130 // make sure that each property name only exists once131 propertyNames.forEach(function (name, index) {132 assert.equals(index, propertyNames.lastIndexOf(name));133 });134 }135 });...
Using AI Code Generation
1var sinon = require('sinon');2var iterator = {3 add: function (arg) {4 this.args.push(arg);5 }6};7var spy = sinon.spy(iterator, 'add');8iterator.add('foo');9iterator.add('bar');10console.log(spy.args.map(function (args) {11 return args[0];12}));
Using AI Code Generation
1var sinon = require('sinon');2var iterator = {3 add: function (arg) {4 this.args.push(arg);5 }6};7var spy = sinon.spy(iterator, 'add');8iterator.add('foo');9iterator.add('bar');10console.log(spy.args.map(function (args) {11 return args[0];12}));
Using AI Code Generation
1var args = iterator.args.map(function (call) {2 return call[0];3});4var thisValues = iterator.thisValues.map(function (call) {5 return call[0];6});7var returnValues = iterator.returnValues.map(function (call) {8 return call[0];9});10var exceptions = iterator.exceptions.map(function (call) {11 return call[0];12});13var thisValues = iterator.thisValues.map(function (call) {14 return call[0];15});16var returnValues = iterator.returnValues.map(function (call) {17 return call[0];18});19var exceptions = iterator.exceptions.map(function (call) {20 return call[0];21});22var thisValues = iterator.thisValues.map(function (call) {23 return call[0];24});25var returnValues = iterator.returnValues.map(function (call) {26 return call[0];27});28var exceptions = iterator.exceptions.map(function (call) {29 return call[0];30});31var thisValues = iterator.thisValues.map(function (call) {
Using AI Code Generation
1describe("test", function() {2 it("test", function() {3 var callback = sinon.spy();4 callback(1, 2, 3);5 expect(callback.args.map(function(args) {6 return args.slice(0, 2);7 })).to.deep.equal([8 ]);9 });10});11describe("test", function() {12 it("test", function() {13 var callback = sinon.spy();14 callback(1, 2, 3);15 expect(callback.args.map(function(args) {16 return args.slice(0, 2);17 })).to.deep.equal([18 ]);19 });20});
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!!