From 5a9f3905c78a3b5703be39c75616c17328dde327 Mon Sep 17 00:00:00 2001 From: Arjun Nemani Date: Thu, 19 Jul 2018 19:54:10 +0530 Subject: [PATCH] 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 --- package.json | 1 + test/sanitize/test_sanitize.js | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/sanitize/test_sanitize.js diff --git a/package.json b/package.json index dc9aba4..e8e41cf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/sanitize/test_sanitize.js b/test/sanitize/test_sanitize.js new file mode 100644 index 0000000..1d381d4 --- /dev/null +++ b/test/sanitize/test_sanitize.js @@ -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') +}) +