Best JavaScript code snippet using jest-extended
js-deep-merge.js
Source:js-deep-merge.js
1/* External Requirements */2var sinon = require("sinon");3var chai = require('chai');4var should = chai.should();5/* Chai Plugins */6chai.use(require('chai-like'));7chai.use(require('sinon-chai'));8/* To test */9require('../src/js-deep-merge.js');10/* Mocks */11var simpleObject1 = { mock1: 'mock1' };12var simpleObject2 = { mock2: 'mock2' };13var simpleObject3 = { mock1: 'mock3', mock2: 'mock2' };14var complexObject1 = { mock1: { mock1a: 'mock1a' } };15var complexObject2 = { mock2: { mock2a: 'mock2a' } };16var complexObject3 = { mock1: { mock2a: 'mock2a' } };17var complexObject4 = { mock1: { mock1a: 'mock1b', mock2a: 'mock2a' } };18var complexObject5 = { mock1: { mock1a: { mock1a1: 'mock1a1' }, mock2a: { mock2a1: 'mock2a1' } } };19var complexObject6 = { mock1: { mock3a: { mock3a1: 'mock3a1' }, mock4a: { mock4a1: 'mock4a1' } } };20var complexObject7 = { mock1: { mock1a: { mock1b: { mock1c: { mock1d: true } } } } };21var complexObject8 = { mock1: { mock1a: { mock1b: { mock1c: { mock1d: false } } } } };22var simpleArray1 = [ 'mock1' ];23var simpleArray2 = [ 'mock2' ];24var complexArray1 = [ [ 'mock1' ], 'mock2' ];25var complexArray2 = [ [ 'mock3' ] ];26var complexArray3 = [ [ 'mock1' ], [ 'mock2', 'mock3' ] ];27var complexArray4 = [ [ 'mock4' ], [ 'mock5' ] ];28var arrayWithObject1 = [ { mock1 : 'mock1' } ];29var arrayWithObject2 = [ { mock2 : 'mock2' } ];30var arrayWithObject3 = [ { mock1 : 'mock1' }, { mock2 : 'mock2' }, { mock3 : 'mock3' } ];31var arrayWithObject4 = [ { mock4 : 'mock4' }, { mock5 : 'mock5' } ];32var arrayWithObject5 = [ { mock1 : 'mock1' } ];33var arrayWithObject6 = [ { mock1 : 'mock2' } ];34var arrayWithObject7 = [ { mock1 : 'mock1' }, 'mock2', [ 'mock3', 'mock4' ], undefined, true ];35var arrayWithObject8 = [ { mock5 : 'mock5' }, { mock6 : 'mock6' }, [ 'mock7' ], 'defined', false ];36var deepArray1 = [{ mock1: [{ mock2: 'mock2', mock3: 'mock3', mock4: 'mock4' }, { mock5: 'mock5', mock6: 'mock6', mock7: 'mock7' }], mock2: [{ mock8: 'mock8', mock9: 'mock9' }, { mock5: 'mock10' }], mock11: 'mock11', mock3: [[ 'mock12' ]]}, {mock4: [ { mock13: 'mock13' } ]}];37var deepArray2 = [{ mock1: [{ mock2: 'mock2a', mock3: 'mock3a' }, { mock5: 'mock5a', mock14: 'mock14' }], mock2: [{ mock8: 'mock8', mock9: 'mock9' }, { mock5: 'mock10' }], mock11: [[ 'mock15' ]], mock3: [[ 'mock12a' ]] }, 'mock16', { mock4: [ { mock13: 'mock13a' } ] }];38var deepObject1 = {mock1: { mock2: { mock3: [ 'mock4', { mock5: 'mock5', mock6: 'mock6' }, true ] } }, mock2: [ [ 'mock7' ], [ 'mock8' ], [ 'mock9' ], [ 'mock10' ] ]};39var deepObject2 = {mock1: { mock2: { mock3: [ 'mock4a', { mock5: 'mock5', mock11: 'mock11' }, false ] } }, mock2: [ [ 'mock7' ], 'mock8a', [ 'mock9' ], [ 'mock10a' ] ]};40var arrayWithUndefined1 = [ 'mock1', undefined, 'mock2' ];41var arrayWithUndefined2 = [ 'mock1a', 'mock3', 'mock2' ];42var arrayWithUndefined3 = [ undefined ];43var arrayWithUndefined4 = [ [ 'mock1' ] ];44var arrayWithUndefined5 = [ undefined ];45var arrayWithUndefined6 = [ { mock1: 'mock1' } ];46var arrayWithUndefined7 = [ 'mock1' ];47var arrayWithUndefined8 = [ undefined ];48var objectWithUndefined1 = { mock1: 'mock1', mock2: undefined, mock3: 'mock3' };49var objectWithUndefined2 = { mock1: 'mock1', mock2: 'mock2', mock3: 'mock3' };50var objectWithUndefined3 = { mock1: undefined };51var objectWithUndefined4 = { mock1: { mock2: 'mock2' } };52var objectWithUndefined5 = { mock1: undefined };53var objectWithUndefined6 = { mock1: [ 'mock1' ] };54var objectWithUndefined7 = { mock1: 'mock1' };55var objectWithUndefined8 = { mock1: undefined };56var finalArray1 = [{"entry":{"key":"mock1","value":"mock1value"},"children":[{"entry":{"key":"mock2","value":"mock2value","value2":"mock2value2"}},{"entry":{"key":"mock3","value":"mock3value"}},{"entry":{"key":"mock4","value1":"mock4value","options":[{"key":0,"value":"A"},{"key":1,"value":"B"},{"key":2,"value":"C"},{"key":3,"value":"D"}]}},{"entry":{"key":"mock5","value":"mock5value"},"children":[{"entry":{"key":"mock6","value":"mock6value"},"children":[{"entry":{"key":"mock7","value":"mock7value"}},{"entry":{"key":"mock8","label":"mock8value"}}]}]}]}];57var finalArray2 = [{"entry":{"key":"mock1","value":"mock1value"},"children":[{"entry":{"key":"mock2","value":"mock2value","value2":"mock2value2"}},{"entry":{"key":"mock4","value1":"mock4value","options":[{"key":0,"value":"A"},{"key":1,"value":"B"},{"key":2,"value":"C"},{"key":3,"value":"D"}]}},{"entry":{"key":"mock3","value":"mock3value"}},{"entry":{"key":"mock5","value":"mock5value"},"children":[{"entry":{"key":"mock6","value":"mock6value"},"children":[{"entry":{"key":"mock7","value":"mock7value"}},{"entry":{"key":"mock8","label":"mock8value"}}]}]}]}];58/* Expectations */59var simpleMergedObject1 = { mock1: 'mock1', mock2: 'mock2' };60var simpleMergedObject2 = { mock1: 'mock3', mock2: 'mock2' };61var complexMergedObject1 = { mock1: { mock1a: 'mock1a' }, mock2: { mock2a: 'mock2a' } };62var complexMergedObject2 = { mock1: { mock1a: 'mock1a', mock2a: 'mock2a' } };63var complexMergedObject3 = { mock1: { mock1a: 'mock1b', mock2a: 'mock2a' } };64var complexMergedObject4 = { mock1: { mock1a: { mock1a1: 'mock1a1' }, mock2a: { mock2a1: 'mock2a1' }, mock3a: { mock3a1: 'mock3a1' }, mock4a: { mock4a1: 'mock4a1' } } };65var simpleConvertedArray = { 0 : 'mock1' };66var simpleMergedArray1 = [ 'mock2' ];67var complexMergedArray1 = [ [ 'mock3' ], 'mock2' ];68var complexMergedArray2 = [ [ 'mock4' ], [ 'mock5', 'mock3' ] ];69var arrayWithObjectMerged1 = [ { mock1 : 'mock1', mock2 : 'mock2' } ];70var arrayWithObjectMerged2 = [ { mock1 : 'mock1', mock4 : 'mock4' }, { mock2 : 'mock2', mock5 : 'mock5' }, { mock3 : 'mock3' } ];71var arrayWithObjectMerged3 = [ { mock1 : 'mock2' } ];72var arrayWithObjectMerged4 = [ { mock1 : 'mock1', mock5 : 'mock5' }, { mock6 : 'mock6' }, [ 'mock7', 'mock4' ], 'defined', false ];73var deepMergedArray = [{ mock1: [{ mock2: 'mock2a', mock3: 'mock3a', mock4: 'mock4' }, { mock5: 'mock5a', mock14: 'mock14', mock6: 'mock6', mock7: 'mock7' }], mock2: [{ mock8: 'mock8', mock9: 'mock9' }, { mock5: 'mock10' }], mock11: [[ 'mock15' ]], mock3: [[ 'mock12a' ]]}, 'mock16', { mock4: [ { mock13: 'mock13a' } ] }];74var deepMergedObject = {mock1: { mock2: { mock3: [ 'mock4a', { mock5: 'mock5', mock11: 'mock11', mock6: 'mock6' }, false ] } }, mock2: [ [ 'mock7' ], 'mock8a', [ 'mock9' ], [ 'mock10a' ] ]};75var arrayWithUndefinedMerged1 = [ [ 'mock1' ] ];76var arrayWithUndefinedMerged2 = [ { mock1: 'mock1' } ];77var arrayWithUndefinedMerged3 = [ undefined ];78var objectWithUndefinedMerged1 = { mock1: 'mock1', mock2: 'mock2', mock3: 'mock3' };79var objectWithUndefinedMerged2 = { mock1: { mock2: 'mock2' } };80var objectWithUndefinedMerged3 = { mock1: [ 'mock1' ] };81var objectWithUndefinedMerged4 = { mock1: undefined };82 /* Test */83describe('Convert Array to Object', function(){84 it('should convert a simple array into an object', function() {85 var arr1 = Object.assign([], simpleArray1);86 var obj1 = Array.toObject(arr1);87 obj1.should.be.like(simpleConvertedArray);88 });89});90describe('Convert Object to Array', function(){91 it('should convert a simple object into an array', function() {92 var obj1 = Object.assign({}, simpleObject1);93 var arr1 = Object.toArray(obj1);94 arr1.should.be.like(simpleArray1);95 });96});97describe('Object merge', function(){98 it('should merge two simple objects', function() {99 var obj1 = Object.assign({}, simpleObject1);100 var obj2 = Object.assign({}, simpleObject2);101 obj1 = Object.deepMerge(obj1, obj2);102 obj1.should.be.like(simpleMergedObject1);103 });104 it('should merge two simple objects and replace one value', function() {105 var obj1 = Object.assign({}, simpleObject1);106 var obj2 = Object.assign({}, simpleObject3);107 obj1 = Object.deepMerge(obj1, obj2);108 obj1.should.be.like(simpleMergedObject2);109 });110 it('should merge two complex objects', function() {111 var obj1 = Object.assign({}, complexObject1);112 var obj2 = Object.assign({}, complexObject2);113 obj1 = Object.deepMerge(obj1, obj2);114 obj1.should.be.like(complexMergedObject1);115 });116 it('should merge two complex objects and deep merge', function() {117 var obj1 = Object.assign({}, complexObject1);118 var obj2 = Object.assign({}, complexObject3);119 obj1 = Object.deepMerge(obj1, obj2);120 obj1.should.be.like(complexMergedObject2);121 });122 it('should merge two complex objects, deep merge and replace deep', function() {123 var obj1 = Object.assign({}, complexObject1);124 var obj2 = Object.assign({}, complexObject4);125 obj1 = Object.deepMerge(obj1, obj2);126 obj1.should.be.like(complexMergedObject3);127 });128 it('should merge two more complex objects', function() {129 var obj1 = Object.assign({}, complexObject5);130 var obj2 = Object.assign({}, complexObject6);131 obj1 = Object.deepMerge(obj1, obj2);132 obj1.should.be.like(complexMergedObject4);133 });134 it('should merge two deep objects', function() {135 var obj1 = Object.assign({}, complexObject7);136 var obj2 = Object.assign({}, complexObject8);137 obj1 = Object.deepMerge(obj1, obj2);138 obj1.should.be.like(complexObject8);139 });140});141describe('Array merge', function() {142 it('should merge two simple arrays', function() {143 var arr1 = Object.assign([], simpleArray1);144 var arr2 = Object.assign([], simpleArray2);145 arr1 = Array.deepMerge(arr1, arr2);146 arr1.should.be.like(simpleMergedArray1);147 });148 it('should merge two complex arrays', function() {149 var arr1 = Object.assign([], complexArray1);150 var arr2 = Object.assign([], complexArray2);151 arr1 = Array.deepMerge(arr1, arr2);152 arr1.should.be.like(complexMergedArray1);153 });154 it('should merge two more complex arrays', function() {155 var arr1 = Object.assign([], complexArray3);156 var arr2 = Object.assign([], complexArray4);157 arr1 = Array.deepMerge(arr1, arr2);158 arr1.should.be.like(complexMergedArray2);159 });160});161describe('Array and Object merge', function() {162 it('should merge two simple arrays with objects', function() {163 var arr1 = Object.assign([], arrayWithObject1);164 var arr2 = Object.assign([], arrayWithObject2);165 arr1 = Array.deepMerge(arr1, arr2);166 arr1.should.be.like(arrayWithObjectMerged1);167 });168 it('should merge two complex arrays with objects', function() {169 var arr1 = Object.assign([], arrayWithObject3);170 var arr2 = Object.assign([], arrayWithObject4);171 arr1 = Array.deepMerge(arr1, arr2);172 arr1.should.be.like(arrayWithObjectMerged2);173 });174 it('should merge two complex arrays with objects and replace one element in object', function() {175 var arr1 = Object.assign([], arrayWithObject5);176 var arr2 = Object.assign([], arrayWithObject6);177 arr1 = Array.deepMerge(arr1, arr2);178 arr1.should.be.like(arrayWithObjectMerged3);179 });180 it('should merge two complex arrays with objects and other mixed attributes', function() {181 var arr1 = Object.assign([], arrayWithObject7);182 var arr2 = Object.assign([], arrayWithObject8);183 arr1 = Array.deepMerge(arr1, arr2);184 arr1.should.be.like(arrayWithObjectMerged4);185 });186 it('should merge two deep arrays with objects, arrays and other mixed attributes', function() {187 var arr1 = Object.assign([], deepArray1);188 var arr2 = Object.assign([], deepArray2);189 arr1 = Array.deepMerge(arr1, arr2);190 arr1.should.be.like(deepMergedArray);191 });192 it('should merge two deep objects with objects, arrays and other mixed attributes', function() {193 var obj1 = Object.assign({}, deepObject1);194 var obj2 = Object.assign({}, deepObject2);195 obj1 = Object.deepMerge(obj1, obj2);196 obj1.should.be.like(deepMergedObject);197 });198});199describe('Undefined merge', function() {200 it('should merge two simple arrays with undefined elements', function() {201 var arr1 = Object.assign([], arrayWithUndefined1);202 var arr2 = Object.assign([], arrayWithUndefined2);203 arr1 = Array.deepMerge(arr1, arr2);204 arr1.should.be.like(arrayWithUndefined2);205 });206 it('should merge two simple arrays with undefined elements', function() {207 var arr1 = Object.assign([], arrayWithUndefined3);208 var arr2 = Object.assign([], arrayWithUndefined4);209 arr1 = Array.deepMerge(arr1, arr2);210 arr1.should.be.like(arrayWithUndefinedMerged1);211 });212 it('should merge two simple arrays with undefined elements', function() {213 var arr1 = Object.assign([], arrayWithUndefined5);214 var arr2 = Object.assign([], arrayWithUndefined6);215 arr1 = Array.deepMerge(arr1, arr2);216 arr1.should.be.like(arrayWithUndefinedMerged2);217 });218 it('should merge two simple arrays with undefined elements', function() {219 var arr1 = Object.assign([], arrayWithUndefined7);220 var arr2 = Object.assign([], arrayWithUndefined8);221 arr1 = Array.deepMerge(arr1, arr2);222 arr1.should.be.like(arrayWithUndefinedMerged3);223 });224 it('should merge two simple objects with undefined elements', function() {225 var obj1 = Object.assign({}, objectWithUndefined1);226 var obj2 = Object.assign({}, objectWithUndefined2);227 obj1 = Object.deepMerge(obj1, obj2);228 obj1.should.be.like(objectWithUndefinedMerged1);229 });230 it('should merge two simple objects with undefined elements', function() {231 var obj1 = Object.assign({}, objectWithUndefined3);232 var obj2 = Object.assign({}, objectWithUndefined4);233 obj1 = Object.deepMerge(obj1, obj2);234 obj1.should.be.like(objectWithUndefinedMerged2);235 });236 it('should merge two simple objects with undefined elements', function() {237 var obj1 = Object.assign({}, objectWithUndefined5);238 var obj2 = Object.assign({}, objectWithUndefined6);239 obj1 = Object.deepMerge(obj1, obj2);240 obj1.should.be.like(objectWithUndefinedMerged3);241 });242 it('should merge two simple objects with undefined elements', function() {243 var obj1 = Object.assign({}, objectWithUndefined7);244 var obj2 = Object.assign({}, objectWithUndefined8);245 obj1 = Object.deepMerge(obj1, obj2);246 obj1.should.be.like(objectWithUndefinedMerged4);247 });248});249describe('Final tests', function() {250 it('should merge two arrays with deep object and array hierarchy', function() {251 var arr1 = Object.assign([], finalArray1);252 var arr2 = Object.assign([], finalArray2);253 arr1 = Array.deepMerge(arr1, arr2);254 arr1.should.be.like(finalArray2);255 });...
index.test.ts
Source:index.test.ts
1import NIndexMap from "../src/index";2import { createMock } from "ts-auto-mock";3interface TestInterface {4 stringId: string;5 numberId: number;6 objectId: object;7 data1: string;8 data2: number;9 data3: object;10}11const stringIndex = "stringId";12const numberIndex = "numberId";13const objectIndex = "objectId";14describe("NIndexMap", () => {15 let mock1: TestInterface;16 let mock2: TestInterface;17 let mock3: TestInterface;18 beforeEach(() => {19 mock1 = createMock<TestInterface>();20 mock2 = createMock<TestInterface>();21 mock3 = createMock<TestInterface>();22 });23 it("call constructor with index and data, then fetch result via string index", () => {24 const sut = new NIndexMap([stringIndex], [mock1, mock2, mock3]);25 const resMock1 = sut.get(stringIndex, mock1.stringId);26 const resMock2 = sut.get(stringIndex, mock2.stringId);27 const resMock3 = sut.get(stringIndex, mock3.stringId);28 expect(resMock1).toEqual(mock1);29 expect(resMock2).toEqual(mock2);30 expect(resMock3).toEqual(mock3);31 });32 it("call constructor with index and data, then fetch result via number index", () => {33 const sut = new NIndexMap([numberIndex], [mock1, mock2, mock3]);34 const resMock1 = sut.get(numberIndex, mock1.numberId);35 const resMock2 = sut.get(numberIndex, mock2.numberId);36 const resMock3 = sut.get(numberIndex, mock3.numberId);37 expect(resMock1).toEqual(mock1);38 expect(resMock2).toEqual(mock2);39 expect(resMock3).toEqual(mock3);40 });41 it("call constructor with index and data, then fetch result via object index", () => {42 const sut = new NIndexMap([objectIndex], [mock1, mock2, mock3]);43 const resMock1 = sut.get(objectIndex, mock1.objectId);44 const resMock2 = sut.get(objectIndex, mock2.objectId);45 const resMock3 = sut.get(objectIndex, mock3.objectId);46 expect(resMock1).toEqual(mock1);47 expect(resMock2).toEqual(mock2);48 expect(resMock3).toEqual(mock3);49 });50 it("call constructor (explicit typing) with index and data, then fetch result via different indexes", () => {51 const sut = new NIndexMap<TestInterface, "stringId" | "numberId" | "objectId">(52 [stringIndex, numberIndex, objectIndex],53 [mock1, mock2, mock3]54 );55 const resMock1 = sut.get(stringIndex, mock1.stringId);56 const resMock2 = sut.get(numberIndex, mock2.numberId);57 const resMock3 = sut.get(objectIndex, mock3.objectId);58 expect(resMock1).toEqual(mock1);59 expect(resMock2).toEqual(mock2);60 expect(resMock3).toEqual(mock3);61 });62 it("call constructor (explicit typing) with index and data, then iterate over entries", () => {63 const sut = new NIndexMap<TestInterface, "stringId">([stringIndex], [mock1, mock2, mock3]);64 const entries = sut.entries();65 expect([...entries].length).toEqual(3);66 for (const [index, mock] of entries) {67 expect(mock).toMatchObject(createMock<TestInterface>());68 }69 });70 it("call constructor (explicit typing) with index and data, then iterate over values", () => {71 const sut = new NIndexMap<TestInterface, "stringId">([stringIndex], [mock1, mock2, mock3]);72 const values = sut.values();73 expect([...values].length).toEqual(3);74 for (const mock of values) {75 expect(mock).toMatchObject(createMock<TestInterface>());76 }77 });78 it("call constructor without arguments, load data, and fetch expected result", () => {79 const sut = new NIndexMap<TestInterface, "stringId">();80 sut.set(mock1);81 sut.set(mock2);82 sut.set(mock3);83 const res = sut.get(stringIndex, mock1.stringId);84 expect(res).toEqual(mock1);85 });86 it("call constructor with just index, load data, and fetch expected result", () => {87 const sut = new NIndexMap<TestInterface, "stringId">(["stringId"]);88 sut.set(mock1);89 sut.set(mock2);90 sut.set(mock3);91 const res = sut.get(stringIndex, mock1.stringId);92 expect(res).toEqual(mock1);93 });94 it("call constructor with just initial data, add index, and fetch expected result", () => {95 const sut = new NIndexMap<TestInterface, "stringId">(undefined, [mock1, mock2, mock3]);96 sut.addIndex("stringId");97 const res = sut.get(stringIndex, mock1.stringId);98 expect(res).toEqual(mock1);99 });100 it("add index", () => {101 const sut = new NIndexMap<TestInterface, "stringId">();102 sut.set(mock1);103 sut.set(mock2);104 sut.set(mock3);105 sut.addIndex(stringIndex);106 const res = sut.get(stringIndex, mock1.stringId);107 expect(res).toEqual(mock1);108 });109 it("remove index", () => {110 const sut = new NIndexMap<TestInterface, "stringId">();111 sut.set(mock1);112 sut.set(mock2);113 sut.set(mock3);114 sut.addIndex(stringIndex);115 sut.removeIndex(stringIndex);116 const res = sut.get(stringIndex, mock1.stringId);117 expect(res).toEqual(mock1);118 });119 it("remove index that doesn't exist", () => {120 const sut = new NIndexMap<TestInterface, "stringId">();121 sut.set(mock1);122 sut.set(mock2);123 sut.set(mock3);124 sut.removeIndex(stringIndex);125 const res = sut.get(stringIndex, mock1.stringId);126 expect(res).toEqual(mock1);127 });128 it("store duplicate data", () => {129 const sut = new NIndexMap<TestInterface, "stringId">();130 sut.set(mock1);131 sut.set(mock1);132 sut.addIndex(stringIndex);133 const res = sut.get(stringIndex, mock1.stringId);134 expect(res).toEqual(mock1);135 expect(sut.size()).toEqual(2);136 });137 it("clear data and indexes", () => {138 const sut = new NIndexMap<TestInterface, "stringId">();139 sut.set(mock1);140 sut.set(mock2);141 sut.addIndex(stringIndex);142 sut.clear();143 const res = sut.get(stringIndex, mock1.stringId);144 expect(res).toBeUndefined();145 expect(sut.size()).toEqual(0);146 expect(sut.indexes()).toEqual(0);147 });148 it("has data", () => {149 const sut = new NIndexMap([stringIndex, numberIndex, objectIndex], [mock1, mock2, mock3]);150 const hasMock1 = sut.has(stringIndex, mock1.stringId);151 const hasMock2 = sut.has(numberIndex, mock2.numberId);152 const hasMock3 = sut.has(objectIndex, mock3.objectId);153 const shouldNotExist = sut.has(stringIndex, "fail");154 expect(hasMock1).toBeTruthy();155 expect(hasMock2).toBeTruthy();156 expect(hasMock3).toBeTruthy();157 expect(shouldNotExist).toBeFalsy();158 });159 it("has data without indexes", () => {160 const sut = new NIndexMap<TestInterface, "stringId" | "numberId" | "objectId">();161 sut.set(mock1);162 sut.set(mock2);163 sut.set(mock3);164 const hasMock1 = sut.has(stringIndex, mock1.stringId);165 const hasMock2 = sut.has(numberIndex, mock2.numberId);166 const hasMock3 = sut.has(objectIndex, mock3.objectId);167 const shouldNotExist = sut.has(stringIndex, "fail");168 expect(hasMock1).toBeTruthy();169 expect(hasMock2).toBeTruthy();170 expect(hasMock3).toBeTruthy();171 expect(shouldNotExist).toBeFalsy();172 });173 it("set data", () => {174 const sut = new NIndexMap([stringIndex, numberIndex, objectIndex], [mock1, mock2, mock3]);175 const mock = createMock<TestInterface>();176 sut.set(mock);177 const hasMockStringIndex = sut.has(stringIndex, mock.stringId);178 const hasMockNumberIndex = sut.has(numberIndex, mock.numberId);179 const hasMockObjectIndex = sut.has(objectIndex, mock.objectId);180 expect(hasMockStringIndex).toBeTruthy();181 expect(hasMockNumberIndex).toBeTruthy();182 expect(hasMockObjectIndex).toBeTruthy();183 });184 it("delete data", () => {185 const sut = new NIndexMap([stringIndex, numberIndex, objectIndex], [mock1, mock2, mock3]);186 sut.delete(stringIndex, mock1.stringId);187 expect(sut.size()).toEqual(2);188 sut.delete(numberIndex, mock2.numberId);189 expect(sut.size()).toEqual(1);190 sut.delete(objectIndex, mock3.objectId);191 expect(sut.size()).toEqual(0);192 });193 it("delete data without indexes", () => {194 const sut = new NIndexMap<TestInterface, "stringId" | "numberId" | "objectId">();195 sut.set(mock1);196 sut.set(mock2);197 sut.set(mock3);198 sut.delete(stringIndex, mock1.stringId);199 expect(sut.size()).toEqual(2);200 sut.delete(numberIndex, mock2.numberId);201 expect(sut.size()).toEqual(1);202 sut.delete(objectIndex, mock3.objectId);203 expect(sut.size()).toEqual(0);204 });205 it("set and get default value", () => {206 const sut = new NIndexMap([stringIndex], [mock1, mock2, mock3]);207 expect(sut.getDefault()).toBeUndefined();208 const defaultMock = createMock<TestInterface>();209 sut.setDefault(defaultMock);210 expect(sut.getDefault()).toEqual(defaultMock);211 const unmatchingMock = createMock<TestInterface>();212 expect(sut.getOrDefault(stringIndex, unmatchingMock.stringId)).toEqual(defaultMock);213 sut.set(unmatchingMock);214 expect(sut.getOrDefault(stringIndex, unmatchingMock.stringId)).toEqual(unmatchingMock);215 sut.setDefault(mock1);216 expect(sut.getDefault()).toEqual(mock1);217 });218});219describe("NIndexMap Input Validation", () => {220 let mock1: TestInterface;221 let mock2: TestInterface;222 let mock3: TestInterface;223 beforeEach(() => {224 mock1 = createMock<TestInterface>();225 mock2 = createMock<TestInterface>();226 mock3 = createMock<TestInterface>();227 });228 it("call constructor with index that isn't of type string or number should fail", () => {229 // @ts-ignore: required to bypass type inference on parameters230 expect(() => new NIndexMap([{ a: "test" }])).toThrow();231 });232 it("call constructor with data that isn't of type object should fail", () => {233 // @ts-ignore: required to bypass type inference on parameters234 expect(() => new NIndexMap(undefined, [1, 2, 3])).toThrow();235 });236 it("call set with data that isn't of type object should fail", () => {237 // @ts-ignore: required to bypass type inference on parameters238 expect(() => new NIndexMap().set(1, 2, 3)).toThrow();239 });240 it("call set with data that does not contain indexed properties", () => {241 // @ts-ignore: required to bypass type inference on parameters242 expect(() => new NIndexMap([numberIndex]).set({ a: "test " })).toThrow();243 });244 it("call set with data whose indexed properties values are not unique", () => {245 // @ts-ignore: required to bypass type inference on parameters246 expect(() => new NIndexMap([numberIndex], [{ numberId: 1 }]).set({ numberId: 1 })).toThrow();247 });248 it("call constructor with initial data, add index that doesn't exist on data type, should fail", () => {249 // @ts-ignore: required to bypass type inference on parameter250 expect(() => new NIndexMap(undefined, [mock1, mock2, mock3]).addIndex("random")).toThrow();251 });252 it("set default with data that isn't of type object", () => {253 // @ts-ignore: required to bypass type inference on parameters254 expect(() => new NIndexMap().setDefault(1)).toThrow();255 });...
mockcontrol_test.js
Source:mockcontrol_test.js
1/**2 * @license3 * Copyright The Closure Library Authors.4 * SPDX-License-Identifier: Apache-2.05 */6goog.module('goog.testing.MockControlTest');7goog.setTestOnly();8const Mock = goog.require('goog.testing.Mock');9const MockControl = goog.require('goog.testing.MockControl');10const testSuite = goog.require('goog.testing.testSuite');11// Emulate the behavior of a mock.12class MockMock {13 constructor() {14 this.replayCalled = false;15 this.resetCalled = false;16 this.verifyCalled = false;17 this.tearDownCalled = false;18 }19}20MockMock.prototype.$replay = function() {21 this.replayCalled = true;22};23MockMock.prototype.$reset = function() {24 this.resetCalled = true;25};26MockMock.prototype.$verify = function() {27 this.verifyCalled = true;28};29testSuite({30 setUp() {31 const mock = new Mock(MockMock);32 },33 /** @suppress {checkTypes} suppression added to enable type checking */34 testAdd() {35 const mockMock = new MockMock();36 const control = new MockControl();37 assertEquals(mockMock, control.addMock(mockMock));38 },39 /** @suppress {checkTypes} suppression added to enable type checking */40 testReplayAll() {41 const mockMock1 = new MockMock();42 const mockMock2 = new MockMock();43 const mockMockExcluded = new MockMock();44 const control = new MockControl();45 control.addMock(mockMock1);46 control.addMock(mockMock2);47 control.$replayAll();48 assertTrue(mockMock1.replayCalled);49 assertTrue(mockMock2.replayCalled);50 assertFalse(mockMockExcluded.replayCalled);51 },52 /** @suppress {checkTypes} suppression added to enable type checking */53 testResetAll() {54 const mockMock1 = new MockMock();55 const mockMock2 = new MockMock();56 const mockMockExcluded = new MockMock();57 const control = new MockControl();58 control.addMock(mockMock1);59 control.addMock(mockMock2);60 control.$resetAll();61 assertTrue(mockMock1.resetCalled);62 assertTrue(mockMock2.resetCalled);63 assertFalse(mockMockExcluded.resetCalled);64 },65 /** @suppress {checkTypes} suppression added to enable type checking */66 testVerifyAll() {67 const mockMock1 = new MockMock();68 const mockMock2 = new MockMock();69 const mockMockExcluded = new MockMock();70 const control = new MockControl();71 control.addMock(mockMock1);72 control.addMock(mockMock2);73 control.$verifyAll();74 assertTrue(mockMock1.verifyCalled);75 assertTrue(mockMock2.verifyCalled);76 assertFalse(mockMockExcluded.verifyCalled);77 },78 /** @suppress {checkTypes} suppression added to enable type checking */79 testTearDownAll() {80 const mockMock1 = new MockMock();81 const mockMock2 = new MockMock();82 const mockMockExcluded = new MockMock();83 // $tearDown is optional.84 /** @suppress {checkTypes} suppression added to enable type checking */85 mockMock2.$tearDown = function() {86 this.tearDownCalled = true;87 };88 /** @suppress {checkTypes} suppression added to enable type checking */89 mockMockExcluded.$tearDown = function() {90 this.tearDownCalled = true;91 };92 const control = new MockControl();93 control.addMock(mockMock1);94 control.addMock(mockMock2);95 control.$tearDown();96 // mockMock2 has a tearDown method and is in the control.97 assertTrue(mockMock2.tearDownCalled);98 assertFalse(mockMock1.tearDownCalled);99 assertFalse(mockMockExcluded.tearDownCalled);100 },...
Using AI Code Generation
1const mock1 = require('jest-extended');2const mock2 = require('jest-extended');3const mock3 = require('jest-extended');4const mock1 = require('jest-extended');5const mock2 = require('jest-extended');6const mock3 = require('jest-extended');
Using AI Code Generation
1const mock1 = require('jest-extended');2const mock2 = require('jest-extended');3const mock3 = require('jest-extended');4const mock4 = require('jest-extended');5const mock5 = require('jest-extended');6const mock6 = require('jest-extended');7const mock7 = require('jest-extended');8const mock8 = require('jest-extended');9const mock9 = require('jest-extended');10const mock10 = require('jest-extended');11const mock11 = require('jest-extended');12const mock12 = require('jest-extended');13const mock13 = require('jest-extended');14const mock14 = require('jest-extended');15const mock15 = require('jest-extended');16const mock16 = require('jest-extended');17const mock17 = require('jest-extended');18const mock18 = require('jest-extended');19const mock19 = require('jest-extended');20const mock20 = require('jest-extended');21const mock21 = require('jest-extended');22const mock22 = require('jest-extended');23const mock23 = require('jest-extended');
Using AI Code Generation
1expect(mock1).toHaveBeenCalled();2expect(mock2).toHaveBeenCalled();3expect(mock3).toHaveBeenCalled();4expect(mock4).toHaveBeenCalled();5expect(mock5).toHaveBeenCalled();6expect(mock6).toHaveBeenCalled();7expect(mock7).toHaveBeenCalled();8expect(mock8).toHaveBeenCalled();9expect(mock9).toHaveBeenCalled();10expect(mock10).toHaveBeenCalled();11expect(mock11).toHaveBeenCalled();12expect(mock12).toHaveBeenCalled();13expect(mock13).toHaveBeenCalled();14expect(mock14).toHaveBeenCalled();15expect(mock15).toHaveBeenCalled();16expect(mock16).toHaveBeenCalled();17expect(mock17).toHaveBeenCalled();18expect(mock18).toHaveBeenCalled();19expect(mock19).toHaveBeenCalled();20expect(mock20).toHaveBeenCalled();21expect(mock21).toHaveBeenCalled();22expect(mock22).toHaveBeenCalled();23expect(mock23).toHaveBeenCalled();24expect(mock24).toHaveBeenCalled();25expect(mock25).toHaveBeenCalled();26expect(mock26).toHaveBeenCalled();27expect(mock27).toHaveBeenCalled();28expect(mock28).toHaveBeenCalled();
Using AI Code Generation
1const mock1 = require('jest-extended');2const mock2 = require('jest-extended');3const mock1 = require('jest-extended');4const mock2 = require('jest-extended');5const mock1 = require('jest-extended');6const mock2 = require('jest-extended');7const mock1 = require('jest-extended');8const mock2 = require('jest-extended');9const mock1 = require('jest-extended');10const mock2 = require('jest-extended');11const mock1 = require('jest-extended');12const mock2 = require('jest-extended');13const mock1 = require('jest-extended');14const mock2 = require('jest-extended');15const mock1 = require('jest-extended');16const mock2 = require('jest-extended');17const mock1 = require('jest-extended');18const mock2 = require('jest-extended');19const mock1 = require('jest-extended');20const mock2 = require('jest-extended');21const mock1 = require('jest-extended');22const mock2 = require('jest-extended');23const mock1 = require('jest-extended');
Using AI Code Generation
1const mock1 = require('jest-extended/lib/matchers/toBeEmpty');2const mock2 = require('jest-extended/lib/matchers/toBeEmptyArray');3const mock3 = require('jest-extended/lib/matchers/toBeEmptyObject');4test('test1', () => {5 expect(mock1).toEqual(expect.any(Function));6});7test('test2', () => {8 expect(mock2).toEqual(expect.any(Function));9});10test('test3', () => {11 expect(mock3).toEqual(expect.any(Function));12});13module.exports = {14 toBeEmpty: jest.fn(),15 toBeEmptyArray: jest.fn(),16 toBeEmptyObject: jest.fn(),17};18const mock1 = require('jest-extended/lib/matchers/toBeEmpty');19const mock2 = require('jest-extended/lib/matchers/toBeEmptyArray');20const mock3 = require('jest-extended/lib/matchers/toBeEmptyObject');21test('test1', () => {22 expect(mock1).toEqual(expect.any(Function));23});24test('test2', () => {25 expect(mock2).toEqual(expect.any(Function));26});27test('test3', () => {28 expect(mock3).toEqual(expect.any(Function));29});30module.exports = {31 toBeEmpty: jest.fn(),32 toBeEmptyArray: jest.fn(),33 toBeEmptyObject: jest.fn(),34};35 Unhandled error. ('Error: Cannot find module \'jest-extended/lib/matchers/toBeEmpty\' from \'test.js\'')36 at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)37 at Object.<anonymous> (test.js:1:1)38 at printDeprecationMessage (node_modules/jest-util/build/index.js:315:11)39 at Object.<anonymous> (node_modules/jest-util/build/index.js:340:3)
Using AI Code Generation
1const mock1 = require('jest-extended/mock1');2const mock2 = require('jest-extended/mock2');3const mock3 = require('jest-extended/mock3');4describe('mock1', () => {5 it('should work', () => {6 const mock = mock1();7 expect(mock).toEqual('mock1');8 });9});10describe('mock2', () => {11 it('should work', () => {12 const mock = mock2();13 expect(mock).toEqual('mock2');14 });15});16describe('mock3', () => {17 it('should work', () => {18 const mock = mock3();19 expect(mock).toEqual('mock3');20 });21});22module.exports = () => 'mock1';23module.exports = () => 'mock2';24module.exports = () => 'mock3';25module.exports = {26};27{28 "scripts": {29 },30 "devDependencies": {31 }32}33You can see that I’ve created three mocks, each with a different name. I’ve also created a test file that imports each of these mocks and runs a test against them. Running this test shows the following output:
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!!