From 04c008e8828785ee22e875229d7dc3d22ff7c67b Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 12 Mar 2021 15:31:03 -0600 Subject: [PATCH] Improve: Increase testing coverage (#21015) --- app/settings/server/raw.tests.js | 47 ++++++++ client/lib/minimongo/comparisons.spec.ts | 130 +++++++++++++++++++++++ package-lock.json | 51 +++++---- package.json | 2 + 4 files changed, 212 insertions(+), 18 deletions(-) create mode 100644 app/settings/server/raw.tests.js create mode 100644 client/lib/minimongo/comparisons.spec.ts diff --git a/app/settings/server/raw.tests.js b/app/settings/server/raw.tests.js new file mode 100644 index 00000000000..7a9d6bccd6c --- /dev/null +++ b/app/settings/server/raw.tests.js @@ -0,0 +1,47 @@ +/* eslint-env mocha */ +import chai, { expect } from 'chai'; +import spies from 'chai-spies'; +import rewire from 'rewire'; + +chai.use(spies); + +describe('Raw Settings', () => { + let rawModule; + const cache = new Map(); + + before('rewire deps', () => { + const spy = chai.spy(async (id) => { + if (id === '1') { return 'some-setting-value'; } + return null; + }); + + rawModule = rewire('./raw'); + rawModule.__set__('setFromDB', spy); + rawModule.__set__('cache', cache); + }); + + it('should get the value from database when it isnt in cache', async () => { + const setting = await rawModule.getValue('1'); + + expect(setting).to.be.equal('some-setting-value'); + }); + + it('should get the value from cache when its available', async () => { + cache.set('2', 'supeer-setting'); + const setting = await rawModule.getValue('2'); + + expect(setting).to.be.equal('supeer-setting'); + }); + + it('should update the value in cache', async () => { + await rawModule.updateValue('2', { value: 'not-super-setting' }); + + expect(cache.get('2')).to.be.equal('not-super-setting'); + }); + + it('should not update the setting if the new value is undefined', async () => { + await rawModule.updateValue('2', {}); + + expect(cache.get('2')).to.be.equal('not-super-setting'); + }); +}); diff --git a/client/lib/minimongo/comparisons.spec.ts b/client/lib/minimongo/comparisons.spec.ts new file mode 100644 index 00000000000..488e091f4e8 --- /dev/null +++ b/client/lib/minimongo/comparisons.spec.ts @@ -0,0 +1,130 @@ +import chai from 'chai'; +import { describe, it } from 'mocha'; + +import { equals, isObject, flatSome, some, isEmptyArray } from './comparisons'; + +describe('Comparisons service', () => { + describe('equals', () => { + it('should return true if two numbers are equal', () => { + chai.expect(equals(1, 1)).to.be.equal(true); + }); + + it('should return false if arguments are null or undefined', () => { + chai.expect(equals(undefined, null)).to.be.equal(false); + chai.expect(equals(null, undefined)).to.be.equal(false); + }); + + it('should return false if arguments arent objects and they are not the same', () => { + chai.expect(equals('not', 'thesame')).to.be.equal(false); + }); + + it('should return true if date objects provided have the same value', () => { + const currentDate = new Date(); + + chai.expect(equals(currentDate, currentDate)).to.be.equal(true); + }); + + it('should return true if 2 equal UInt8Array are provided', () => { + const arr1 = new Uint8Array([1, 2]); + const arr2 = new Uint8Array([1, 2]); + + chai.expect(equals(arr1, arr2)).to.be.equal(true); + }); + + it('should return true if 2 equal arrays are provided', () => { + const arr1 = [1, 2, 4]; + const arr2 = [1, 2, 4]; + + chai.expect(equals(arr1, arr2)).to.be.equal(true); + }); + + it('should return false if 2 arrays with different length are provided', () => { + const arr1 = [1, 4, 5]; + const arr2 = [1, 4, 5, 7]; + + chai.expect(equals(arr1, arr2)).to.be.equal(false); + }); + + it('should return true if the objects provided are "equal"', () => { + const obj = { a: 1 }; + const obj2 = obj; + + chai.expect(equals(obj, obj2)).to.be.equal(true); + }); + + it('should return true if both objects have the same keys', () => { + const obj = { a: 1 }; + const obj2 = { a: 1 }; + + chai.expect(equals(obj, obj2)).to.be.equal(true); + }); + }); + + describe('isObject', () => { + it('should return true if value is an object or function', () => { + const obj = {}; + const func = (a: any): any => a; + + chai.expect(isObject(obj)).to.be.equal(true); + chai.expect(isObject(func)).to.be.equal(true); + }); + + it('should return false for other data types', () => { + chai.expect(isObject(1)).to.be.equal(false); + chai.expect(isObject(true)).to.be.equal(false); + chai.expect(isObject('212')).to.be.equal(false); + }); + }); + + describe('flatSome', () => { + it('should run .some on array', () => { + const arr = [1, 2, 4, 6, 9]; + const isEven = (v: number): boolean => v % 2 === 0; + + chai.expect(flatSome(arr, isEven)).to.be.equal(true); + }); + + it('should run the function on the value when its not an array', () => { + const val = 1; + const isEven = (v: number): boolean => v % 2 === 0; + + chai.expect(flatSome(val, isEven)).to.be.equal(false); + }); + }); + + describe('some', () => { + it('should run .some on array', () => { + const arr = [1, 2, 4, 6, 9]; + const isEven = (v: number | number[]): boolean => { + if (Array.isArray(v)) { return false; } + return v % 2 === 0; + }; + + chai.expect(some(arr, isEven)).to.be.equal(true); + }); + + it('should run the function on the value when its not an array', () => { + const val = 1; + const isEven = (v: number | number[]): boolean => { + if (Array.isArray(v)) { return false; } + return v % 2 === 0; + }; + + chai.expect(some(val, isEven)).to.be.equal(false); + }); + }); + + describe('isEmptyArray', () => { + it('should return true if array is empty', () => { + chai.expect(isEmptyArray([])).to.be.equal(true); + }); + + it('should return false if value is not an array', () => { + chai.expect(isEmptyArray(1)).to.be.equal(false); + }); + + it('should return false if array is not empty', () => { + chai.expect(isEmptyArray([1, 2])).to.be.equal(false); + }); + }); +}); diff --git a/package-lock.json b/package-lock.json index 07658333a6e..77c56ed8c86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11125,6 +11125,12 @@ "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz", "integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==" }, + "@types/rewire": { + "version": "2.5.28", + "resolved": "https://registry.npmjs.org/@types/rewire/-/rewire-2.5.28.tgz", + "integrity": "sha512-uD0j/AQOa5le7afuK+u+woi8jNKF1vf3DN0H7LCJhft/lNNibUr7VcAesdgtWfEKveZol3ZG1CJqwx2Bhrnl8w==", + "dev": true + }, "@types/semver": { "version": "7.3.3", "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.3.tgz", @@ -19929,7 +19935,7 @@ }, "chownr": { "version": "1.1.1", - "resolved": "", + "resolved": false, "integrity": "sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g==", "dev": true, "optional": true @@ -19964,7 +19970,7 @@ }, "debug": { "version": "4.1.1", - "resolved": "", + "resolved": false, "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", "dev": true, "optional": true, @@ -19995,7 +20001,7 @@ }, "fs-minipass": { "version": "1.2.5", - "resolved": "", + "resolved": false, "integrity": "sha512-JhBl0skXjUPCFH7x6x61gQxrKyXsxB5gcgePLZCwfyCGGsTISMoIeObbrvVeP6Xmyaudw4TT43qV2Gz+iyd2oQ==", "dev": true, "optional": true, @@ -20029,7 +20035,7 @@ }, "glob": { "version": "7.1.3", - "resolved": "", + "resolved": false, "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", "dev": true, "optional": true, @@ -20061,7 +20067,7 @@ }, "ignore-walk": { "version": "3.0.1", - "resolved": "", + "resolved": false, "integrity": "sha512-DTVlMx3IYPe0/JJcYP7Gxg7ttZZu3IInhuEhbchuqneY9wWe5Ojy2mXLBaQFUQmo0AW2r3qG7m1mg86js+gnlQ==", "dev": true, "optional": true, @@ -20082,7 +20088,7 @@ }, "inherits": { "version": "2.0.3", - "resolved": "", + "resolved": false, "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", "dev": true, "optional": true @@ -20130,7 +20136,7 @@ }, "minipass": { "version": "2.3.5", - "resolved": "", + "resolved": false, "integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==", "dev": true, "optional": true, @@ -20141,7 +20147,7 @@ }, "minizlib": { "version": "1.2.1", - "resolved": "", + "resolved": false, "integrity": "sha512-7+4oTUOWKg7AuL3vloEWekXY2/D20cevzsrNT2kGWm+39J9hGTCBv8VI5Pm5lXZ/o3/mdR4f8rflAPhnQb8mPA==", "dev": true, "optional": true, @@ -20161,7 +20167,7 @@ }, "ms": { "version": "2.1.1", - "resolved": "", + "resolved": false, "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", "dev": true, "optional": true @@ -20175,7 +20181,7 @@ }, "needle": { "version": "2.3.0", - "resolved": "", + "resolved": false, "integrity": "sha512-QBZu7aAFR0522EyaXZM0FZ9GLpq6lvQ3uq8gteiDUp7wKdy0lSd2hPlgFwVuW1CBkfEs9PfDQsQzZghLs/psdg==", "dev": true, "optional": true, @@ -20187,7 +20193,7 @@ }, "node-pre-gyp": { "version": "0.12.0", - "resolved": "", + "resolved": false, "integrity": "sha512-4KghwV8vH5k+g2ylT+sLTjy5wmUOb9vPhnM8NHvRf9dHmnW/CndrFXy2aRPaPST6dugXSdHXfeaHQm77PIz/1A==", "dev": true, "optional": true, @@ -20217,14 +20223,14 @@ }, "npm-bundled": { "version": "1.0.6", - "resolved": "", + "resolved": false, "integrity": "sha512-8/JCaftHwbd//k6y2rEWp6k1wxVfpFzB6t1p825+cUb7Ym2XQfhwIC5KwhrvzZRJu+LtDE585zVaS32+CGtf0g==", "dev": true, "optional": true }, "npm-packlist": { "version": "1.4.1", - "resolved": "", + "resolved": false, "integrity": "sha512-+TcdO7HJJ8peiiYhvPxsEDhF3PJFGUGRcFsGve3vxvxdcpO2Z4Z7rkosRM0kWj6LfbK/P0gu3dzk5RU1ffvFcw==", "dev": true, "optional": true, @@ -20304,7 +20310,7 @@ }, "process-nextick-args": { "version": "2.0.0", - "resolved": "", + "resolved": false, "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==", "dev": true, "optional": true @@ -20349,7 +20355,7 @@ }, "rimraf": { "version": "2.6.3", - "resolved": "", + "resolved": false, "integrity": "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==", "dev": true, "optional": true, @@ -20380,7 +20386,7 @@ }, "semver": { "version": "5.7.0", - "resolved": "", + "resolved": false, "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", "dev": true, "optional": true @@ -20440,7 +20446,7 @@ }, "tar": { "version": "4.4.8", - "resolved": "", + "resolved": false, "integrity": "sha512-LzHF64s5chPQQS0IYBn9IN5h3i98c12bo4NCO7e0sGM2llXQ3p2FGC5sdENN4cTW48O915Sh+x+EXx7XW96xYQ==", "dev": true, "optional": true, @@ -20480,7 +20486,7 @@ }, "yallist": { "version": "3.0.3", - "resolved": "", + "resolved": false, "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==", "dev": true, "optional": true @@ -32243,6 +32249,15 @@ "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", "dev": true }, + "rewire": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/rewire/-/rewire-5.0.0.tgz", + "integrity": "sha512-1zfitNyp9RH5UDyGGLe9/1N0bMlPQ0WrX0Tmg11kMHBpqwPJI4gfPpP7YngFyLbFmhXh19SToAG0sKKEFcOIJA==", + "dev": true, + "requires": { + "eslint": "^6.8.0" + } + }, "rimraf": { "version": "2.4.5", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz", diff --git a/package.json b/package.json index 10084ed72af..297ac09fa73 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@types/mongodb": "^3.5.26", "@types/node": "^10.12.14", "@types/react-dom": "^16.9.8", + "@types/rewire": "^2.5.28", "@types/semver": "^7.3.3", "@types/toastr": "^2.1.38", "@typescript-eslint/eslint-plugin": "^2.34.0", @@ -115,6 +116,7 @@ "postcss-url": "^8.0.0", "progress": "^2.0.3", "proxyquire": "^2.1.3", + "rewire": "^5.0.0", "simple-git": "^1.107.0", "source-map": "^0.5.7", "stylelint": "^13.6.1",