Best JavaScript code snippet using ng-mocks
winjs.polyfill.promise.test.ts
Source:winjs.polyfill.promise.test.ts
1/*---------------------------------------------------------------------------------------------2 * Copyright (c) Microsoft Corporation. All rights reserved.3 * Licensed under the MIT License. See License.txt in the project root for license information.4 *--------------------------------------------------------------------------------------------*/5'use strict';6import * as assert from 'assert';7import { PolyfillPromise } from 'vs/base/common/winjs.polyfill.promise';8import { Promise as WinJSPromise } from 'vs/base/common/winjs.base';9suite('Polyfill Promise', function () {10 test('sync-resolve, NativePromise', function () {11 // native promise behaviour12 const actual: string[] = [];13 const promise = new Promise(resolve => {14 actual.push('inCtor');15 resolve(null);16 }).then(() => actual.push('inThen'));17 actual.push('afterCtor');18 return promise.then(() => {19 assert.deepEqual(actual, ['inCtor', 'afterCtor', 'inThen']);20 });21 });22 test('sync-resolve, WinJSPromise', function () {23 // winjs promise behaviour24 const actual: string[] = [];25 const promise = new WinJSPromise(resolve => {26 actual.push('inCtor');27 resolve(null);28 }).then(() => actual.push('inThen'));29 actual.push('afterCtor');30 return promise.then(() => {31 assert.deepEqual(actual, ['inCtor', 'inThen', 'afterCtor']);32 });33 });34 test('sync-resolve, PolyfillPromise', function () {35 // winjs promise behaviour36 const actual: string[] = [];37 const promise = new PolyfillPromise(resolve => {38 actual.push('inCtor');39 resolve(null);40 }).then(() => actual.push('inThen'));41 actual.push('afterCtor');42 return promise.then(() => {43 assert.deepEqual(actual, ['inCtor', 'afterCtor', 'inThen']);44 });45 });46 test('sync-then, NativePromise', function () {47 const actual: string[] = [];48 const promise = Promise.resolve(123).then(() => actual.push('inThen'));49 actual.push('afterThen');50 return promise.then(() => {51 assert.deepEqual(actual, ['afterThen', 'inThen']);52 });53 });54 test('sync-then, WinJSPromise', function () {55 const actual: string[] = [];56 const promise = WinJSPromise.as(123).then(() => actual.push('inThen'));57 actual.push('afterThen');58 return promise.then(() => {59 assert.deepEqual(actual, ['inThen', 'afterThen']);60 });61 });62 test('sync-then, PolyfillPromise', function () {63 const actual: string[] = [];64 const promise = PolyfillPromise.resolve(123).then(() => actual.push('inThen'));65 actual.push('afterThen');66 return promise.then(() => {67 assert.deepEqual(actual, ['afterThen', 'inThen']);68 });69 });70 test('PolyfillPromise, executor has two params', function () {71 return new PolyfillPromise(function () {72 assert.equal(arguments.length, 2);73 assert.equal(typeof arguments[0], 'function');74 assert.equal(typeof arguments[1], 'function');75 arguments[0]();76 });77 });78 // run the same tests for the native and polyfill promise79 (<any[]>[Promise, PolyfillPromise]).forEach(PromiseCtor => {80 test(PromiseCtor.name + ', resolved value', function () {81 return new PromiseCtor((resolve: Function) => resolve(1)).then((value: number) => assert.equal(value, 1));82 });83 test(PromiseCtor.name + ', rejected value', function () {84 return new PromiseCtor((_: Function, reject: Function) => reject(1)).then(null, (value: number) => assert.equal(value, 1));85 });86 test(PromiseCtor.name + ', catch', function () {87 return new PromiseCtor((_: Function, reject: Function) => reject(1)).catch((value: number) => assert.equal(value, 1));88 });89 test(PromiseCtor.name + ', static-resolve', function () {90 return PromiseCtor.resolve(42).then((value: number) => assert.equal(value, 42));91 });92 test(PromiseCtor.name + ', static-reject', function () {93 return PromiseCtor.reject(42).then(null, (value: number) => assert.equal(value, 42));94 });95 test(PromiseCtor.name + ', static-all, 1', function () {96 return PromiseCtor.all([97 PromiseCtor.resolve(1),98 PromiseCtor.resolve(2)99 ]).then((values: number[]) => {100 assert.deepEqual(values, [1, 2]);101 });102 });103 test(PromiseCtor.name + ', static-all, 2', function () {104 return PromiseCtor.all([105 PromiseCtor.resolve(1),106 3,107 PromiseCtor.resolve(2)108 ]).then((values: number[]) => {109 assert.deepEqual(values, [1, 3, 2]);110 });111 });112 test(PromiseCtor.name + ', static-all, 3', function () {113 return PromiseCtor.all([114 PromiseCtor.resolve(1),115 PromiseCtor.reject(13),116 PromiseCtor.reject(12),117 ]).catch((values: number) => {118 assert.deepEqual(values, 13);119 });120 });121 test(PromiseCtor.name + ', static-race, 1', function () {122 return PromiseCtor.race([123 PromiseCtor.resolve(1),124 PromiseCtor.resolve(2),125 ]).then((value: number) => {126 assert.deepEqual(value, 1);127 });128 });129 test(PromiseCtor.name + ', static-race, 2', function () {130 return PromiseCtor.race([131 PromiseCtor.reject(-1),132 PromiseCtor.resolve(2),133 ]).catch((value: number) => {134 assert.deepEqual(value, -1);135 });136 });137 test(PromiseCtor.name + ', static-race, 3', function () {138 return PromiseCtor.race([139 PromiseCtor.resolve(1),140 PromiseCtor.reject(2),141 ]).then((value: number) => {142 assert.deepEqual(value, 1);143 });144 });145 test(PromiseCtor.name + ', throw in ctor', function () {146 return new PromiseCtor(() => {147 throw new Error('sooo bad');148 }).catch((err: Error) => {149 assert.equal(err.message, 'sooo bad');150 });151 });152 });...
mixin.ts
Source:mixin.ts
1// tslint:disable: max-classes-per-file2import { Document } from 'mongoose';3import { Model, SubDocument, Resource } from './model';4import { Ctor } from '../utils';5import * as Base from './base';6import { gtSchemaStore } from '../store/schema-store';7function mixObjects(base: any, mixins: any[]): void {8 mixins.forEach(mixin => {9 Object.getOwnPropertyNames(mixin)10 .concat(Object.getOwnPropertySymbols(mixin) as any)11 .forEach(name => {12 // mixin can't override base behavior, only add13 if (!base.hasOwnProperty(name)) {14 // if its a property descriptor we need to rewire the context15 const propDesc = Object.getOwnPropertyDescriptor(mixin, name);16 if (propDesc) {17 Object.defineProperty(base, name, propDesc);18 } else {19 base[name] = mixin[name];20 }21 }22 });23 });24}25export function GtQuery<Q1>(QH: Ctor<Q1>): <T, C>(Cls: Ctor<Document & T> & Model & C) => Ctor<Document & T> & C & Model<Q1>26export function GtQuery<Q1, Q2>(Q1: Ctor<Q1>, Q2: Ctor<Q2>): <T, C>(Cls: Ctor<Document & T> & Model & C) => Ctor<Document & T> & C & Model<Q1 & Q2>27export function GtQuery<Q1, Q2, Q3>(Q1: Ctor<Q1>, Q2: Ctor<Q2>, Q3: Ctor<Q3>): <T, C>(Cls: Ctor<Document & T> & Model & C) => Ctor<Document & T> & C & Model<Q1 & Q2 & Q3>28export function GtQuery<Q1, Q2, Q3, Q4>(Q1: Ctor<Q1>, Q2: Ctor<Q2>, Q3: Ctor<Q3>, Q4: Ctor<Q4>): <T, C>(Cls: Ctor<Document & T> & Model & C) => Ctor<Document & T> & C & Model<Q1 & Q2 & Q3 & Q4>29export function GtQuery<QMIXIN>(...mixins: Array<Ctor<QMIXIN>>): <T, C>(Cls: Ctor<Document & T> & Model & C) => Ctor<Document & T> & C & Model<QMIXIN> {30 return <T, C>(Cls: Ctor<Document & T> & Model & C) => {31 if (mixins.length > 0) {32 class QueryHelper { }33 mixObjects(QueryHelper.prototype, mixins.map(m => m.prototype));34 mixObjects(QueryHelper, mixins);35 gtSchemaStore.getCreate(Cls).defineQueryHelper(QueryHelper);36 }37 return Cls as any;38 }39}40export function GtModel(): Ctor<Document> & Model;41export function GtModel<T1, C1>(m1: C1 & Ctor<T1>): Ctor<Document & T1> & Model & C1;42export function GtModel<T1, C1, T2, C2>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>): Ctor<Document & T1 & T2> & Model & C1 & C2;43export function GtModel<T1, C1, T2, C2, T3, C3>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>, m3: C3 & Ctor<T3>): Ctor<Document & T1 & T2 & T3> & Model & C1 & C2 & C3;44export function GtModel<T1, C1, T2, C2, T3, C3, T4, C4>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>, m3: C3 & Ctor<T3>, m4: C4 & Ctor<T4>): Ctor<Document & T1 & T2 & T3 & T4> & Model & C1 & C2 & C3 & C4;45export function GtModel<T1, C1, T2, C2, T3, C3, T4, C4, T5, C5>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>, m3: C3 & Ctor<T3>, m4: C4 & Ctor<T4>, m5: C5 & Ctor<T5>): Ctor<Document & T1 & T2 & T3 & T4 & T5> & Model & C1 & C2 & C3 & C4 & C5;46export function GtModel<TMIXIN, CMIXIN>(...mixins: Array<CMIXIN & Ctor<TMIXIN>>): Ctor<TMIXIN> & CMIXIN {47 class GtModelContainer extends Base.GtModelContainer { }48 if (mixins.length > 0) {49 mixObjects(GtModelContainer.prototype, mixins.map(m => m.prototype));50 mixObjects(GtModelContainer, mixins);51 gtSchemaStore.getCreate(GtModelContainer).defineMixins(mixins);52 }53 return GtModelContainer as any;54}55export function GtResource(): Ctor<SubDocument> & Resource;56export function GtResource<T1, C1>(m1: C1 & Ctor<T1>): Ctor<SubDocument & T1> & Resource & C1;57export function GtResource<T1, C1, T2, C2>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>): Ctor<SubDocument & T1 & T2> & Resource & C1 & C2;58export function GtResource<T1, C1, T2, C2, T3, C3>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>, m3: C3 & Ctor<T3>): Ctor<SubDocument & T1 & T2 & T3> & Resource & C1 & C2 & C3;59export function GtResource<T1, C1, T2, C2, T3, C3, T4, C4>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>, m3: C3 & Ctor<T3>, m4: C4 & Ctor<T4>): Ctor<SubDocument & T1 & T2 & T3 & T4> & Resource & C1 & C2 & C3 & C4;60export function GtResource<T1, C1, T2, C2, T3, C3, T4, C4, T5, C5>(m1: C1 & Ctor<T1>, m2: C2 & Ctor<T2>, m3: C3 & Ctor<T3>, m4: C4 & Ctor<T4>, m5: C5 & Ctor<T5>): Ctor<SubDocument & T1 & T2 & T3 & T4 & T5> & Resource & C1 & C2 & C3 & C4 & C5;61export function GtResource<TMIXIN, CMIXIN>(...mixins: Array<CMIXIN & Ctor<TMIXIN>>): Ctor<TMIXIN> & CMIXIN {62 class GtResourceContainer extends Base.GtResourceContainer { }63 if (mixins.length > 0) {64 mixObjects(GtResourceContainer.prototype, mixins.map(m => m.prototype));65 mixObjects(GtResourceContainer, mixins);66 gtSchemaStore.getCreate(GtResourceContainer).defineMixins(mixins);67 }68 return GtResourceContainer as any;...
util.d.ts
Source:util.d.ts
1 declare const noop: () => void;2 declare const hasProto: boolean;3 interface VueDecorator {4 (Ctor: typeof Vue): void;5 (target: Vue, key: string): void;6 (target: Vue, key: string, index: number): void;7}8declare namespace VueClassComponent {9export function createDecorator(factory: (options: ComponentOptions<Vue>, key: string, index: number) => void): VueDecorator;10}11 //declare function mixins<A>(CtorA: VueClass<A>): VueClass<A>;12 //declare function mixins<A, B>(CtorA: VueClass<A>, CtorB: VueClass<B>): VueClass<A & B>;13 //declare function mixins<A, B, C>(CtorA: VueClass<A>, CtorB: VueClass<B>, CtorC: VueClass<C>): VueClass<A & B & C>;14 //declare function mixins<A, B, C, D>(CtorA: VueClass<A>, CtorB: VueClass<B>, CtorC: VueClass<C>, CtorD: VueClass<D>): VueClass<A & B & C & D>;15 //declare function mixins<A, B, C, D, E>(CtorA: VueClass<A>, CtorB: VueClass<B>, CtorC: VueClass<C>, CtorD: VueClass<D>, CtorE: VueClass<E>): VueClass<A & B & C & D & E>;16 //declare function mixins<T>(...Ctors: VueClass<Vue>[]): VueClass<T>;17 declare function isPrimitive(value: any): boolean;18 declare function warn(message: string): void;19 interface ImixinsExtends {20 <A>(CtorA: VueClass<A>): VueClass<A>;21 <A, B>(CtorA: VueClass<A>, CtorB: VueClass<B>): VueClass<A & B>;22 <A, B, C>(CtorA: VueClass<A>, CtorB: VueClass<B>, CtorC: VueClass<C>): VueClass<A & B & C>;23 <A, B, C, D>(CtorA: VueClass<A>, CtorB: VueClass<B>, CtorC: VueClass<C>, CtorD: VueClass<D>): VueClass<A & B & C & D>;24 <A, B, C, D, E>(CtorA: VueClass<A>, CtorB: VueClass<B>, CtorC: VueClass<C>, CtorD: VueClass<D>, CtorE: VueClass<E>): VueClass<A & B & C & D & E>;25 <T>(...Ctors: VueClass<Vue>[]): VueClass<T>;...
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { AppComponent } from './app.component';3describe('AppComponent', () => {4 beforeEach(() => MockBuilder(AppComponent));5 it('should create the app', () => {6 const fixture = MockRender(AppComponent);7 const app = ngMocks.findInstance(AppComponent);8 expect(app).toBeTruthy();9 });10});
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { MyComponent } from './my.component';3describe('MyComponent', () => {4 beforeEach(() => MockBuilder(MyComponent));5 it('should create', () => {6 const fixture = MockRender(MyComponent);7 expect(ngMocks.formatText(fixture)).toEqual('Hello World');8 });9});10import { Component } from '@angular/core';11@Component({12})13export class MyComponent {}14import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';15import { MyComponent } from './my.component';16describe('MyComponent', () => {17 beforeEach(() => MockBuilder(MyComponent));18 it('should create', () => {19 const fixture = MockRender(MyComponent);20 expect(ngMocks.formatText(fixture)).toEqual('Hello World');21 });22});23import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';24import { MyComponent } from './my.component';25describe('MyComponent', () => {26 beforeEach(() => MockBuilder(MyComponent));27 it('should create', () => {28 const fixture = MockRender(MyComponent);29 expect(ngMocks.formatText(fixture)).toEqual('Hello World');30 });31});32import { Component } from '@angular/core';33@Component({34})35export class MyComponent {}36import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';37import { MyComponent } from './my.component';38describe('MyComponent', () => {39 beforeEach(() => MockBuilder(MyComponent));40 it('should create', () => {41 const fixture = MockRender(MyComponent);42 expect(ngMocks.formatText(fixture)).toEqual('Hello World');43 });44});
Using AI Code Generation
1import { MockBuilder, MockRender } from 'ng-mocks';2import { AppModule } from './app.module';3import { AppComponent } from './app.component';4beforeEach(() => MockBuilder(AppComponent, AppModule));5it('renders the component', () => {6 const fixture = MockRender(AppComponent);7 expect(fixture.point.componentInstance).toBeDefined();8});9import { MockBuilder, MockRender } from 'ng-mocks';10import { AppModule } from './app.module';11import { AppComponent } from './app.component';12beforeEach(() => MockBuilder(AppComponent, AppModule));13it('renders the component', () => {14 const fixture = MockRender(AppComponent);15 expect(fixture.point.componentInstance).toBeDefined();16});17import { MockBuilder, MockRender } from 'ng-mocks';18import { AppModule } from './app.module';19import { AppComponent } from './app.component';20beforeEach(() => MockBuilder(AppComponent, AppModule));21it('renders the component', () => {22 const fixture = MockRender(AppComponent);23 expect(fixture.point.componentInstance).toBeDefined();24});25import { MockBuilder, MockRender } from 'ng-mocks';26import { AppModule } from './app.module';27import { AppComponent } from './app.component';28beforeEach(() => MockBuilder(AppComponent, AppModule));29it('renders the component', () => {30 const fixture = MockRender(AppComponent);31 expect(fixture.point.componentInstance).toBeDefined();32});33import { MockBuilder, MockRender } from 'ng-mocks';34import { AppModule } from './app.module';35import { AppComponent } from './app.component';36beforeEach(() => MockBuilder(AppComponent, AppModule));37it('renders the component', () => {38 const fixture = MockRender(AppComponent);39 expect(fixture.point.componentInstance).toBeDefined();40});41import { MockBuilder, MockRender } from 'ng-mocks';42import { AppModule } from './app.module';43import { AppComponent } from './app.component';44beforeEach(() => MockBuilder(AppComponent, AppModule));
Using AI Code Generation
1import { MockBuilder, MockRender } from 'ng-mocks';2import { AppModule } from './app.module';3beforeEach(() => MockBuilder(AppModule));4it('renders', () => MockRender(AppComponent));5import { NgModule } from '@angular/core';6import { AppComponent } from './app.component';7@NgModule({8})9export class AppModule {}10import { Component } from '@angular/core';11@Component({12})13export class AppComponent {}14describe('AppComponent', () => {15 it('renders', () => {16 const fixture = MockRender(AppComponent);17 expect(fixture.nativeElement.innerHTML).toContain('Hello World');18 });19});20import { MockBuilder, MockRender } from 'ng-mocks';21describe('AppComponent', () => {22 beforeEach(() => MockBuilder(AppComponent));23 it('renders', () => {24 const fixture = MockRender(AppComponent);25 expect(fixture.nativeElement.innerHTML).toContain('Hello World');26 });27});28import { MockBuilder, MockRender, MockInstance } from 'ng-mocks';29describe('AppComponent', () => {30 beforeEach(() =>31 MockBuilder(AppComponent).mock(MyService, {32 get: () => 'Hello World',33 }),34 );35 it('renders', () => {36 const fixture = MockRender(AppComponent);37 expect(fixture.nativeElement.innerHTML).toContain('Hello World');38 });39 it('renders the service', () => {40 const fixture = MockRender(AppComponent);41 expect(42 fixture.debugElement.query(By.directive(MyServiceComponent)).nativeElement43 ).toContain('Hello World');44 });45});46import { MockBuilder, MockRender, MockInstance } from 'ng-mocks';47describe('AppComponent', () => {48 beforeEach(() =>49 MockBuilder(AppComponent).mock(MyService, {50 get: () => 'Hello World',51 }),52 );
Using AI Code Generation
1import { MockBuilder, MockRender } from 'ng-mocks';2import { AppModule } from './app.module';3beforeEach(() => MockBuilder().mock(AppModule));4afterEach(() => MockBuilder().reset());5import { TestBed } from '@angular/core/testing';6import { AppModule } from './app.module';7beforeEach(() => TestBed.configureTestingModule({ imports: [AppModule] }));8afterEach(() => TestBed.resetTestingModule());9import { NgModule } from '@angular/core';10import { BrowserModule } from '@angular/platform-browser';11import { AppComponent } from './app.component';12@NgModule({13 imports: [BrowserModule],14})15export class AppModule {}16import { Component } from '@angular/core';17@Component({18 <p>{{ message }}</p>19})20export class AppComponent {21 message = 'Hello World';22}23import { MockRender } from 'ng-mocks';24describe('AppComponent', () => {25 it('should create the app', () => {26 const fixture = MockRender(AppComponent);27 const app = fixture.point.componentInstance;28 expect(app).toBeTruthy();29 });30 it(`should have as title 'app'`, () => {31 const fixture = MockRender(AppComponent);32 const app = fixture.point.componentInstance;33 expect(app.message).toEqual('Hello World');34 });35 it('should render title in a h1 tag', () => {36 const fixture = MockRender(AppComponent);37 expect(fixture.point.nativeElement.querySelector('h1').textContent).toContain(38 );39 });40});41import { TestBed } from '@angular/core/testing';42import { AppComponent } from './app.component';43describe('AppComponent', () => {44 beforeEach(() => TestBed.configureTestingModule({ declarations: [AppComponent] }));45 afterEach(() => TestBed.resetTestingModule());46 it('should create the app', () => {47 const fixture = TestBed.createComponent(AppComponent);48 const app = fixture.componentInstance;49 expect(app).toBeTruthy();50 });51 it(`should have as title 'app'`, () => {52 const fixture = TestBed.createComponent(AppComponent);53 const app = fixture.componentInstance;54 expect(app.message).toEqual
Using AI Code Generation
1const mock = ngMocks.findInstance(ChildComponent);2const mock = jasmine.createSpyObj(ChildComponent, ['method1', 'method2']);3constructor() {4 super();5}6constructor() {7 super();8}9describe('ChildComponent', () => {10 beforeEach(() => {11 TestBed.configureTestingModule({12 providers: [ngMocks.guts(ChildComponent)],13 });14 });15 it('should work', () => {16 const fixture = TestBed.createComponent(ChildComponent);17 const component = fixture.componentInstance;18 expect(component).toBeTruthy();19 });20});21describe('ChildComponent', () => {22 beforeEach(() => {23 TestBed.configureTestingModule({24 providers: [jasmine.createSpyObj(ChildComponent, ['method1', 'method2'])],25 });26 });27 it('should work', () => {28 const fixture = TestBed.createComponent(ChildComponent);29 const component = fixture.componentInstance;30 expect(component).toBeTruthy();31 });32});33const mock = ngMocks.findInstance(ChildComponent);34const mock = jasmine.createSpyObj(ChildComponent, ['method1', 'method2']);35constructor() {36 super();37}38constructor() {39 super();40}41describe('ChildComponent', () => {42 beforeEach(() => {43 TestBed.configureTestingModule({44 providers: [ngMocks.guts(ChildComponent)],45 });46 });47 it('should work', () => {48 const fixture = TestBed.createComponent(ChildComponent);49 const component = fixture.componentInstance;50 expect(component).toBeTruthy();51 });52});53describe('ChildComponent', () => {54 beforeEach(() => {55 TestBed.configureTestingModule({
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!!