Skip to content

Commit

Permalink
Add assertTableOfObject helper;
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Magné committed Jul 17, 2020
1 parent b70d1f5 commit fcd5746
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**.

Expand Down
19 changes: 19 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
const assert = require('assert')

const modeName = {
dev: 'DEVELOPMENT',
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
83 changes: 83 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit fcd5746

Please sign in to comment.