Best JavaScript code snippet using ng-mocks
ietabUtils.js
Source:ietabUtils.js
1//2// ietabUtils.js3//4// Copyright (C) 2012 yuoo2k5//6// This program is free software: you can redistribute it and/or modify7// it under the terms of the GNU General Public License as published by8// the Free Software Foundation, either version 3 of the License, or9// (at your option) any later version.10//11// This program is distributed in the hope that it will be useful,12// but WITHOUT ANY WARRANTY; without even the implied warranty of13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14// GNU General Public License for more details.15//16// You should have received a copy of the GNU General Public License17// along with this program. If not, see <http:www.gnu.org/licenses/>.18//19//-----------------------------------------------------------------------------20IeTab.prototype.mlog = function(text) {21 Components.classes["@mozilla.org/consoleservice;1"]22 .getService(Components.interfaces.nsIConsoleService)23 .logStringMessage("IeTab: "+text);24}25//-----------------------------------------------------------------------------26IeTab.prototype.addEventListener = function(obj, type, listener) {27 if (typeof(obj) == "string") obj = document.getElementById(obj);28 if (obj) obj.addEventListener(type, listener, false);29}30IeTab.prototype.removeEventListener = function(obj, type, listener) {31 if (typeof(obj) == "string") obj = document.getElementById(obj);32 if (obj) obj.removeEventListener(type, listener, false);33}34IeTab.prototype.addEventListenerByTagName = function(tag, type, listener) {35 var objs = document.getElementsByTagName(tag);36 for (var i = 0; i < objs.length; i++) {37 objs[i].addEventListener(type, listener, false);38 }39}40IeTab.prototype.removeEventListenerByTagName = function(tag, type, listener) {41 var objs = document.getElementsByTagName(tag);42 for (var i = 0; i < objs.length; i++) {43 objs[i].removeEventListener(type, listener, false);44 }45}46//-----------------------------------------------------------------------------47IeTab.prototype.hookCode = function(orgFunc, orgCode, myCode) {48 try{ if (eval(orgFunc).toString() == eval(orgFunc + "=" + eval(orgFunc).toString().replace(orgCode, myCode))) throw orgFunc; }49 catch(e){ Components.utils.reportError("Failed to hook function: "+orgFunc); }50}51/*52// We need to find a better way to implement this53IeTab.prototype.hookFunc = function(orgFunc, myFunc) {54 try{55 var oldFunc = orgFunc;56 orgFunc = function() {57 if(myFunc())58 return;59 oldFunc();60 }61 }62 catch(e){63 Components.utils.reportError("Failed to hook function: "+orgFunc);64 }65}66*/67IeTab.prototype.hookAttr = function(parentNode, attrName, myFunc) {68 if (typeof(parentNode) == "string") parentNode = document.getElementById(parentNode);69 try { parentNode.setAttribute(attrName, myFunc + parentNode.getAttribute(attrName)); }catch(e){ Components.utils.reportError("Failed to hook attribute: "+attrName); }70}71IeTab.prototype.hookProp = function(parentNode, propName, myGetter, mySetter) {72 var oGetter = parentNode.__lookupGetter__(propName);73 var oSetter = parentNode.__lookupSetter__(propName);74 if (oGetter && myGetter) myGetter = oGetter.toString().replace(/{/, "{"+myGetter.toString().replace(/^.*{/,"").replace(/.*}$/,""));75 if (oSetter && mySetter) mySetter = oSetter.toString().replace(/{/, "{"+mySetter.toString().replace(/^.*{/,"").replace(/.*}$/,""));76 if (!myGetter) myGetter = oGetter;77 if (!mySetter) mySetter = oSetter;78 if (myGetter) try { eval('parentNode.__defineGetter__(propName, '+ myGetter.toString() +');'); }catch(e){ Components.utils.reportError("Failed to hook property Getter: "+propName); }79 if (mySetter) try { eval('parentNode.__defineSetter__(propName, '+ mySetter.toString() +');'); }catch(e){ Components.utils.reportError("Failed to hook property Setter: "+propName); }80}81/*82orz, This does not work at all.83// http://jordanwallwork.co.uk/2013/02/intercepting-properties-with-getterssetters/84IeTab.prototype.hookProp2 = function(obj, propName, myGetter, mySetter) {85 try{86 var desc = Object.getOwnPropertyDescriptor(obj, propName);87 if(!desc) {88 var protoType = Object.getPrototypeOf(obj);89 var desc = Object.getOwnPropertyDescriptor(protoType, propName);90 }91 var oGetter = desc.get;92 var oSetter = desc.set;93 // alert(desc + "\n" + oGetter + "\n" + oSetter);94 desc = {95 configurable: true,96 enumerable: true97 };98 if(myGetter)99 desc.get = myGetter;100 else if(oGetter)101 desc.get = oGetter;102 if(mySetter)103 desc.set = mySetter;104 else if(oSetter)105 desc.set = oSetter;106 Object.defineProperty(obj, propName, desc);107 desc = Object.getOwnPropertyDescriptor(obj, propName);108 alert(desc + "\n" + desc.get + "\n" + desc.set);109 }110 catch(e) {111 alert(protoType + propName + e);112 }113}114*/115//-----------------------------------------------------------------------------116IeTab.prototype.trim = function(s) {117 if (s) return s.replace(/^\s+/g,"").replace(/\s+$/g,""); else return "";118}119IeTab.prototype.startsWith = function(s, prefix) {120 if (s) return( (s.substring(0, prefix.length) == prefix) ); else return false;121}122//-----------------------------------------------------------------------------123IeTab.prototype.getBoolPref = function(prefName, defval) {124 var result = defval;125 var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);126 var prefs = prefservice.getBranch("extensions.");127 if (prefs.getPrefType(prefName) == prefs.PREF_BOOL) {128 try { result = prefs.getBoolPref(prefName); }catch(e){}129 }130 return(result);131}132IeTab.prototype.getIntPref = function(prefName, defval) {133 var result = defval;134 var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);135 var prefs = prefservice.getBranch("extensions.");136 if (prefs.getPrefType(prefName) == prefs.PREF_INT) {137 try { result = prefs.getIntPref(prefName); }catch(e){}138 }139 return(result);140}141IeTab.prototype.getStrPref = function(prefName, defval) {142 var result = defval;143 var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);144 var prefs = prefservice.getBranch("extensions.");145 if (prefs.getPrefType(prefName) == prefs.PREF_STRING) {146 try { result = prefs.getComplexValue(prefName, Components.interfaces.nsISupportsString).data; }catch(e){}147 }148 return(result);149}150//-----------------------------------------------------------------------------151IeTab.prototype.setBoolPref = function(prefName, value) {152 var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);153 var prefs = prefservice.getBranch("extensions.");154 try { prefs.setBoolPref(prefName, value); } catch(e){}155}156IeTab.prototype.setIntPref = function(prefName, value) {157 var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);158 var prefs = prefservice.getBranch("extensions.");159 try { prefs.setIntPref(prefName, value); } catch(e){}160}161IeTab.prototype.setStrPref = function(prefName, value) {162 var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);163 var prefs = prefservice.getBranch("extensions.");164 var sString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);165 sString.data = value;166 try { prefs.setComplexValue(prefName, Components.interfaces.nsISupportsString, sString); } catch(e){}167}168//-----------------------------------------------------------------------------169IeTab.prototype.getDefaultCharset = function(defval) {170 var charset = this.getStrPref("ietab.intl.charset.default", "");171 if (charset.length) return charset;172 var gPrefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);173 if(gPrefs.prefHasUserValue("intl.charset.default")) {174 return gPrefs.getCharPref("intl.charset.default");175 } else {176 var strBundle = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);177 var intlMess = strBundle.createBundle("chrome://global-platform/locale/intl.properties");178 try {179 return intlMess.GetStringFromName("intl.charset.default");180 } catch(e) {181 return defval;182 }183 }184}185IeTab.prototype.convertToUTF8 = function(data, charset) {186 try {187 data = decodeURI(data);188 }catch(e){189 if (!charset) charset = gIeTab.getDefaultCharset();190 if (charset) {191 var uc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);192 try {193 uc.charset = charset;194 data = uc.ConvertToUnicode(unescape(data));195 data = decodeURI(data);196 }catch(e){}197 uc.Finish();198 }199 }200 return data;201}202IeTab.prototype.convertToASCII = function(data, charset) {203 if (!charset) charset = gIeTab.getDefaultCharset();204 if (charset) {205 var uc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);206 uc.charset = charset;207 try {208 data = uc.ConvertFromUnicode(data);209 }catch(e){210 data = uc.ConvertToUnicode(unescape(data));211 data = decodeURI(data);212 data = uc.ConvertFromUnicode(data);213 }214 uc.Finish();215 }216 return data;217}218//-----------------------------------------------------------------------------219IeTab.prototype.getUrlDomain = function(url) {220 if (url && !gIeTab.startsWith(url, "about:")) {221 if (/^file:\/\/.*/.test(url)) return url;222 var matches = url.match(/^([A-Za-z]+:\/+)*([^\:^\/]+):?(\d*)(\/.*)*/);223 if (matches) url = matches[1]+matches[2]+(matches[3]==""?"":":"+matches[3])+"/";224 }225 return url;226}227IeTab.prototype.getUrlHost = function(url) {228 if (url && !gIeTab.startsWith(url, "about:")) {229 if (/^file:\/\/.*/.test(url)) return url;230 var matches = url.match(/^([A-Za-z]+:\/+)*([^\:^\/]+):?(\d*)(\/.*)*/);231 if (matches) url = matches[2];232 }233 return url;234}235//-----------------------------------------------------------------------------236var keyCodeNames = new Array();237// initialize keyCodeNames238function initKeyCodeNames() {239 keyCodeNames[0x03] = "VK_CANCEL";240 keyCodeNames[0x08] = "VK_BACK";241 keyCodeNames[0x09] = "VK_TAB";242 keyCodeNames[0x0C] = "VK_CLEAR";243 keyCodeNames[0x0D] = "VK_RETURN";244 keyCodeNames[0x0D] = "VK_ENTER"245 keyCodeNames[0x10] = "VK_SHIFT"246 keyCodeNames[0x11] = "VK_CONTROL";247 keyCodeNames[0x12] = "VK_ALT";248 keyCodeNames[0x13] = "VK_PAUSE";249 keyCodeNames[0x14] = "VK_CAPS_LOCK";250 keyCodeNames[0x1B] = "VK_ESCAPE";251 keyCodeNames[0x20] = "VK_SPACE";252 keyCodeNames[0x21] = "VK_PAGE_UP";253 keyCodeNames[0x22] = "VK_PAGE_DOWN";254 keyCodeNames[0x23] = "VK_END";255 keyCodeNames[0x24] = "VK_HOME";256 keyCodeNames[0x25] = "VK_LEFT";257 keyCodeNames[0x26] = "VK_UP";258 keyCodeNames[0x27] = "VK_RIGHT";259 keyCodeNames[0x28] = "VK_DOWN";260 keyCodeNames[0x2A] = "VK_PRINTSCREEN";261 keyCodeNames[0x2D] = "VK_INSERT";262 keyCodeNames[0x2E] = "VK_DELETE";263 //keyCodeNames[] = "VK_SEMICOLON";264 //keyCodeNames[] = "VK_EQUALS";265 for(i = 0; i <=9; ++i) { // numpad 0-9266 keyCodeNames[0x60 + i] = ("VK_NUMPAD" + i);267 }268 keyCodeNames[0x6A] = "VK_MULTIPLY";269 keyCodeNames[0x6B] = "VK_ADD";270 keyCodeNames[0x6C] = "VK_SEPARATOR";271 keyCodeNames[0x6D] = "VK_SUBTRACT";272 keyCodeNames[0x6E] = "VK_DECIMAL";273 keyCodeNames[0x6F] = "VK_DIVIDE";274 for(i = 1; i <= 24; ++i) { // F1 - F24275 keyCodeNames[0x6F + i] = "VK_F" + i;276 }277 keyCodeNames[0x90] = "VK_NUM_LOCK";278 keyCodeNames[0x91] = "VK_SCROLL_LOCK";279 keyCodeNames[0xBC] = "VK_COMMA";280 keyCodeNames[0xBE] = "VK_PERIOD";281 // keyCodeNames[] = "VK_SLASH";282 // keyCodeNames[] = "VK_BACK_QUOTE";283 // keyCodeNames[] = "VK_OPEN_BRACKET";284 // keyCodeNames[] = "VK_BACK_SLASH";285 // keyCodeNames[] = "VK_CLOSE_BRACKET";286 // keyCodeNames[] = "VK_QUOTE";287 keyCodeNames[0x2F] = "VK_HELP";288}289initKeyCodeNames();290// convert virtual key code from value to string291IeTab.prototype.keyCodeToString = function(keycode_val) {292 return keyCodeNames[keycode_val];...
test-dev-nonstd-setget-key-argument.js
Source:test-dev-nonstd-setget-key-argument.js
...32 // key: property name (non-standard)33 print('myGetter', this === obj, typeof key, key);34 return (typeof key === 'string' ? key.toUpperCase() : key);35 }36 function mySetter(val, key) {37 // 'this' binding: target object (standard)38 // val: property value (standard)39 // key: property name (non-standard)40 print('mySetter', this === obj, typeof val, val, typeof key, key);41 }42 obj = {};43 Object.defineProperties(obj, {44 'foo': { enumerable: true, configurable: true, get: myGetter, set: mySetter },45 'bar': { enumerable: true, configurable: true, get: myGetter, set: mySetter },46 '1': { enumerable: true, configurable: true, get: myGetter, set: mySetter }47 });48 // Normal property reads49 print('obj.foo:', obj.foo);50 print('obj["bar"]:', obj['bar']);...
Using AI Code Generation
1import { mySetter } from 'ng-mocks';2import { TestComponent } from './test.component';3describe('TestComponent', () => {4 let testComponent: TestComponent;5 beforeEach(() => {6 testComponent = new TestComponent();7 });8 it('should set value', () => {9 mySetter(testComponent, 'value', 'test');10 expect(testComponent.value).toEqual('test');11 });12});13import { Component } from '@angular/core';14@Component({15})16export class TestComponent {17 value: string;18}19import { ComponentFixture, TestBed } from '@angular/core/testing';20import { TestComponent } from './test.component';21describe('TestComponent', () => {22 let testComponent: TestComponent;23 let fixture: ComponentFixture<TestComponent>;24 beforeEach(async () => {25 await TestBed.configureTestingModule({26 }).compileComponents();27 });28 beforeEach(() => {29 fixture = TestBed.createComponent(TestComponent);30 testComponent = fixture.componentInstance;31 fixture.detectChanges();32 });33 it('should set value', () => {34 testComponent.value = 'test';35 expect(testComponent.value).toEqual('test');36 });37});
Using AI Code Generation
1const mock = ngMocks.findInstance(MyClass);2mock.mySetter = 'myValue';3const mock = ngMocks.findInstance(MyClass);4mock.mySetter = 'myValue';5const mock = ngMocks.findInstance(MyClass);6mock.mySetter = 'myValue';7const mock = ngMocks.findInstance(MyClass);8mock.mySetter = 'myValue';9const mock = ngMocks.findInstance(MyClass);10mock.mySetter = 'myValue';11const mock = ngMocks.findInstance(MyClass);12mock.mySetter = 'myValue';13const mock = ngMocks.findInstance(MyClass);14mock.mySetter = 'myValue';15const mock = ngMocks.findInstance(MyClass);16mock.mySetter = 'myValue';17const mock = ngMocks.findInstance(MyClass);18mock.mySetter = 'myValue';19const mock = ngMocks.findInstance(MyClass);20mock.mySetter = 'myValue';21const mock = ngMocks.findInstance(MyClass);22mock.mySetter = 'myValue';23const mock = ngMocks.findInstance(MyClass);24mock.mySetter = 'myValue';25const mock = ngMocks.findInstance(MyClass);26mock.mySetter = 'myValue';27const mock = ngMocks.findInstance(MyClass);28mock.mySetter = 'myValue';
Using AI Code Generation
1mySetter('myInput', 'myValue');2mySetter('myInput', 'myValue', 'myForm');3mySetter('myInput', 'myValue', 'myForm', 'myFormName');4mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController');5mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController', 'myFormControllerName');6mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController', 'myFormControllerName', 'myInputController');7mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController', 'myFormControllerName', 'myInputController', 'myInputControllerName');8mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController', 'myFormControllerName', 'myInputController', 'myInputControllerName', 'myInputControllerScope');9mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController', 'myFormControllerName', 'myInputController', 'myInputControllerName', 'myInputControllerScope', 'myInputControllerScopeName');10mySetter('myInput', 'myValue', 'myForm', 'myFormName', 'myFormController', 'myFormControllerName', 'myInputController', 'myInputControllerName', 'myInputControllerScope', 'myInputControllerScopeName', 'myInputControllerScopeElement');
Using AI Code Generation
1mySetter('myInput', 'myValue');2mySetter('myInput', 'myValue');3mySetter('myInput', 'myValue');4mySetter('myInput', 'myValue');5mySetter('myInput', 'myValue');6mySetter('myInput', 'myValue');7mySetter('myInput', 'myValue');8mySetter('myInput', 'myValue');9mySetter('myInput', 'myValue');10mySetter('myInput', 'myValue');
Using AI Code Generation
1mySetter('myValue');2myGetter();3myMethod('myValue');4it('should call mySetter', function() {5 spyOn(mySetter, 'mySetter');6 mySetter('myValue');7 expect(mySetter.mySetter).toHaveBeenCalled();8});9it('should call myGetter', function() {10 spyOn(myGetter, 'myGetter');11 myGetter();12 expect(myGetter.myGetter).toHaveBeenCalled();13});14it('should call myMethod', function() {15 spyOn(myMethod, 'myMethod');16 myMethod('myValue');17 expect(myMethod.myMethod).toHaveBeenCalled();18});19mySetter('myValue');20myGetter();21myMethod('myValue');22it('should call mySetter', function() {23 spyOn(mySetter, 'mySetter');24 mySetter('myValue');25 expect(mySetter.mySetter).toHaveBeenCalled();26});27it('should call myGetter', function() {28 spyOn(myGetter, 'myGetter');29 myGetter();30 expect(myGetter.myGetter).toHaveBeenCalled();31});32it('should call myMethod', function() {33 spyOn(myMethod, 'myMethod');34 myMethod('myValue');35 expect(myMethod.myMethod).toHaveBeenCalled();36});
Using AI Code Generation
1mySetter('myInput', 'myValue');2myGetter('myInput');3myElement('myInput');4myInjector();5myCompile();6myModule();7myProvider('myProvider');8myService('myService');9myController('myController');10myDirective('myDirective');11myFactory('myFactory');12myFilter('myFilter');13myConstant('myConstant');14myValue('myValue');15myComponent('myComponent');16myInject(function(myDependency, $httpBackend) {17});18myHttpBackend();19myFlush();20myInterval();
Using AI Code Generation
1var mySetter = ngMocks.get('$controller').prototype.$setViewValue;2mySetter.call(this, 'myValue');3ngMocks.get('$rootScope').$digest();4expect(this.mySetter).toHaveBeenCalledWith('myValue');5ngMocks.mock('myService', {6 myMethod: function() {7 return 'myValue';8 }9});10var myController = ngMocks.inject('myController');11var value = myController.myMethod();12expect(value).toEqual('myValue');13ngMocks.mock('$http', {14 get: function() {15 return 'myValue';16 }17});18var myController = ngMocks.inject('myController');19var value = myController.myMethod();20expect(value).toEqual('myValue');21ngMocks.mock('$q', {22 defer: function() {23 return 'myValue';24 }25});26var myController = ngMocks.inject('myController');27var value = myController.myMethod();28expect(value).toEqual('myValue');
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!!