Best JavaScript code snippet using ng-mocks
getAlertingValidationMessage.test.ts
Source:getAlertingValidationMessage.test.ts
1import { DataSourceSrv } from '@grafana/runtime';2import { DataSourceApi, PluginMeta, DataTransformerConfig } from '@grafana/data';3import { ElasticsearchQuery } from '../../plugins/datasource/elasticsearch/types';4import { getAlertingValidationMessage } from './getAlertingValidationMessage';5describe('getAlertingValidationMessage', () => {6 describe('when called with some targets containing template variables', () => {7 it('then it should return false', async () => {8 let call = 0;9 const datasource: DataSourceApi = ({10 meta: ({ alerting: true } as any) as PluginMeta,11 targetContainsTemplate: () => {12 if (call === 0) {13 call++;14 return true;15 }16 return false;17 },18 name: 'some name',19 } as any) as DataSourceApi;20 const getMock = jest.fn().mockResolvedValue(datasource);21 const datasourceSrv: DataSourceSrv = {22 get: getMock,23 getDataSourceSettingsByUid(): any {},24 };25 const targets: ElasticsearchQuery[] = [26 { refId: 'A', query: '@hostname:$hostname', isLogsQuery: false },27 { refId: 'B', query: '@instance:instance', isLogsQuery: false },28 ];29 const transformations: DataTransformerConfig[] = [];30 const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);31 expect(result).toBe('');32 expect(getMock).toHaveBeenCalledTimes(2);33 expect(getMock).toHaveBeenCalledWith(datasource.name);34 });35 });36 describe('when called with some targets using a datasource that does not support alerting', () => {37 it('then it should return false', async () => {38 const alertingDatasource: DataSourceApi = ({39 meta: ({ alerting: true } as any) as PluginMeta,40 targetContainsTemplate: () => false,41 name: 'alertingDatasource',42 } as any) as DataSourceApi;43 const datasource: DataSourceApi = ({44 meta: ({ alerting: false } as any) as PluginMeta,45 targetContainsTemplate: () => false,46 name: 'datasource',47 } as any) as DataSourceApi;48 const datasourceSrv: DataSourceSrv = {49 get: (name: string) => {50 if (name === datasource.name) {51 return Promise.resolve(datasource);52 }53 return Promise.resolve(alertingDatasource);54 },55 getDataSourceSettingsByUid(): any {},56 };57 const targets: any[] = [58 { refId: 'A', query: 'some query', datasource: 'alertingDatasource' },59 { refId: 'B', query: 'some query', datasource: 'datasource' },60 ];61 const transformations: DataTransformerConfig[] = [];62 const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);63 expect(result).toBe('');64 });65 });66 describe('when called with all targets containing template variables', () => {67 it('then it should return false', async () => {68 const datasource: DataSourceApi = ({69 meta: ({ alerting: true } as any) as PluginMeta,70 targetContainsTemplate: () => true,71 name: 'some name',72 } as any) as DataSourceApi;73 const getMock = jest.fn().mockResolvedValue(datasource);74 const datasourceSrv: DataSourceSrv = {75 get: getMock,76 getDataSourceSettingsByUid(): any {},77 };78 const targets: ElasticsearchQuery[] = [79 { refId: 'A', query: '@hostname:$hostname', isLogsQuery: false },80 { refId: 'B', query: '@instance:$instance', isLogsQuery: false },81 ];82 const transformations: DataTransformerConfig[] = [];83 const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);84 expect(result).toBe('Template variables are not supported in alert queries');85 expect(getMock).toHaveBeenCalledTimes(2);86 expect(getMock).toHaveBeenCalledWith(datasource.name);87 });88 });89 describe('when called with all targets using a datasource that does not support alerting', () => {90 it('then it should return false', async () => {91 const datasource: DataSourceApi = ({92 meta: ({ alerting: false } as any) as PluginMeta,93 targetContainsTemplate: () => false,94 name: 'some name',95 } as any) as DataSourceApi;96 const getMock = jest.fn().mockResolvedValue(datasource);97 const datasourceSrv: DataSourceSrv = {98 get: getMock,99 getDataSourceSettingsByUid(): any {},100 };101 const targets: ElasticsearchQuery[] = [102 { refId: 'A', query: '@hostname:hostname', isLogsQuery: false },103 { refId: 'B', query: '@instance:instance', isLogsQuery: false },104 ];105 const transformations: DataTransformerConfig[] = [];106 const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);107 expect(result).toBe('The datasource does not support alerting queries');108 expect(getMock).toHaveBeenCalledTimes(2);109 expect(getMock).toHaveBeenCalledWith(datasource.name);110 });111 });112 describe('when called with transformations', () => {113 it('then it should return false', async () => {114 const datasource: DataSourceApi = ({115 meta: ({ alerting: true } as any) as PluginMeta,116 targetContainsTemplate: () => false,117 name: 'some name',118 } as any) as DataSourceApi;119 const getMock = jest.fn().mockResolvedValue(datasource);120 const datasourceSrv: DataSourceSrv = {121 get: getMock,122 getDataSourceSettingsByUid(): any {},123 };124 const targets: ElasticsearchQuery[] = [125 { refId: 'A', query: '@hostname:hostname', isLogsQuery: false },126 { refId: 'B', query: '@instance:instance', isLogsQuery: false },127 ];128 const transformations: DataTransformerConfig[] = [{ id: 'A', options: null }];129 const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);130 expect(result).toBe('Transformations are not supported in alert queries');131 expect(getMock).toHaveBeenCalledTimes(0);132 });133 });...
sources.test.ts
Source:sources.test.ts
...49};50test("should resolve from registry using sources", async () => {51 const sources = getSources();52 const registered = await resolve(sources, registryDependency);53 expect(getMock(sources.registry["vba-blocks"].resolve).calls.length).toEqual(1);54 expect(getMock(sources.path.resolve).calls.length).toEqual(0);55 expect(getMock(sources.git.resolve).calls.length).toEqual(0);56 expect(registered).toMatchSnapshot();57});58test("should resolve from path using sources", async () => {59 const sources = getSources();60 const registered = await resolve(sources, pathDependency);61 expect(getMock(sources.registry["vba-blocks"].resolve).calls.length).toEqual(0);62 expect(getMock(sources.path.resolve).calls.length).toEqual(1);63 expect(getMock(sources.git.resolve).calls.length).toEqual(0);64 expect(registered).toMatchSnapshot();65});66test("should resolve from git using sources", async () => {67 const sources = getSources();68 const registered = await resolve(sources, gitDependency);69 expect(getMock(sources.registry["vba-blocks"].resolve).calls.length).toEqual(0);70 expect(getMock(sources.path.resolve).calls.length).toEqual(0);71 expect(getMock(sources.git.resolve).calls.length).toEqual(1);72 expect(registered).toMatchSnapshot();73});74test("should fetch from registry using sources", async () => {75 const sources = getSources();76 const path = await fetch(sources, registryRegistration);77 expect(getMock(sources.registry["vba-blocks"].fetch).calls.length).toEqual(1);78 expect(getMock(sources.path.fetch).calls.length).toEqual(0);79 expect(getMock(sources.git.fetch).calls.length).toEqual(0);80 expect(path).toEqual("registry path");81});82test("should fetch from path using sources", async () => {83 const sources = getSources();84 const path = await fetch(sources, pathRegistration);85 expect(getMock(sources.registry["vba-blocks"].fetch).calls.length).toEqual(0);86 expect(getMock(sources.path.fetch).calls.length).toEqual(1);87 expect(getMock(sources.git.fetch).calls.length).toEqual(0);88 expect(path).toEqual("path");89});90test("should fetch from git using sources", async () => {91 const sources = getSources();92 const path = await fetch(sources, gitRegistration);93 expect(getMock(sources.registry["vba-blocks"].fetch).calls.length).toEqual(0);94 expect(getMock(sources.path.fetch).calls.length).toEqual(0);95 expect(getMock(sources.git.fetch).calls.length).toEqual(1);96 expect(path).toEqual("git path");97});98test("should throw on unknown type", async () => {99 const sources = getSources();100 await expect(resolve(sources, unknownDependency)).rejects.toMatchSnapshot();101 await expect(fetch(sources, unknownRegistration)).rejects.toMatchSnapshot();102});103function getSources() {104 const sources: Sources = {105 registry: {106 "vba-blocks": {107 resolve: jest.fn(_dependency => [registryRegistration]),108 fetch: jest.fn(_registration => "registry path")109 }110 },111 path: {112 resolve: jest.fn(_dependency => [pathRegistration]),113 fetch: jest.fn(_registration => "path")114 },115 git: {116 resolve: jest.fn(_dependency => [gitRegistration]),117 fetch: jest.fn(_registration => "git path")118 }119 };120 return sources;121}122function getMock(value: any): any {123 return value.mock;...
Using AI Code Generation
1import { getMock } from 'ng-mocks';2import { AppComponent } from './app.component';3describe('AppComponent', () => {4 it('should create the app', () => {5 const fixture = getMock(AppComponent);6 const app = fixture.componentInstance;7 expect(app).toBeTruthy();8 });9});10import { getMock } from 'ng-mocks';11import { AppComponent } from './app.component';12describe('AppComponent', () => {13 it('should create the app', () => {14 const fixture = getMock(AppComponent);15 const app = fixture.componentInstance;16 expect(app).toBeTruthy();17 });18});19import { getMock } from 'ng-mocks';20import { AppComponent } from './app.component';21describe('AppComponent', () => {22 it('should create the app', () => {23 const fixture = getMock(AppComponent);24 const app = fixture.componentInstance;25 expect(app).toBeTruthy();26 });27});28import { getMock } from
Using AI Code Generation
1import { getMock } from 'ng-mocks';2import { MyService } from './my.service';3import { MyComponent } from './my.component';4describe('MyComponent', () => {5 let component: MyComponent;6 let service: MyService;7 beforeEach(() => {8 service = getMock(MyService);9 component = new MyComponent(service);10 });11 it('should return the value from the service', () => {12 expect(component.getValue()).toBe('test value');13 });14});
Using AI Code Generation
1import {getMock} from 'ng-mocks';2const mock = getMock(HelloService);3mock.sayHello.and.returnValue('Mocked hello');4import {getMock} from 'ng-mocks';5const mock = getMock(HelloService);6mock.sayHello.and.returnValue('Mocked hello');7import {getMock} from 'ng-mocks';8const mock = getMock(HelloService);9mock.sayHello.and.returnValue('Mocked hello');10import {getMock} from 'ng-mocks';11const mock = getMock(HelloService);12mock.sayHello.and.returnValue('Mocked hello');13import {getMock} from 'ng-mocks';14const mock = getMock(HelloService);15mock.sayHello.and.returnValue('Mocked hello');16import {getMock} from 'ng-mocks';17const mock = getMock(HelloService);18mock.sayHello.and.returnValue('Mocked hello');19import {getMock} from 'ng-mocks';20const mock = getMock(HelloService);21mock.sayHello.and.returnValue('Mocked hello');22import {getMock} from 'ng-mocks';23const mock = getMock(HelloService);24mock.sayHello.and.returnValue('Mocked hello');25import {getMock} from 'ng-mocks';26const mock = getMock(HelloService);27mock.sayHello.and.returnValue('Mocked hello');28import {getMock} from 'ng-mocks';29const mock = getMock(HelloService);30mock.sayHello.and.returnValue('Mocked hello');31import {getMock} from 'ng-mocks';
Using AI Code Generation
1import {getMock} from 'ng-mocks';2const mock = getMock(MyService, 'method');3import {getMock} from 'ng-mocks';4const mock = getMock(MyService, 'method');5import {getMock} from 'ng-mocks';6const mock = getMock(MyService, 'method');7import {getMock} from 'ng-mocks';8const mock = getMock(MyService, 'method');9import {getMock} from 'ng-mocks';10const mock = getMock(MyService, 'method');11import {getMock} from 'ng-mocks';12const mock = getMock(MyService, 'method');13import {getMock} from 'ng-mocks';14const mock = getMock(MyService, 'method');15import {getMock} from 'ng-mocks
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!!