Best JavaScript code snippet using ng-mocks
failed_resolutions_cache.js
Source:failed_resolutions_cache.js
1/*2Copyright: Ambrosus Inc.3Email: tech@ambrosus.io4This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.5This Source Code Form is âIncompatible With Secondary Licensesâ, as defined by the Mozilla Public License, v. 2.0.6*/7import chai from 'chai';8import FailedResolutionsCache from '../../src/services/failed_resolutions_cache';9import sinon from 'sinon';10const {expect} = chai;11describe('Failed resolutions cache', () => {12 let failedResolutionsCache;13 let clock;14 const ttl = 5;15 const now = 15000000;16 const resolutionId1 = 'cid1';17 const resolutionId2 = 'cid2';18 beforeEach(() => {19 clock = sinon.useFakeTimers(now * 1000);20 failedResolutionsCache = new FailedResolutionsCache();21 });22 afterEach(() => {23 clock.restore();24 });25 it('failed resolutions list should be empty at the beginning', () => {26 expect(failedResolutionsCache.failedResolutionsEndTime).to.deep.equal({});27 });28 it('rememberFailedResolution should set current timestamp to resolutionId', () => {29 failedResolutionsCache.rememberFailedResolution(resolutionId1, ttl);30 clock.tick(3000);31 failedResolutionsCache.rememberFailedResolution(resolutionId2, ttl);32 expect(failedResolutionsCache.failedResolutionsEndTime).to.deep.equal({33 [resolutionId1]: now + ttl,34 [resolutionId2]: now + ttl + 335 });36 });37 describe('didResolutionFailRecently', () => {38 it('returns false when resolution is not saved as failed', () => {39 expect(failedResolutionsCache.didResolutionFailRecently(resolutionId1)).to.be.false;40 });41 it('returns true when resolution has been saved as failed and ttl has not passed', async () => {42 failedResolutionsCache.rememberFailedResolution(resolutionId1, ttl);43 expect(failedResolutionsCache.didResolutionFailRecently(resolutionId1)).to.be.true;44 });45 it('returns false when resolution has been saved as failed but ttl has passed', async () => {46 failedResolutionsCache.rememberFailedResolution(resolutionId1, ttl);47 clock.tick(ttl * 1000);48 expect(failedResolutionsCache.didResolutionFailRecently(resolutionId1)).to.be.false;49 });50 it('removes resolution from cache when ttl has passed', async () => {51 failedResolutionsCache.rememberFailedResolution(resolutionId1, ttl);52 clock.tick(ttl * 1000);53 failedResolutionsCache.didResolutionFailRecently(resolutionId1);54 expect(failedResolutionsCache.failedResolutionsEndTime).to.deep.equal({});55 });56 });57 it('clearOutdatedResolutions removes all outdated resolutions', async () => {58 failedResolutionsCache.rememberFailedResolution(resolutionId1, ttl);59 failedResolutionsCache.rememberFailedResolution(resolutionId2, ttl + 1);60 clock.tick(ttl * 1000);61 failedResolutionsCache.clearOutdatedResolutions();62 expect(failedResolutionsCache.failedResolutionsEndTime).to.deep.equal({63 [resolutionId2]: now + ttl + 164 });65 });...
active_resolutions_cache.js
Source:active_resolutions_cache.js
1/*2Copyright: Ambrosus Inc.3Email: tech@ambrosus.io4This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.5This Source Code Form is âIncompatible With Secondary Licensesâ, as defined by the Mozilla Public License, v. 2.0.6*/7export default class ActiveResolutionsCache {8 constructor(resolutionIdField) {9 this.resolutionIdField = resolutionIdField;10 this.activeResolutionsDict = {};11 }12 get activeResolutions() {13 return this.sortChronologically(Object.values(this.activeResolutionsDict));14 }15 setActiveResolutions(resolutions) {16 this.activeResolutionsDict = {};17 for (const resolution of resolutions) {18 this.add(resolution);19 }20 }21 add(resolution) {22 if (!this.has(resolution[this.resolutionIdField])) {23 if (resolution.count === undefined) {24 resolution.count = 1;25 }26 this.activeResolutionsDict[resolution[this.resolutionIdField]] = resolution;27 }28 }29 has(resolutionId) {30 return this.activeResolutionsDict[resolutionId] !== undefined;31 }32 get(resolutionId) {33 return this.activeResolutionsDict[resolutionId];34 }35 expire(resolutionId) {36 delete this.activeResolutionsDict[resolutionId];37 }38 decreaseActiveCount(resolutionId) {39 if (this.has(resolutionId)) {40 this.activeResolutionsDict[resolutionId].count --;41 if (this.get(resolutionId).count <= 0) {42 this.expire(resolutionId);43 }44 }45 }46 applyIncomingResolutionEvents(startedResolutions, resolvedResolutions, timedOutResolutions) {47 const addAction = (resolution, action) => ({...resolution, action});48 const startedResolutionsWithAction = startedResolutions.map((resolution) => addAction(resolution, () => this.add(resolution)));49 const resolvedResolutionsWithAction = resolvedResolutions.map((resolution) => addAction(resolution, () => this.decreaseActiveCount(resolution[this.resolutionIdField])));50 const timedOutResolutionsWithAction = timedOutResolutions.map((resolution) => addAction(resolution, () => this.expire(resolution[this.resolutionIdField])));51 const resolutionsWithActionList = this.sortChronologically([...startedResolutionsWithAction, ...resolvedResolutionsWithAction, ...timedOutResolutionsWithAction]);52 resolutionsWithActionList.forEach((resolution) => resolution.action());53 }54 sortChronologically(resolutions) {55 return resolutions.sort((left, right) => {56 if (left.blockNumber !== right.blockNumber) {57 return left.blockNumber - right.blockNumber;58 }59 return left.logIndex - right.logIndex;60 });61 }...
image-sharp-fragments.js
Source:image-sharp-fragments.js
1/* eslint-disable */2export const gatsbyImageSharpResolutions = graphql`3 fragment GatsbyImageSharpResolutions on ImageSharpResolutions {4 base645 width6 height7 src8 srcSet9 }10`11export const gatsbyImageSharpResolutionsTracedSVG = graphql`12 fragment GatsbyImageSharpResolutions_tracedSVG on ImageSharpResolutions {13 tracedSVG14 width15 height16 src17 srcSet18 }19`20export const gatsbyImageSharpResolutionsPreferWebp = graphql`21 fragment GatsbyImageSharpResolutions_withWebp on ImageSharpResolutions {22 base6423 width24 height25 src26 srcSet27 srcWebp28 srcSetWebp29 }30`31export const gatsbyImageSharpResolutionsPreferWebpTracedSVG = graphql`32 fragment GatsbyImageSharpResolutions_withWebp_tracedSVG on ImageSharpResolutions {33 tracedSVG34 width35 height36 src37 srcSet38 srcWebp39 srcSetWebp40 }41`42export const gatsbyImageSharpResolutionsNoBase64 = graphql`43 fragment GatsbyImageSharpResolutions_noBase64 on ImageSharpResolutions {44 width45 height46 src47 srcSet48 }49`50export const gatsbyImageSharpResolutionsPreferWebpNoBase64 = graphql`51 fragment GatsbyImageSharpResolutions_withWebp_noBase64 on ImageSharpResolutions {52 width53 height54 src55 srcSet56 srcWebp57 srcSetWebp58 }59`60export const gatsbyImageSharpSizes = graphql`61 fragment GatsbyImageSharpSizes on ImageSharpSizes {62 base6463 aspectRatio64 src65 srcSet66 sizes67 }68`69export const gatsbyImageSharpSizesTracedSVG = graphql`70 fragment GatsbyImageSharpSizes_tracedSVG on ImageSharpSizes {71 tracedSVG72 aspectRatio73 src74 srcSet75 sizes76 }77`78export const gatsbyImageSharpSizesPreferWebp = graphql`79 fragment GatsbyImageSharpSizes_withWebp on ImageSharpSizes {80 base6481 aspectRatio82 src83 srcSet84 srcWebp85 srcSetWebp86 sizes87 }88`89export const gatsbyImageSharpSizesPreferWebpTracedSVG = graphql`90 fragment GatsbyImageSharpSizes_withWebp_tracedSVG on ImageSharpSizes {91 tracedSVG92 aspectRatio93 src94 srcSet95 srcWebp96 srcSetWebp97 sizes98 }99`100export const gatsbyImageSharpSizesNoBase64 = graphql`101 fragment GatsbyImageSharpSizes_noBase64 on ImageSharpSizes {102 aspectRatio103 src104 srcSet105 sizes106 }107`108export const gatsbyImageSharpSizesPreferWebpNoBase64 = graphql`109 fragment GatsbyImageSharpSizes_withWebp_noBase64 on ImageSharpSizes {110 aspectRatio111 src112 srcSet113 srcWebp114 srcSetWebp115 sizes116 }...
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } 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(ngMocks.formatText(fixture)).toEqual('Hello World!');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.nativeElement.innerHTML).toContain('Hello World!');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.nativeElement.innerHTML).toContain('Hello World!');24});25import { MockBuilder, MockInstance, MockRender } from 'ng-mocks';26import { AppModule } from './app.module';27import { AppComponent } from './app.component';28import { AppService } from './app.service';29beforeEach(() => MockBuilder(AppComponent, AppModule));30it('renders the component', () => {31 const fixture = MockRender(AppComponent);32 const service = MockInstance(AppService, 'get');33 expect(service.get).toHaveBeenCalled();34});35import { MockBuilder, MockRender, MockProvider } from 'ng-mocks';36import { AppModule }
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { AppModule } from './app.module';3import { AppComponent } from './app.component';4import { MyService } from './my.service';5describe('AppComponent', () => {6 beforeEach(() => MockBuilder(AppComponent, AppModule));7 it('should create the app', () => {8 const fixture = MockRender(AppComponent);9 const app = fixture.point.componentInstance;10 expect(app).toBeTruthy();11 });12 it('should call the method', () => {13 const service = ngMocks.find(MyService);14 spyOn(service, 'myMethod');15 const fixture = MockRender(AppComponent);16 const component = ngMocks.find(AppComponent);17 component.myMethod();18 expect(service.myMethod).toHaveBeenCalled();19 });20});
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 render the component', () => {6 const fixture = MockRender(AppComponent);7 expect(ngMocks.formatText(fixture)).toEqual('Hello World!');8 });9});10MockBuilder(11 imports?: any[],12): Promise<void>;13If `imports` is provided, it will be used instead of the imports of the component. 14MockRender(15): ComponentFixture<any>;16MockInstance(17): any;
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2describe('MockBuilder', () => {3 beforeEach(() => MockBuilder(MyComponent).mock(MyService));4 it('renders', () => {5 const fixture = MockRender(MyComponent);6 expect(ngMocks.formatText(fixture)).toEqual('hello world');7 });8});9import { MockBuilder, MockRender, MockInstance } from 'ng-mocks';10describe('MockBuilder', () => {11 beforeEach(() => MockBuilder(MyComponent).mock(MyService));12 it('renders', () => {13 const fixture = MockRender(MyComponent);14 expect(ngMocks.formatText(fixture)).toEqual('hello world');15 });16});17import { MockBuilder, MockRender } from 'ng-mocks';18describe('MockBuilder', () => {19 beforeEach(() => MockBuilder(MyComponent).mock(MyService));20 it('renders', () => {21 const fixture = MockRender(MyComponent);22 expect(ngMocks.formatText(fixture)).toEqual('hello world');23 });24});25import { MockBuilder, MockRender, MockService } from 'ng-mocks';26describe('MockBuilder', () => {27 beforeEach(() => MockBuilder(MyComponent).mock(MyService));28 it('renders', () => {29 const fixture = MockRender(MyComponent, {30 providers: [MockService(MyService)],31 });32 expect(ngMocks.formatText(fixture)).toEqual('hello world');33 });34});
Using AI Code Generation
1import {mockComponent} from 'ng-mocks';2const component = mockComponent({3});4import {TestBed} from '@angular/core/testing';5import {TestComponent} from './test';6describe('TestComponent', () => {7 beforeEach(() => {8 TestBed.configureTestingModule({9 });10 });11 it('should render test', () => {12 const fixture = TestBed.createComponent(TestComponent);13 fixture.detectChanges();14 expect(fixture.nativeElement).toMatchSnapshot();15 });16});17import {mockDirective} from 'ng-mocks';18const directive = mockDirective({19});20import {TestBed} from '@angular/core/testing';21import {TestComponent} from './test';22describe('TestComponent', () => {23 beforeEach(() => {24 TestBed.configureTestingModule({25 });26 });27 it('should render test', () => {28 const fixture = TestBed.createComponent(TestComponent);29 fixture.detectChanges();30 expect(fixture.nativeElement).toMatchSnapshot();31 });32});33import {mockPipe} from 'ng-mocks';34const pipe = mockPipe({35});36import {TestBed} from '@angular/core/testing';37import {TestComponent} from './test';38describe('TestComponent', () => {39 beforeEach(() => {40 TestBed.configureTestingModule({41 });42 });43 it('should render test', () => {44 const fixture = TestBed.createComponent(TestComponent);45 fixture.detectChanges();46 expect(fixture.nativeElement).toMatchSnapshot();47 });48});49import {mockRender} from 'ng-mocks';50const component = mockRender(`51`);52import {TestBed} from '@angular/core/testing';53import {TestComponent} from './test';54describe('TestComponent', () => {55 beforeEach(() => {
Using AI Code Generation
1describe('TestComponent', () => {2 let component: TestComponent;3 let fixture: ComponentFixture<TestComponent>;4 beforeEach(() => {5 TestBed.configureTestingModule({6 imports: [HttpClientTestingModule],7 {8 useValue: {9 getUser: () => of({ name: 'Test User' }),10 },11 },12 }).compileComponents();13 });14 beforeEach(() => {15 fixture = TestBed.createComponent(TestComponent);16 component = fixture.componentInstance;17 fixture.detectChanges();18 });19 it('should create', () => {20 TestBed.configureTestingModule({21 imports: [HttpClientTestingModule],22 {23 useValue: {24 getUser: () => of({ name: 'Test User' }),25 },26 },27 }).compileComponents();28 });29});
Using AI Code Generation
1var mock = ngMocks.findInstance(MyComponent);2ngMocks.resolutions(mock).then(function (resolutions) {3 console.log('resolutions', resolutions);4});5var mock = ngMocks.findInstance(MyComponent);6ngMocks.stub(mock, 'foo').callsFake(function () { return 'bar'; });7var mock = ngMocks.findInstance(MyComponent);8ngMocks.throw(mock, 'foo').callsFake(function () { return 'bar'; });9var fixture = ngMocks.findInstance(MyComponent);10var element = fixture.debugElement.nativeElement;11ngMocks.trigger(element, 'click');12var fixture = ngMocks.findInstance(MyComponent);13var element = fixture.debugElement.nativeElement;14ngMocks.type(element, 'foo');15var mock = ngMocks.findInstance(MyComponent);16ngMocks.verify(mock, 'foo', 1);
Using AI Code Generation
1import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';2import { AppComponent } from './app.component';3import { AppService } from './app.service';4ngMocks.faster();5MockBuilder(AppComponent)6 .mock(AppService, {7 get: () => 'mocked',8 });9describe('AppComponent', () => {10 beforeEach(() => MockRender(AppComponent));11 it('should render the component', () => {12 expect(ngMocks.formatText('h1')).toEqual('mocked');13 });14});15import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';16import { AppComponent } from './app.component';17import { AppService } from './app.service';18ngMocks.faster();19MockBuilder(AppComponent)20 .mock(AppService, {21 get: () => 'mocked',22 });23describe('AppComponent', () => {24 beforeEach(() => MockRender(AppComponent));25 it('should render the component', () => {26 expect(ngMocks.formatText('h1')).toEqual('mocked');27 });28});29import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';30import { AppComponent } from './app.component';31import { AppService } from './app.service';32ngMocks.faster();33MockBuilder(AppComponent)34 .mock(AppService, {35 get: () => 'mocked',36 });
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!!