diff --git a/CHANGELOG.md b/CHANGELOG.md index 942edbe..9bc6a46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # HearthJS +### v2.5.1 +- Add assertTableOfObject in helper + ### v2.5.0 - Remove JSON parse middleware from hearthjs, now you have to set it in your **init function**. diff --git a/lib/helper.js b/lib/helper.js index 55d7504..505d131 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -1,5 +1,6 @@ const fs = require('fs') const path = require('path') +const assert = require('assert') const modeName = { dev: 'DEVELOPMENT', @@ -132,6 +133,24 @@ const helper = { }, null, 2), callback) }, + /** + * Assert two arrays or equal without checking elements order + * @param {Array} actual Array to check + * @param {Array} expected Expected array + * @param {String} key Key to match array + */ + assertTableOfObject: function (actual, expected, key) { + for (let i = 0; i < expected.length; i++) { + let index = actual.findIndex(item => item[key] === expected[i][key]) + + if (index === -1) { + throw new Error('Object not found with key ' + expected[i][key]) + } + + assert.deepStrictEqual(expected[i], actual[index]) + } + }, + /** * Loop on an array to process all items * @param {Array} items List of item to process diff --git a/package.json b/package.json index cdd6963..9fd6467 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hearthjs", - "version": "2.5.0", + "version": "2.5.1", "description": "A NodeJS server framework to build fast Rest API", "main": "lib/index.js", "bin": "bin/hearthjs", diff --git a/test/helper.js b/test/helper.js index d0b0ed0..11faad2 100644 --- a/test/helper.js +++ b/test/helper.js @@ -3,6 +3,89 @@ const helper = require('../lib/helper') const assert = require('assert') describe('Helper', () => { + describe('Assert array of object', () => { + it('should assert arrays are equal if items are in the same order', () => { + let actual = [ + { id: 1, name: 'toto', obj: { key: 'lala' } }, + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } } + ] + let expected = [ + { id: 1, name: 'toto', obj: { key: 'lala' } }, + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } } + ] + + try { + helper.assertTableOfObject(actual, expected, 'id') + assert.strictEqual(true, true) + } catch (e) { + assert.strictEqual(true, false) + } + }) + + it('should assert arrays are equal if items are not in the same order', () => { + let actual = [ + { id: 1, name: 'toto', obj: { key: 'lala' } }, + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } } + ] + let expected = [ + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } }, + { id: 1, name: 'toto', obj: { key: 'lala' } } + ] + + try { + helper.assertTableOfObject(actual, expected, 'id') + assert.strictEqual(true, true) + } catch (e) { + console.log(e) + assert.strictEqual(true, false) + } + }) + + it('should throw an error if on object is missing', () => { + let actual = [ + { id: 1, name: 'toto', obj: { key: 'lala' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } } + ] + let expected = [ + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } }, + { id: 1, name: 'toto', obj: { key: 'lala' } } + ] + + try { + helper.assertTableOfObject(actual, expected, 'id') + assert.strictEqual(true, false) + } catch (e) { + assert.strictEqual(e.message, 'Object not found with key 2') + assert.strictEqual(true, true) + } + }) + + it('should throw an error if one value is different', () => { + let actual = [ + { id: 1, name: 'toto', obj: { key: 'lala' } }, + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'lili' } } + ] + let expected = [ + { id: 2, name: 'tata', obj: { key: 'lolo' } }, + { id: 3, name: 'titi', obj: { key: 'luli' } }, + { id: 1, name: 'toto', obj: { key: 'lala' } } + ] + + try { + helper.assertTableOfObject(actual, expected, 'id') + assert.strictEqual(true, false) + } catch (e) { + assert.strictEqual(true, true) + } + }) + }) + describe('Handle promise error', () => { it('should return an error if promise return an error', async () => { let promise = new Promise((resolve, reject) => {