Best JavaScript code snippet using ng-mocks
utils.spec.ts
Source:utils.spec.ts
1import { isPrimitive, isUndefined , uuidv4, isArray, isBoolean, isNull, isNullOrUndefined, isNumber, isString, isSymbol, isRegExp, isObject, isDate, isError, isFunction, from, select, of } from "../utils";23describe('util function collection', () => {45 it('uuidv4', () => {6 const u1 = uuidv4();7 const u2 = uuidv4();8 expect(u1).not.toEqual(u2);9 });1011 it('isArray', () => {12 expect(isArray([])).toBeTruthy();13 });1415 it('isBoolean', () => {16 expect(isBoolean(false)).toBeTruthy();17 });1819 it('isNull', () => {20 expect(isNull(null)).toBeTruthy();21 });2223 it('isNullOrUndefined', () => {24 expect(isNullOrUndefined(null)).toBeTruthy();25 });2627 it('isNumber', () => {28 expect(isNumber(0)).toBeTruthy();29 expect(isNumber(NaN)).toBeTruthy();30 expect(isNumber('')).toBeFalsy();31 });3233 it('isString', () => {34 expect(isString('')).toBeTruthy();35 expect(isString(42)).toBeFalsy();36 });3738 it('isSymbol', () => {39 expect(isSymbol(Symbol())).toBeTruthy();40 });4142 it('isUndefined', () => {43 expect(isUndefined(undefined)).toBeTruthy();44 });4546 it('isRegExp', () => {47 expect(isRegExp(/Test/i)).toBeTruthy();48 });4950 it('isObject', () => {51 expect(isObject({})).toBeTruthy();52 });5354 it('isDate', () => {55 expect(isDate(new Date())).toBeTruthy();56 });5758 it('isError', () => {59 const err = new Error();60 expect(isError(err)).toBeTruthy();61 });6263 it('isFunction', () => {64 function test(){}65 expect(isFunction(test)).toBeTruthy();66 });6768 it('isPrimitive', () => {69 expect(isPrimitive(null)).toBeTruthy();70 expect(isPrimitive('boolean')).toBeTruthy();71 expect(isPrimitive('number')).toBeTruthy();72 expect(isPrimitive('string')).toBeTruthy();73 expect(isPrimitive('symbol')).toBeTruthy();74 expect(isPrimitive('undefined')).toBeTruthy();75 });7677 describe('function of', () => {78 it('simple', () => {79 function test() { }80 const result = `@@${test.name}@@`;81 expect(result).toEqual('@@test@@');82 });8384 it('class', () => {85 class MockOf {86 name: string = '';87 }88 const result = of<MockOf>(o => o.name);89 expect(result).toEqual('@@name@@');90 });9192 it('class', () => {93 class MockOf {94 name: string = '';95 }96 const result = of<MockOf>(MockOf);97 expect(result).toEqual('@@MockOf@@');98 });99 });100101 it('from', () => {102 const data = [];103 expect(from(data)).toMatch('[]');104 });105106 it('select', () => {107 class MockModel {108 name: string = 'test';109 }110 expect(select<MockModel>((d) => d.name)).toMatch('@@name@@');111 });112
...
voices.test.ts
Source:voices.test.ts
2import fakeVoice from '../../test/helpers/fakeVoice'3import { getUSEnglishVoice } from './voices'4describe('getUSEnglishVoice', () => {5 it('returns nothing given no voices', () => {6 mockOf(speechSynthesis.getVoices).mockReturnValue([])7 expect(getUSEnglishVoice()).toBeUndefined()8 })9 it('never returns non-local voices', () => {10 mockOf(speechSynthesis.getVoices).mockReturnValue([11 fakeVoice({ localService: false, name: 'Google US English' }),12 ])13 expect(getUSEnglishVoice()).toBeUndefined()14 })15 it.each([16 'cmu_us_slt_arctic_hts festival',17 'cmu_us_slt_arctic_clunits festival',18 ])('prefers the CMU voice "%s"', (name) => {19 mockOf(speechSynthesis.getVoices).mockReturnValue([20 // Preferred over default21 fakeVoice({ default: true }),22 // Preferred over US English23 fakeVoice({ name: 'US English' }),24 // Preferred over English25 fakeVoice({ name: 'English' }),26 // Preferred over lang matches27 fakeVoice({ lang: 'en-US' }),28 fakeVoice({ name }),29 ])30 expect(getUSEnglishVoice()?.name).toBe(name)31 })32 it('prefers US English to UK English', () => {33 mockOf(speechSynthesis.getVoices).mockReturnValue([34 fakeVoice({ name: 'Google UK English' }),35 fakeVoice({ name: 'Google US English' }),36 ])37 expect(getUSEnglishVoice()?.name).toBe('Google US English')38 })39 it('prefers en-US over en', () => {40 mockOf(speechSynthesis.getVoices).mockReturnValue([41 fakeVoice({ lang: 'en' }),42 fakeVoice({ lang: 'en-US' }),43 ])44 expect(getUSEnglishVoice()?.lang).toBe('en-US')45 })46 it('prefers en over en-GB', () => {47 mockOf(speechSynthesis.getVoices).mockReturnValue([48 fakeVoice({ lang: 'en-GB' }),49 fakeVoice({ lang: 'en' }),50 ])51 expect(getUSEnglishVoice()?.lang).toBe('en')52 })53 it('falls back to the default if there is one', () => {54 mockOf(speechSynthesis.getVoices).mockReturnValue([55 fakeVoice({ lang: 'af', default: false }),56 fakeVoice({ default: true }),57 fakeVoice({ name: 'Google æ®éè¯ï¼ä¸å½å¤§éï¼', default: false }),58 ])59 expect(getUSEnglishVoice()?.default).toBe(true)60 })61 it('falls back to the first one if none match or are the default', () => {62 mockOf(speechSynthesis.getVoices).mockReturnValue([63 fakeVoice({ lang: 'af', default: false }),64 fakeVoice({ name: 'Google æ®éè¯ï¼ä¸å½å¤§éï¼', default: false }),65 ])66 expect(getUSEnglishVoice()?.lang).toBe('af')67 })...
mock-proxy.ts
Source:mock-proxy.ts
1// ReturnType maps a function type to the type that function returns, or any if no return type can be inferred2type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;3// MockOf maps function types to a jest mock that wrap that function's return type, and non-function types to themself4type MockOf<T> = T extends Function ? jest.Mock<ReturnType<T>, any> : T;5// Turn all functions in a given type into mocks of those functions. Leave non-functions unnaffected6export type MockProxy<T> = { [TP in keyof T]: MockOf<T[TP]> };7export function MockProxy<T>(withBase: Partial<T> = {}): MockProxy<T> {8 const handler = {9 // Angular uses '$quoted$' in the module compiler to find quoted properties10 // We don't need to worry about that here it just needs to be iterable11 get: (obj: any, prop: string) => prop === '$quoted$' ? [] : obj[prop] || (obj[prop] = jest.fn())12 };13 return new Proxy(withBase, handler);...
Using AI Code Generation
1var mockOf = require('ng-mocks-universal').mockOf;2describe('Test', () => {3 it('should create', () => {4 const service = mockOf(ExampleService);5 expect(service).toBeTruthy();6 });7});8import 'ng-mocks-universal';
Using AI Code Generation
1import { mockOf } from 'ng-mocks';2import { MyService } from './my-service';3const myServiceMock = mockOf(MyService);4myServiceMock.myMethod.and.returnValue('Mocked Value');5import { Injectable } from '@angular/core';6@Injectable()7export class MyService {8 myMethod() {9 return 'Real Value';10 }11}12import { mockOf } from 'ng-mocks';13import { MyService } from './my-service';14const myServiceMock = mockOf(MyService);15myServiceMock.myMethod.mockImplementation(() => 'Mocked Value');16import { Injectable } from '@angular/core';17@Injectable()18export class MyService {19 myMethod() {20 return 'Real Value';21 }22}23import { mockOf } from 'ng-mocks';24import { MyService } from './my-service';25const myServiceMock = mockOf(MyService);26myServiceMock.myMethod.mockImplementation(() => 'Mocked Value');
Using AI Code Generation
1import { mockOf } from 'ng-mocks';2const mockService = mockOf(Service);3mockService.method();4import { mockOf } from 'ng-mocks';5const mockService = mockOf(Service, { method: () => 'mocked' });6mockService.method();7import { mockOf } from 'ng-mocks';8const mockService = mockOf(Service, { method: () => 'mocked' });9mockService.method();10import { mockOf } from 'ng-mocks';11const mockService = mockOf(Service, { method: () => 'mocked' });12mockService.method();13import { mockOf } from 'ng-mocks';14const mockService = mockOf(Service, { method: () => 'mocked' });15mockService.method();16import { mockOf } from 'ng-mocks';17const mockService = mockOf(Service, { method: () => 'mocked' });18mockService.method();19import { mockOf } from 'ng-mocks';20const mockService = mockOf(Service, { method: () => 'mocked' });21mockService.method();22import { mockOf } from 'ng-mocks';23const mockService = mockOf(Service, { method: () => 'mocked' });24mockService.method();25import { mockOf } from 'ng-mocks';26const mockService = mockOf(Service, { method: () =>
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!!