1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-27 23:27:50 +01:00

Add tests for sanitize.js

Installs rewire from npm and use it to test functions
in sanitize.js

Part of https://github.com/bigchaindb/js-bigchaindb-driver/issues/220
This commit is contained in:
Arjun Nemani 2018-07-19 19:54:10 +05:30
parent a3be6c63a6
commit 5a9f3905c7
2 changed files with 61 additions and 0 deletions

View File

@ -80,6 +80,7 @@
"js-utility-belt": "^1.5.0",
"json-stable-stringify": "^1.0.1",
"query-string": "^6.0.0",
"rewire": "^4.0.1",
"sprintf-js": "^1.1.1",
"tweetnacl": "^1.0.0",
"uglifyjs-webpack-plugin": "^1.2.7",

View File

@ -0,0 +1,60 @@
import test from 'ava'
import rewire from 'rewire'
const sanitize = rewire('../../src/sanitize.js')
const applyFilterOnObject = sanitize.__get__('applyFilterOnObject')
const filterFromObject = sanitize.__get__('filterFromObject')
test('Ensure that null filter returns same object', t => {
const expected = { 'testObj': 'test' }
const actual = applyFilterOnObject({ 'testObj': 'test' }, null)
t.deepEqual(actual, expected)
})
test('Ensure function filter with isInclusion true works properly', t => {
const testObj = [true, false, undefined, '', 0, null]
const expected = { 0: true }
const actual = filterFromObject(testObj, (val) => !!val, { isInclusion: true })
t.deepEqual(actual, expected)
})
test('Ensure function filter with isInclusion false works properly', t => {
const testObj = [false, true, 1, 10, 'this will be removed as it is truthy']
const expected = { 0: false }
const actual = filterFromObject(testObj, (val) => !!val, { isInclusion: false })
t.deepEqual(actual, expected)
})
test('Ensure array filter with isInclusion true works properly', t => {
const testObj = [true, false, undefined, '', 0, null]
const expected = { 0: true }
const actual = filterFromObject(testObj, [true], { isInclusion: true })
t.deepEqual(actual, expected)
})
test('Ensure array filter with isInclusion false works properly', t => {
const testObj = [false, true, 1, 10]
const expected = { 0: false }
const actual = filterFromObject(testObj, [true, 1, 10], { isInclusion: false })
t.deepEqual(actual, expected)
})
test('Ensure throws error when given invalid filter', t => {
const error = t.throws(() => {
filterFromObject({}, 'lol')
}, Error)
t.is(error.message, 'The given filter is not an array or function. Filter aborted')
})