|
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +require('./setup') |
| 4 | + |
| 5 | +const _ = require('../src/index') |
| 6 | + |
| 7 | +describe('fauxdash', function () { |
| 8 | + describe('Array Functions', function () { |
| 9 | + it('should correctly evaluate any', function () { |
| 10 | + _.any([,,, 5]).should.equal(true) |
| 11 | + _.any([,,,, ]).should.equal(false) |
| 12 | + _.any([1,,,, ]).should.equal(true) |
| 13 | + _.any([,, 10,, ], x => x > 5).should.equal(true) |
| 14 | + _.any([,,,, 10], x => x < 5).should.equal(false) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should correctly evaluate contains', function () { |
| 18 | + _.contains(['a', 'b', 'c', 'd'], 'b').should.equal(true) |
| 19 | + _.contains(['a', 'b', 'c', 'd'], 'e').should.equal(false) |
| 20 | + _.contains([1, 2, 3, 4, 5], 6).should.equal(false) |
| 21 | + _.contains([1, 2, 3, 4, 5], 3).should.equal(true) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should filter correctly', function () { |
| 25 | + _.filter([,, 4,, 1]).should.eql([4, 1]) |
| 26 | + _.filter(['a',,,, 'b']).should.eql(['a', 'b']) |
| 27 | + _.filter([1, 2, 3, 4, 5, 6], x => x % 2 === 0).should.eql([2, 4, 6]) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should find correctly', function () { |
| 31 | + _.find([,,, 10,,, ]).should.equal(10) |
| 32 | + _.find([9, 3, 1, 10, 4, 2, 8], x => x >= 10).should.equal(10) |
| 33 | + _.find(['cat', 'bat', 'hat', 'rat', 'mat'], x => x === 'bat').should.equal('bat') |
| 34 | + expect(_.find(['a', 'b', 'c'], x => x === 'd')).equal(undefined) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should flatten correctly', function () { |
| 38 | + _.flatten([1, 2, 3, 4]).should.eql([1, 2, 3, 4]) |
| 39 | + _.flatten([1, [2, 3], [4]]).should.eql([1, 2, 3, 4]) |
| 40 | + _.flatten([1, [2, [3]], [4], [[[5, 6, 7]]]]).should.eql([1, 2, 3, 4, 5, 6, 7]) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('Is', function () { |
| 45 | + it('should correctly identify dates', function () { |
| 46 | + _.isDate(new Date()).should.equal(true) |
| 47 | + _.isDate('01/01/2010').should.equal(false) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should correctly identify functions', function () { |
| 51 | + _.isFunction(function () {}).should.equal(true) |
| 52 | + _.isFunction(() => {}).should.equal(true) |
| 53 | + _.isFunction(x => x).should.equal(true) |
| 54 | + _.isFunction('').should.equal(false) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should correctly identify numbers', function () { |
| 58 | + _.isNumber(100).should.equal(true) |
| 59 | + _.isNumber(100.5).should.equal(true) |
| 60 | + _.isNumber('5').should.equal(false) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should correctly identify objects', function () { |
| 64 | + _.isObject(new Object()).should.equal(true) |
| 65 | + _.isObject({}).should.equal(true) |
| 66 | + _.isObject(new Date()).should.equal(true) |
| 67 | + _.isObject(() => {}).should.equal(true) |
| 68 | + _.isObject('hi').should.equal(false) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should correctly identify plain objects', function () { |
| 72 | + _.isObject(new Object()).should.equal(true) |
| 73 | + _.isObject({}).should.equal(true) |
| 74 | + _.isObject(() => {}).should.equal(true) |
| 75 | + _.isObject(5).should.equal(false) |
| 76 | + _.isObject('hi').should.equal(false) |
| 77 | + }) |
| 78 | + |
| 79 | + it('should correctly identify promisey objects', function () { |
| 80 | + _.isPromisey(new Promise(() => {})).should.equal(true) |
| 81 | + _.isPromisey({then: () => {}}).should.equal(true) |
| 82 | + _.isPromisey(new Object()).should.equal(false) |
| 83 | + _.isPromisey('no').should.equal(false) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should correctly identify strings', function () { |
| 87 | + _.isString('yay').should.equal(true) |
| 88 | + _.isString(5).should.equal(false) |
| 89 | + _.isString(['a', 'b', 'c']).should.equal(false) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + describe('Function tools', function () { |
| 94 | + it('should get arguments correctly', function () { |
| 95 | + _.getArguments(function (a, b, c) {}).should.eql(['a', 'b', 'c']) |
| 96 | + _.getArguments((a, b, c, d) => {}).should.eql(['a', 'b', 'c', 'd']) |
| 97 | + _.getArguments(a => {}).should.eql(['a']) |
| 98 | + }) |
| 99 | + |
| 100 | + it('should parse function correctly', function () { |
| 101 | + _.parseFunction(function one (a, b, c) {}) |
| 102 | + .should.eql({ name: 'one', arguments: ['a', 'b', 'c'], body: undefined }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should parse function correctly', function () { |
| 106 | + _.parseFunction((a, b) => {}) |
| 107 | + .should.eql({ name: undefined, arguments: ['a', 'b'], body: undefined }) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should parse function correctly', function () { |
| 111 | + _.parseFunction(a => {}) |
| 112 | + .should.eql({ name: undefined, arguments: ['a'], body: undefined }) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('Utility functions', function () { |
| 117 | + let lifted |
| 118 | + before(function () { |
| 119 | + function asyncCall (a, cb) { |
| 120 | + if (!a) { cb(new Error('nope')) } |
| 121 | + cb(null, a) |
| 122 | + } |
| 123 | + lifted = _.lift(asyncCall) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should trim and filter list of strings correctly', function () { |
| 127 | + _.trim([]).should.eql([]) |
| 128 | + _.trim([,, '', '']).should.eql([]) |
| 129 | + _.trim([,, ' ', ' ']).should.eql([]) |
| 130 | + _.trim(['a ', ' b', 'c', ' d ', 'e ']).should.eql(['a', 'b', 'c', 'd', 'e']) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should reject on failed async function', function () { |
| 134 | + return lifted(null).should.be.rejectedWith('nope') |
| 135 | + }) |
| 136 | + |
| 137 | + it('should resolved on successful function', function () { |
| 138 | + return lifted(100).should.eventually.equal(100) |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments