Best JavaScript code snippet using fast-check-monorepo
ValuesAndSeparateKeysToObject.spec.ts
Source:ValuesAndSeparateKeysToObject.spec.ts
1import {2 buildValuesAndSeparateKeysToObjectMapper,3 buildValuesAndSeparateKeysToObjectUnmapper,4} from '../../../../../src/arbitrary/_internals/mappers/ValuesAndSeparateKeysToObject';5describe('buildValuesAndSeparateKeysToObjectMapper', () => {6 it('should create instances with Object prototype', () => {7 // Arrange8 const keys: (string | symbol)[] = [];9 const magicNoValue = Symbol('no-value');10 const values: any[] = [];11 // Act12 const mapper = buildValuesAndSeparateKeysToObjectMapper<any, typeof magicNoValue>(keys, magicNoValue);13 const obj = mapper(values);14 // Assert15 expect(Object.getPrototypeOf(obj)).toBe(Object.prototype);16 });17 it('should replace magic values by no key', () => {18 // Arrange19 const keys: (string | symbol)[] = ['a', 'b', 'c', 'd'];20 const magicNoValue = Symbol('no-value');21 const values: any[] = [undefined, magicNoValue, null, 0];22 // Act23 const mapper = buildValuesAndSeparateKeysToObjectMapper<any, typeof magicNoValue>(keys, magicNoValue);24 const obj = mapper(values);25 // Assert26 expect(obj).toHaveProperty('a');27 expect(obj).not.toHaveProperty('b');28 expect(obj).toHaveProperty('c');29 expect(obj).toHaveProperty('d');30 expect(obj).toEqual({ a: undefined, c: null, d: 0 });31 });32 it('should be able to produce instances with "__proto__" as key', () => {33 // Arrange34 const keys: (string | symbol)[] = ['__proto__'];35 const magicNoValue = Symbol('no-value');36 const values: any[] = [0];37 // Act38 const mapper = buildValuesAndSeparateKeysToObjectMapper<any, typeof magicNoValue>(keys, magicNoValue);39 const obj = mapper(values);40 // Assert41 expect(Object.getPrototypeOf(obj)).toBe(Object.prototype);42 expect(obj).toHaveProperty('__proto__');43 expect(obj.__proto__).toBe(0);44 expect(obj).toEqual({ ['__proto__']: 0 });45 });46});47describe('buildValuesAndSeparateKeysToObjectUnmapper', () => {48 it('should properly unmap basic instances of Object without keys', () => {49 // Arrange50 const obj = {};51 const keys: (string | symbol)[] = [];52 const magicNoValue = Symbol('no-value');53 // Act54 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);55 const values = unmapper(obj);56 // Assert57 expect(values).toEqual([]);58 });59 it('should properly unmap basic instances of Object with multiple keys and no missing', () => {60 // Arrange61 const obj = { a: 'e', 1: 'hello', b: undefined };62 const keys: (string | symbol)[] = ['b', '1', 'a'];63 const magicNoValue = Symbol('no-value');64 // Act65 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);66 const values = unmapper(obj);67 // Assert68 expect(values).toEqual([undefined, 'hello', 'e']);69 });70 it('should properly unmap instances of Object with known symbols as keys', () => {71 // Arrange72 const s1 = Symbol('s1');73 const s2 = Symbol('s2');74 const obj = { a: 'e', [s2]: 'hello', [s1]: undefined };75 const keys: (string | symbol)[] = [s2, s1, 'a'];76 const magicNoValue = Symbol('no-value');77 // Act78 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);79 const values = unmapper(obj);80 // Assert81 expect(values).toEqual(['hello', undefined, 'e']);82 });83 it('should properly unmap instances of Object with missing keys', () => {84 // Arrange85 const s1 = Symbol('s1');86 const s2 = Symbol('s2');87 const s3 = Symbol('s2');88 const obj = { a: 'e', [s2]: 'hello', [s1]: undefined };89 const keys: (string | symbol)[] = [s2, 'b', s1, 'a', 'd', s3];90 const magicNoValue = Symbol('no-value');91 // Act92 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);93 const values = unmapper(obj);94 // Assert95 expect(values).toEqual(['hello', magicNoValue, undefined, 'e', magicNoValue, magicNoValue]);96 });97 it('should properly unmap instances of Object with "__proto__" as key when there', () => {98 // Arrange99 const obj = { ['__proto__']: 'e' };100 const keys: (string | symbol)[] = ['toString', '__proto__', 'a'];101 const magicNoValue = Symbol('no-value');102 // Act103 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);104 const values = unmapper(obj);105 // Assert106 expect(values).toEqual([magicNoValue, 'e', magicNoValue]);107 });108 it('should properly unmap instances of Object with "__proto__" as key when not there', () => {109 // Arrange110 const obj = {};111 const keys: (string | symbol)[] = ['toString', '__proto__', 'a'];112 const magicNoValue = Symbol('no-value');113 // Act114 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);115 const values = unmapper(obj);116 // Assert117 expect(values).toEqual([magicNoValue, magicNoValue, magicNoValue]);118 });119 it.each`120 value | condition121 ${Object.create(null)} | ${'it has no prototype'}122 ${new (class A {})()} | ${'it is not just a simple object but a more complex type'}123 ${[]} | ${'it is an Array'}124 ${new Number(0)} | ${'it is a boxed-Number'}125 ${0} | ${'it is a number'}126 ${null} | ${'it is null'}127 ${undefined} | ${'it is undefined'}128 ${{ [Symbol('unknown')]: 5 }} | ${'it contains an unknown symbol property'}129 ${{ [Symbol('unknown')]: 5, a: 6, [Symbol.for('a')]: 7 }} | ${'it contains an unknown symbol property (even if others are there)'}130 ${{ unknown: 5 }} | ${'it contains an unknown named property'}131 ${{ unknown: 5, a: 6, [Symbol.for('a')]: 7 }} | ${'it contains an unknown named property (even if others are there)'}132 ${Object.defineProperty({}, 'a', { value: 5, configurable: false })} | ${'it contains a non-configurable property'}133 ${Object.defineProperty({}, 'a', { value: 5, enumerable: false })} | ${'it contains a non-enumerable property'}134 ${Object.defineProperty({}, 'a', { value: 5, writable: false })} | ${'it contains a non-writable property'}135 ${Object.defineProperty({}, 'a', { get: () => 5 })} | ${'it contains a get property'}136 ${Object.defineProperty({}, 'a', { set: () => {} })} | ${'it contains a set property'}137 `('should reject unmap on instance when $condition', ({ value }) => {138 // Arrange139 const keys: (string | symbol)[] = ['a', Symbol.for('a')];140 const magicNoValue = Symbol('no-value');141 const unmapper = buildValuesAndSeparateKeysToObjectUnmapper<any, typeof magicNoValue>(keys, magicNoValue);142 // Act / Assert143 expect(() => unmapper(value)).toThrowError();144 });...
PartialRecordArbitraryBuilder.spec.ts
Source:PartialRecordArbitraryBuilder.spec.ts
1import { fakeArbitrary } from '../../__test-helpers__/ArbitraryHelpers';2import { buildPartialRecordArbitrary } from '../../../../../src/arbitrary/_internals/builders/PartialRecordArbitraryBuilder';3import * as OptionMock from '../../../../../src/arbitrary/option';4import * as TupleMock from '../../../../../src/arbitrary/tuple';5import * as ValuesAndSeparateKeysToObjectMock from '../../../../../src/arbitrary/_internals/mappers/ValuesAndSeparateKeysToObject';6function beforeEachHook() {7 jest.resetModules();8 jest.restoreAllMocks();9}10beforeEach(beforeEachHook);11describe('buildPartialRecordArbitrary', () => {12 it('should never wrap arbitraries linked to required keys and forward all keys to mappers', () => {13 // Arrange14 const { instance: mappedInstance } = fakeArbitrary<any>();15 const { instance: tupleInstance, map } = fakeArbitrary<any[]>();16 const option = jest.spyOn(OptionMock, 'option');17 const tuple = jest.spyOn(TupleMock, 'tuple');18 tuple.mockReturnValue(tupleInstance);19 map.mockReturnValue(mappedInstance);20 const mapper = jest.fn();21 const buildValuesAndSeparateKeysToObjectMapper = jest.spyOn(22 ValuesAndSeparateKeysToObjectMock,23 'buildValuesAndSeparateKeysToObjectMapper'24 );25 buildValuesAndSeparateKeysToObjectMapper.mockReturnValue(mapper);26 const unmapper = jest.fn();27 const buildValuesAndSeparateKeysToObjectUnmapper = jest.spyOn(28 ValuesAndSeparateKeysToObjectMock,29 'buildValuesAndSeparateKeysToObjectUnmapper'30 );31 buildValuesAndSeparateKeysToObjectUnmapper.mockReturnValue(unmapper);32 const arbKey1 = fakeArbitrary();33 const arbKey2 = fakeArbitrary();34 const recordModel = {35 a: arbKey1,36 b: arbKey2,37 };38 const requiredKeys: (keyof typeof recordModel)[] = ['a', 'b'];39 const allKeys: (keyof typeof recordModel)[] = ['a', 'b'];40 // Act41 const arb = buildPartialRecordArbitrary(recordModel, requiredKeys);42 // Assert43 expect(arb).toBe(mappedInstance);44 expect(option).not.toHaveBeenCalled();45 expect(tuple).toHaveBeenCalledTimes(1);46 expect(tuple).toHaveBeenCalledWith(recordModel.a, recordModel.b);47 expect(buildValuesAndSeparateKeysToObjectMapper).toHaveBeenCalledTimes(1);48 expect(buildValuesAndSeparateKeysToObjectMapper).toHaveBeenCalledWith(allKeys, expect.any(Symbol));49 expect(buildValuesAndSeparateKeysToObjectUnmapper).toHaveBeenCalledTimes(1);50 expect(buildValuesAndSeparateKeysToObjectUnmapper).toHaveBeenCalledWith(allKeys, expect.any(Symbol));51 expect(map).toHaveBeenCalledTimes(1);52 expect(map).toHaveBeenCalledWith(mapper, unmapper);53 });54 it('should wrap arbitraries not linked to required keys into option and forward all keys to mappers', () => {55 // Arrange56 const { instance: mappedInstance } = fakeArbitrary<any>();57 const { instance: tupleInstance, map } = fakeArbitrary<any[]>();58 const { instance: optionInstance1 } = fakeArbitrary();59 const { instance: optionInstance2 } = fakeArbitrary();60 const option = jest.spyOn(OptionMock, 'option');61 const tuple = jest.spyOn(TupleMock, 'tuple');62 const optionInstance1Old = optionInstance1;63 const optionInstance2Old = optionInstance2;64 option.mockReturnValueOnce(optionInstance1Old).mockReturnValueOnce(optionInstance2Old);65 tuple.mockReturnValue(tupleInstance);66 map.mockReturnValue(mappedInstance);67 const mapper = jest.fn();68 const buildValuesAndSeparateKeysToObjectMapper = jest.spyOn(69 ValuesAndSeparateKeysToObjectMock,70 'buildValuesAndSeparateKeysToObjectMapper'71 );72 buildValuesAndSeparateKeysToObjectMapper.mockReturnValue(mapper);73 const unmapper = jest.fn();74 const buildValuesAndSeparateKeysToObjectUnmapper = jest.spyOn(75 ValuesAndSeparateKeysToObjectMock,76 'buildValuesAndSeparateKeysToObjectUnmapper'77 );78 buildValuesAndSeparateKeysToObjectUnmapper.mockReturnValue(unmapper);79 const arbKey1 = fakeArbitrary();80 const arbKey2 = fakeArbitrary();81 const arbKey3 = fakeArbitrary();82 const recordModel = {83 a: arbKey1,84 b: arbKey2,85 c: arbKey3,86 };87 const requiredKeys: (keyof typeof recordModel)[] = ['b'];88 const allKeys: (keyof typeof recordModel)[] = ['a', 'b', 'c'];89 // Act90 const arb = buildPartialRecordArbitrary(recordModel, requiredKeys);91 // Assert92 expect(arb).toBe(mappedInstance);93 expect(option).toHaveBeenCalledTimes(2);94 expect(option).toHaveBeenCalledWith(recordModel.a, { nil: expect.any(Symbol) });95 expect(option).toHaveBeenCalledWith(recordModel.c, { nil: expect.any(Symbol) });96 expect(tuple).toHaveBeenCalledTimes(1);97 expect(tuple).toHaveBeenCalledWith(optionInstance1Old, recordModel.b, optionInstance2Old);98 expect(buildValuesAndSeparateKeysToObjectMapper).toHaveBeenCalledTimes(1);99 expect(buildValuesAndSeparateKeysToObjectMapper).toHaveBeenCalledWith(allKeys, expect.any(Symbol));100 expect(buildValuesAndSeparateKeysToObjectUnmapper).toHaveBeenCalledTimes(1);101 expect(buildValuesAndSeparateKeysToObjectUnmapper).toHaveBeenCalledWith(allKeys, expect.any(Symbol));102 expect(map).toHaveBeenCalledTimes(1);103 expect(map).toHaveBeenCalledWith(mapper, unmapper);104 });105 it('should not wrap any arbitrary when required keys is not specified (all required) and forward all keys to mappers', () => {106 // Arrange107 const { instance: mappedInstance } = fakeArbitrary<any>();108 const { instance: tupleInstance, map } = fakeArbitrary<any[]>();109 const option = jest.spyOn(OptionMock, 'option');110 const tuple = jest.spyOn(TupleMock, 'tuple');111 tuple.mockReturnValue(tupleInstance);112 map.mockReturnValue(mappedInstance);113 const mapper = jest.fn();114 const buildValuesAndSeparateKeysToObjectMapper = jest.spyOn(115 ValuesAndSeparateKeysToObjectMock,116 'buildValuesAndSeparateKeysToObjectMapper'117 );118 buildValuesAndSeparateKeysToObjectMapper.mockReturnValue(mapper);119 const unmapper = jest.fn();120 const buildValuesAndSeparateKeysToObjectUnmapper = jest.spyOn(121 ValuesAndSeparateKeysToObjectMock,122 'buildValuesAndSeparateKeysToObjectUnmapper'123 );124 buildValuesAndSeparateKeysToObjectUnmapper.mockReturnValue(unmapper);125 const arbKey1 = fakeArbitrary();126 const arbKey2 = fakeArbitrary();127 const recordModel = {128 a: arbKey1,129 b: arbKey2,130 };131 const requiredKeys = undefined;132 const allKeys: (keyof typeof recordModel)[] = ['a', 'b'];133 // Act134 const arb = buildPartialRecordArbitrary(recordModel, requiredKeys);135 // Assert136 expect(arb).toBe(mappedInstance);137 expect(option).not.toHaveBeenCalled();138 expect(tuple).toHaveBeenCalledTimes(1);139 expect(tuple).toHaveBeenCalledWith(recordModel.a, recordModel.b);140 expect(buildValuesAndSeparateKeysToObjectMapper).toHaveBeenCalledTimes(1);141 expect(buildValuesAndSeparateKeysToObjectMapper).toHaveBeenCalledWith(allKeys, expect.any(Symbol));142 expect(buildValuesAndSeparateKeysToObjectUnmapper).toHaveBeenCalledTimes(1);143 expect(buildValuesAndSeparateKeysToObjectUnmapper).toHaveBeenCalledWith(allKeys, expect.any(Symbol));144 expect(map).toHaveBeenCalledTimes(1);145 expect(map).toHaveBeenCalledWith(mapper, unmapper);146 });...
PartialRecordArbitraryBuilder.ts
Source:PartialRecordArbitraryBuilder.ts
1import { Arbitrary } from '../../../check/arbitrary/definition/Arbitrary';2import { safeIndexOf, safePush } from '../../../utils/globals';3import { option } from '../../option';4import { tuple } from '../../tuple';5import { EnumerableKeyOf, extractEnumerableKeys } from '../helpers/EnumerableKeysExtractor';6import {7 buildValuesAndSeparateKeysToObjectMapper,8 buildValuesAndSeparateKeysToObjectUnmapper,9} from '../mappers/ValuesAndSeparateKeysToObject';10const noKeyValue: unique symbol = Symbol('no-key');11type NoKeyType = typeof noKeyValue;12/** @internal */13export function buildPartialRecordArbitrary<T, TKeys extends EnumerableKeyOf<T>>(14 recordModel: { [K in keyof T]: Arbitrary<T[K]> },15 requiredKeys: TKeys[] | undefined16): Arbitrary<Partial<T> & Pick<T, TKeys>> {17 const keys = extractEnumerableKeys(recordModel);18 const arbs: Arbitrary<T[keyof T] | NoKeyType>[] = [];19 for (let index = 0; index !== keys.length; ++index) {20 const k: EnumerableKeyOf<T> = keys[index];21 const requiredArbitrary = recordModel[k];22 if (requiredKeys === undefined || safeIndexOf(requiredKeys, k as TKeys) !== -1) {23 safePush(arbs, requiredArbitrary);24 } else {25 safePush(arbs, option(requiredArbitrary, { nil: noKeyValue as NoKeyType }));26 }27 }28 return tuple(...arbs).map(29 buildValuesAndSeparateKeysToObjectMapper<T, NoKeyType>(keys, noKeyValue),30 buildValuesAndSeparateKeysToObjectUnmapper<T, NoKeyType>(keys, noKeyValue)31 );...
Using AI Code Generation
1const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check');2const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b', 'c']);3const result = mapper([1, 2, 3]);4console.log(result);5const { buildValuesAndKeysToObjectMapper } = require('fast-check');6const mapper = buildValuesAndKeysToObjectMapper(['a', 'b', 'c']);7const result = mapper([1, 2, 3]);8console.log(result);9const { buildValuesToObjectMapper } = require('fast-check');10const mapper = buildValuesToObjectMapper();11const result = mapper([1, 2, 3]);12console.log(result);13const { buildStringToObjectMapper } = require('fast-check');14const mapper = buildStringToObjectMapper();15const result = mapper('abc');16console.log(result);17const { buildStringToValuesMapper } = require('fast-check');
Using AI Code Generation
1const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check');2const { buildObject } = require('fast-check/lib/arbitrary/ObjectArbitrary');3const { buildString } = require('fast-check/lib/arbitrary/StringArbitrary');4const buildCustomObject = buildObject({withSet: true});5const buildCustomString = buildString({minLength: 5, maxLength: 10});6const buildCustomObjectWithCustomString = buildValuesAndSeparateKeysToObjectMapper(buildCustomString, buildCustomObject);7const customObject = buildCustomObjectWithCustomString();8console.log(customObject);
Using AI Code Generation
1const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check-monorepo');2const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b', 'c']);3const obj = mapper([1, 2, 3]);4const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check-monorepo');5const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b', 'c']);6const obj = mapper([1, 2, 3]);7const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check-monorepo');8const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b', 'c']);9const obj = mapper([1, 2, 3]);10const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check-monorepo');11const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b', 'c']);12const obj = mapper([1, 2, 3]);13const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check-monorepo');14const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b', 'c']);15const obj = mapper([1, 2, 3]);
Using AI Code Generation
1const { buildValuesAndSeparateKeysToObjectMapper } = require( 'fast-check' );2const { Mapper } = require( 'fast-check/lib/types/mapper/Mapper' );3const { Arbitrary } = require( 'fast-check/lib/check/arbitrary/definition/Arbitrary' );4const { ArbitraryWithShrink } = require( 'fast-check/lib/check/arbitrary/definition/ArbitraryWithShrink' );5const mapper = buildValuesAndSeparateKeysToObjectMapper( { a: 'b' } );6const arb = new ArbitraryWithShrink( new Arbitrary( () => 1 ) );7const mapped = mapper( arb );8console.log( mapped );
Using AI Code Generation
1const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check');2const mapper = buildValuesAndSeparateKeysToObjectMapper(['a', 'b']);3const values = ['a', 'b'];4const values = ['a'];5const values = ['a', 'b', 'c'];6const values = ['a', 'b', 'c', 'd'];7const values = ['a', 'b', 'c', 'd', 'e'];8const values = ['a', 'b', 'c', 'd', 'e', 'f'];9const values = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];10const values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];11const values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];12const values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
Using AI Code Generation
1const { buildValuesAndSeparateKeysToObjectMapper } = require('fast-check-monorepo');2const mapper = buildValuesAndSeparateKeysToObjectMapper({3 key1: fc.integer(),4 key2: fc.string(),5 key3: fc.boolean(),6});7fc.assert(8 fc.property(mapper, (obj) => {9 })10);11const { buildValuesAndCombinedKeysToObjectMapper } = require('fast-check-monorepo');12const mapper = buildValuesAndCombinedKeysToObjectMapper({13 key1: fc.integer(),14 key2: fc.string(),15 key3: fc.boolean(),16});17fc.assert(18 fc.property(mapper, (obj) => {19 })20);21const { buildValuesAndCombinedKeysToObjectMapper } = require('fast-check-monorepo');22const mapper = buildValuesAndCombinedKeysToObjectMapper({23 key1: fc.integer(),24 key2: fc.string(),25 key3: fc.boolean(),26});27fc.assert(28 fc.property(mapper, (obj) => {29 })30);31const { buildValuesAndCombinedKeysToObjectMapper } = require('fast-check-monorepo');32const mapper = buildValuesAndCombinedKeysToObjectMapper({33 key1: fc.integer(),34 key2: fc.string(),35 key3: fc.boolean(),36});37fc.assert(38 fc.property(mapper, (obj
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!!