Best JavaScript code snippet using ng-mocks
delegateService.unit.js
Source:delegateService.unit.js
1describe('DelegateFactory', function() {2 function setup(methods) {3 var delegate;4 inject(function($log, $injector) {5 delegate = $injector.instantiate(ionic.DelegateService(methods || []));6 });7 return delegate;8 }9 it('should have properties', function() {10 expect(setup()._instances).toEqual([]);11 expect(setup()._registerInstance).toEqual(jasmine.any(Function));12 expect(setup().$getByHandle).toEqual(jasmine.any(Function));13 });14 it('should allow reg & dereg of instance with handle', function() {15 var delegate = setup();16 var instance = {};17 var deregister = delegate._registerInstance(instance, 'handle');18 expect(instance.$$delegateHandle).toBe('handle');19 expect(delegate._instances[0]).toBe(instance);20 deregister();21 expect(delegate._instances.length).toBe(0);22 });23 it('should allow reg & dereg of instance, without handle', function() {24 var delegate = setup();25 var instance = {};26 var deregister = delegate._registerInstance(instance, null);27 expect(instance.$$delegateHandle).toBe(null);28 expect(delegate._instances[0]).toBe(instance);29 deregister();30 expect(delegate._instances.length).toBe(0);31 });32 it('should have given methodNames on root', function() {33 var delegate = setup(['foo', 'bar']);34 expect(delegate.foo).toEqual(jasmine.any(Function));35 expect(delegate.bar).toEqual(jasmine.any(Function));36 });37 it('should call given methodNames on all instances with args', function() {38 var delegate = setup(['foo', 'bar']);39 var instance1 = {40 foo: jasmine.createSpy('foo1'),41 bar: jasmine.createSpy('bar1')42 };43 var instance2 = {44 foo: jasmine.createSpy('foo1'),45 bar: jasmine.createSpy('bar1')46 };47 delegate._registerInstance(instance1);48 delegate._registerInstance(instance2);49 expect(instance1.foo).not.toHaveBeenCalled();50 expect(instance2.foo).not.toHaveBeenCalled();51 delegate.foo('a', 'b', 'c');52 expect(instance1.foo).toHaveBeenCalledWith('a', 'b', 'c');53 expect(instance2.foo).toHaveBeenCalledWith('a', 'b', 'c');54 instance1.foo.reset();55 instance2.foo.reset();56 delegate.foo(1, 2, 3);57 expect(instance1.foo).toHaveBeenCalledWith(1, 2, 3);58 expect(instance2.foo).toHaveBeenCalledWith(1, 2, 3);59 expect(instance1.bar).not.toHaveBeenCalled();60 expect(instance2.bar).not.toHaveBeenCalled();61 delegate.bar('something');62 expect(instance1.bar).toHaveBeenCalledWith('something');63 expect(instance2.bar).toHaveBeenCalledWith('something');64 });65 it('should return the first return value from multiple instances', function() {66 var delegate = setup(['fn']);67 var instance1 = {68 fn: jasmine.createSpy('fn').andReturn('ret1')69 };70 var instance2 = {71 fn: jasmine.createSpy('fn').andReturn('ret2')72 };73 var deregister = delegate._registerInstance(instance1);74 delegate._registerInstance(instance2);75 expect(delegate.fn()).toBe('ret1');76 deregister();77 expect(delegate.fn()).toBe('ret2');78 });79 it('should return the first active instance return value when multiple instances w/ undefined handle', function() {80 var delegate = setup(['fn']);81 var instance1 = {82 fn: jasmine.createSpy('fn').andReturn('ret1')83 };84 var instance2 = {85 fn: jasmine.createSpy('fn').andReturn('ret2')86 };87 delegate._registerInstance(instance1, undefined, function() {88 return false;89 });90 delegate._registerInstance(instance2, undefined, function() {91 return true;92 });93 expect(instance1.fn).not.toHaveBeenCalled();94 expect(delegate.fn()).toBe('ret2');95 });96 it('should return the first active instance return value when multiple instances w/ same handle', function() {97 var delegate = setup(['fn']);98 var instance1 = {99 fn: jasmine.createSpy('fn').andReturn('ret1')100 };101 var instance2 = {102 fn: jasmine.createSpy('fn').andReturn('ret2')103 };104 delegate._registerInstance(instance1, 'myhandle', function() {105 return true;106 });107 delegate._registerInstance(instance2, 'myhandle', function() {108 return false;109 });110 expect(instance2.fn).not.toHaveBeenCalled();111 expect(delegate.fn()).toBe('ret1');112 });113 describe('$getByHandle', function() {114 var delegate, instance1, instance2, instance3;115 beforeEach(function() {116 delegate = setup(['a']);117 instance1 = {118 a: jasmine.createSpy('a1').andReturn('a1')119 };120 instance2 = {121 a: jasmine.createSpy('a2').andReturn('a2')122 };123 instance3 = {124 a: jasmine.createSpy('a3').andReturn('a3')125 };126 });127 it('should return an InstanceWithHandle object with fields', function() {128 expect(delegate.$getByHandle('one').a).toEqual(jasmine.any(Function));129 expect(delegate.$getByHandle('two').a).toEqual(jasmine.any(Function));130 expect(delegate.$getByHandle('invalid').a).toEqual(jasmine.any(Function));131 });132 it('should noop & warn if calling for a non-added instance', inject(function($log) {133 spyOn($log, 'warn');134 expect(delegate.$getByHandle('one').a()).toBeUndefined();135 expect($log.warn).toHaveBeenCalled();136 }));137 it('should call an added instance\'s method', function() {138 delegate._registerInstance(instance1, '1');139 delegate._registerInstance(instance2, '2');140 var result = delegate.$getByHandle('1').a(1,2,3);141 expect(instance1.a).toHaveBeenCalledWith(1,2,3);142 expect(instance2.a).not.toHaveBeenCalled();143 expect(result).toBe('a1');144 instance1.a.reset();145 result = delegate.$getByHandle('2').a(2,3,4);146 expect(instance2.a).toHaveBeenCalledWith(2,3,4);147 expect(instance1.a).not.toHaveBeenCalled();148 expect(result).toBe('a2');149 });150 it('should call multiple instances with the same handle', function() {151 var deregister = delegate._registerInstance(instance1, '1');152 delegate._registerInstance(instance2, '1');153 delegate._registerInstance(instance3, 'other');154 var delegateInstance = delegate.$getByHandle('1');155 expect(instance1.a).not.toHaveBeenCalled();156 expect(instance2.a).not.toHaveBeenCalled();157 expect(instance3.a).not.toHaveBeenCalled();158 var result = delegateInstance.a('1');159 expect(instance1.a).toHaveBeenCalledWith('1');160 expect(instance2.a).toHaveBeenCalledWith('1');161 expect(instance3.a).not.toHaveBeenCalled();162 expect(result).toBe('a1');163 instance1.a.reset();164 deregister();165 result = delegateInstance.a(2);166 expect(instance1.a).not.toHaveBeenCalled();167 expect(instance2.a).toHaveBeenCalledWith(2);168 expect(instance3.a).not.toHaveBeenCalled();169 expect(result).toBe('a2');170 });171 it('should get the only active instance from multiple instances with the same handle', function() {172 delegate._registerInstance(instance1, 'myhandle', function() { return false; });173 delegate._registerInstance(instance2, 'myhandle', function() { return true; });174 delegate._registerInstance(instance3, 'myhandle', function() { return false; });175 var delegateInstance = delegate.$getByHandle('myhandle');176 expect(instance1.a).not.toHaveBeenCalled();177 expect(instance2.a).not.toHaveBeenCalled();178 expect(instance3.a).not.toHaveBeenCalled();179 var result = delegateInstance.a('test-a');180 expect(instance1.a).not.toHaveBeenCalled();181 expect(instance2.a).toHaveBeenCalledWith('test-a');182 expect(instance3.a).not.toHaveBeenCalled();183 expect(result).toBe('a2');184 });185 it('should get active instance when multiple sname name handles, among multiple active instances', function() {186 delegate._registerInstance(instance1, 'myhandle-1', function() { return true; });187 delegate._registerInstance(instance2, 'myhandle-2', function() { return false; });188 delegate._registerInstance(instance3, 'myhandle-2', function() { return true; });189 var delegateInstance = delegate.$getByHandle('myhandle-2');190 expect(instance1.a).not.toHaveBeenCalled();191 expect(instance2.a).not.toHaveBeenCalled();192 expect(instance3.a).not.toHaveBeenCalled();193 var result = delegateInstance.a('test-a');194 expect(instance1.a).not.toHaveBeenCalled();195 expect(instance2.a).not.toHaveBeenCalled();196 expect(instance3.a).toHaveBeenCalledWith('test-a');197 expect(result).toBe('a3');198 });199 });...
app.js
Source:app.js
1const { readFileSync } = require("fs");2const assert = require('assert')3const table = new WebAssembly.Table({ initial: 10, element: "anyfunc" });4function showTable(tbl) {5 v = [];6 for (let i = 0; i < tbl.length; i++) {7 v.push(tbl.get(i) != null ? 1 : 0);8 }9 console.log(v);10}11// Global Offset Table12const GOT = {};13function GOTMemHandler(obj, symName) {14 let rtn = GOT[symName];15 if (!rtn) {16 rtn = GOT[symName] = new WebAssembly.Global(17 {18 value: "i32",19 mutable: true,20 },21 instance.exports[symName]22 );23 }24 return rtn;25}26let nextTablePos = 2;27const funcMap = {};28function GOTFuncHandler(obj, symName) {29 let rtn = GOT[symName];30 if (!rtn) {31 // place in the table32 funcMap[symName] = nextTablePos;33 rtn = GOT[symName] = new WebAssembly.Global(34 {35 value: "i32",36 mutable: true,37 },38 nextTablePos39 );40 nextTablePos += 1;41 }42 return rtn;43}44//45// Load the main non-dynamic WebAssembly instance46//47const memory = new WebAssembly.Memory({ initial: 50, maximum: 1000 });48const stack_pointer = new WebAssembly.Global(49 { value: "i32", mutable: true },50 6553651);52const opts = {53 env: {54 memory,55 __indirect_function_table: table,56 // TODO: simulating some of what dlopen/dlsym will give us here:57 pointer_to_add10: () => {58 return instance2.exports.pointer_to_add10();59 },60 pointer_to_add389: () => {61 return instance2.exports.pointer_to_add389();62 },63 },64};65const binary = new Uint8Array(readFileSync("dist/app.wasm"));66const mod = new WebAssembly.Module(binary);67const instance = new WebAssembly.Instance(mod, opts);68console.log("instance.exports = ", instance.exports);69showTable(table);70//71// Load the dynamic module72//73const binary2 = new Uint8Array(readFileSync("dist/dynamic-library.wasm.so"));74const mod2 = new WebAssembly.Module(binary2);75const stack_pointer2 = new WebAssembly.Global(76 { value: "i32", mutable: true },77 50000078);79const opts2 = {80 env: {81 memory,82 __indirect_function_table: table,83 __memory_base: 500000, // TODO: need to use malloc84 __table_base: 0,85 __stack_pointer: stack_pointer2, // TODO: no clue what the input of this is.86 },87 "GOT.mem": new Proxy(GOT, { get: GOTMemHandler }),88 "GOT.func": new Proxy(GOT, { get: GOTFuncHandler }),89};90const instance2 = new WebAssembly.Instance(mod2, opts2);91console.log("instance2.exports = ", instance2.exports);92for (const symName in funcMap) {93 console.log(`table: setting position ${funcMap[symName]} to ${symName}`);94 table.set(funcMap[symName], instance2.exports[symName]);95 delete funcMap[symName];96}97console.log("instance2.exports.add10(22) = ", instance2.exports.add10(22));98assert(instance2.exports.add10(22) == 32);99table.set(2, instance2.exports.add10);100showTable(table);101console.log("instance.exports.add10(22) =", instance.exports.add10(22));102assert(instance.exports.add10(22) == 32);103console.log("instance.exports.add389(22) =", instance.exports.add389(22));104assert(instance.exports.add389(22) == 389 + 22);105console.log("instance.exports.a_pynone() =", instance.exports.pynone_a());106console.log("instance2.exports.b_pynone() =", instance2.exports.pynone_b());107assert(instance.exports.pynone_a() == instance2.exports.pynone_b());108console.log("instance.exports.a_pysome() =", instance.exports.pysome_a());109console.log("instance2.exports.b_pysome() =", instance2.exports.pysome_b());110assert(instance.exports.pysome_a() == instance2.exports.pysome_b());111console.log(112 "instance2.exports.my_add10(2020) =",113 instance2.exports.my_add10(2020)114);115assert(instance2.exports.my_add10(2020) == 2030);...
index.js
Source:index.js
1import Vue from 'vue'2import Vuex from 'vuex'3import Cookie from 'vue-cookies'4Vue.use(Vuex)5export default new Vuex.Store({6 // ç»ä»¶ä¸éè¿ this.$store.state.username è°ç¨7 state: {8 username: Cookie.get("username"),9 addr: Cookie.get("addr"),10 poolInstance: {},11 poolInstance2: {},12 delegateInstance: {},13 delegateInstance2: {},14 larktokenInstance: {},15 larktokenInstance2: {},16 ercAbi: [],17 artInstance: {},18 artInstance2: {},19 20 photoInstance: {},21 photoInstance2: {},22 created: [],23 photoCreated: []24 },25 mutations: {26 // ç»ä»¶ä¸éè¿ this.$store.commit('saveToken',åæ°) è°ç¨,åªè½å¸¦ä¸ä¸ªåæ°ï¼æå¤ä¸ªåæ°ä»¥å¯¹è±¡çå½¢å¼ä¼ å
¥27 saveUser: function (state, userobj) {28 state.username = userobj.username29 state.addr = userobj.addr30 Cookie.set("username", userobj.username, "30d")31 Cookie.set("addr", userobj.addr, "30d")32 },33 savePoolInstance: function (state, instance){34 state.poolInstance = instance35 },36 savePoolInstance2: function (state, instance2){37 state.poolInstance2 = instance238 },39 saveDelegateInstance: function (state, instance){40 state.delegateInstance = instance41 },42 saveDelegateInstance2: function (state, instance2){43 state.delegateInstance2 = instance244 },45 savelarktokenInstance: function (state, instance){46 state.larktokenInstance = instance47 },48 savelarktokenInstance2: function (state, instance2){49 state.larktokenInstance2 = instance250 },51 saveERCAbi: function (state, abi){52 state.ercAbi = abi53 },54 saveArtInstance: function (state, instance){55 state.artInstance = instance56 },57 saveArtInstance2: function (state, instance2){58 state.artInstance2 = instance259 },60 61 savePhotoInstance: function (state, instance){62 state.photoInstance = instance63 },64 savePhotoInstance2: function (state, instance2){65 state.photoInstance2 = instance266 },67 saveCreated: function (state, instance){68 state.created = instance69 },70 savePhotoCreated: function (state, instance){71 state.photoCreated = instance72 },73 74 clearUser: function (state) {75 state.username = null76 state.password = null77 state.active = null78 state.addr = null79 Cookie.remove('username')80 Cookie.remove('password')81 Cookie.remove('active')82 Cookie.remove('addr')83 }84 }...
Using AI Code Generation
1import { instance2 } from 'ng-mocks';2import { MockBuilder } from 'ng-mocks';3describe('AppComponent', () => {4 beforeEach(() => MockBuilder(AppComponent));5 it('should create the app', () => {6 const fixture = TestBed.createComponent(AppComponent);7 const app = fixture.componentInstance;8 expect(app).toBeTruthy();9 });10 it('should have as title \'angular-app\'', () => {11 const fixture = TestBed.createComponent(AppComponent);12 const app = fixture.componentInstance;13 expect(app.title).toEqual('angular-app');14 });15 it('should render title', () => {16 const fixture = TestBed.createComponent(AppComponent);17 fixture.detectChanges();18 const compiled = fixture.nativeElement;19 expect(compiled.querySelector('.content span').textContent).toContain('angular-app app is running!');20 });21});22import { MockBuilder } from 'ng-mocks';23import { AppComponent } from './app.component';24describe('AppComponent', () => {25 beforeEach(() => MockBuilder(AppComponent));26 it('should create the app', () => {27 const fixture = TestBed.createComponent(AppComponent);28 const app = fixture.componentInstance;29 expect(app).toBeTruthy();30 });31 it('should have as title \'angular-app\'', () => {32 const fixture = TestBed.createComponent(AppComponent);33 const app = fixture.componentInstance;34 expect(app.title).toEqual('angular-app');35 });36 it('should render title', () => {37 const fixture = TestBed.createComponent(AppComponent);38 fixture.detectChanges();39 const compiled = fixture.nativeElement;40 expect(compiled.querySelector('.content span').textContent).toContain('angular-app app is running!');41 });42});43import { MockBuilder } from 'ng-mocks';44import { AppComponent } from './app.component';45describe('AppComponent', () => {46 beforeEach(() => MockBuilder(AppComponent));47 it('should create the app', () => {48 const fixture = TestBed.createComponent(AppComponent);49 const app = fixture.componentInstance;50 expect(app).toBeTruthy();51 });52 it('should have as title \'angular-app\'', () => {53 const fixture = TestBed.createComponent(AppComponent);54 const app = fixture.componentInstance;55 expect(app.title).toEqual('angular-app');56 });57 it('should render title', () => {58 const fixture = TestBed.createComponent(AppComponent);59 fixture.detectChanges();
Using AI Code Generation
1import { MockInstance } from 'ng-mocks';2import { MyService } from './my-service';3import { MyComponent } from './my-component';4describe('MyComponent', () => {5 it('should call MyService', () => {6 const myService = MockInstance(MyService, 'instance2', () => {7 return {8 myMethod: () => 'myValue',9 };10 });11 const myComponent = new MyComponent(myService);12 expect(myComponent.myMethod()).toEqual('myValue');13 });14});
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!!