@@ -5,7 +5,7 @@ require('./setup')
55const _ = require ( '../src/index' )
66
77describe ( 'fauxdash' , function ( ) {
8- describe ( 'Array Functions ' , function ( ) {
8+ describe ( 'Array functions ' , function ( ) {
99 it ( 'should correctly evaluate any' , function ( ) {
1010 _ . any ( [ , , , 5 ] ) . should . equal ( true )
1111 _ . any ( [ , , , , ] ) . should . equal ( false )
@@ -60,6 +60,31 @@ describe('fauxdash', function () {
6060 . should . eql ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] )
6161 _ . uniq ( [ 'alice' , 'bob' , 'clarice' , 'dan' , 'erica' , 'frank' , 'alice' , 'erica' , 'dan' , 'felicia' , 'dan' , 'frank' ] )
6262 . should . eql ( [ 'alice' , 'bob' , 'clarice' , 'dan' , 'erica' , 'frank' , 'felicia' ] )
63+
64+ _ . unique ( [
65+ { name : 'elizabeth' } ,
66+ { name : 'alice' } ,
67+ { name : 'francesca' } ,
68+ { name : 'barbara' } ,
69+ { name : 'clarice' } ,
70+ { name : 'francesca' } ,
71+ { name : 'clarice' } ,
72+ { name : 'daria' } ,
73+ { name : 'barbara' } ,
74+ { name : 'elizabeth' } ,
75+ { name : 'francesca' } ,
76+ { name : 'gloria' } ,
77+ { name : 'gloria' }
78+ ] , x => x . name )
79+ . should . eql ( [
80+ { name : 'elizabeth' } ,
81+ { name : 'alice' } ,
82+ { name : 'francesca' } ,
83+ { name : 'barbara' } ,
84+ { name : 'clarice' } ,
85+ { name : 'daria' } ,
86+ { name : 'gloria' }
87+ ] )
6388 } )
6489
6590 it ( 'should apply without correctly' , function ( ) {
@@ -231,4 +256,217 @@ describe('fauxdash', function () {
231256 m2 ( { a : true , b : 'string' , c : { d : 2 } } ) . should . equal ( false )
232257 } )
233258 } )
259+
260+ describe ( 'Sorting by property' , ( ) => {
261+ it ( 'should sort numeric properties correctly' , ( ) => {
262+ _ . sortBy ( [ { age : 30 } , { age : 10 } , { age : 40 } , { age : 19 } , { age : 2 } ] , 'age' )
263+ . should . eql ( [ { age : 2 } , { age : 10 } , { age : 19 } , { age : 30 } , { age : 40 } ] )
264+ } )
265+
266+ it ( 'should sort alphabetic properties correctly' , ( ) => {
267+ _ . sortBy ( [ { name : 'zak' } , { name : 'ed' } , { name : 'amy' } , { name : 'jim' } , { name : 'pam' } ] , 'name' )
268+ . should . eql ( [ { name : 'amy' } , { name : 'ed' } , { name : 'jim' } , { name : 'pam' } , { name : 'zak' } ] )
269+ } )
270+ } )
271+
272+ describe ( 'MapCall: Spreading hash properties over function parameters' , ( ) => {
273+ function testCall ( actor , argOne , argTwo , argThree ) {
274+ return [ actor , argOne , argTwo , argThree ]
275+ }
276+
277+ var model = {
278+ test : testCall
279+ }
280+ var actor = { id : 'testing' }
281+
282+ describe ( 'with exact matches' , ( ) => {
283+ var message = { argOne : 1 , argTwo : 'two' , argThree : true }
284+ var result
285+
286+ before ( ( ) => {
287+ var fn = _ . mapCall ( model . test , true )
288+ result = fn ( actor , message )
289+ } )
290+
291+ it ( 'should call the function with correct arguments' , ( ) => {
292+ result . should . eql ( [ actor , 1 , 'two' , true ] )
293+ } )
294+ } )
295+
296+ describe ( 'with partial matches and a map' , ( ) => {
297+ describe ( 'and a map' , ( ) => {
298+ var message = { argOne : 1 , arg2 : 'two' , argThree : true }
299+ var result
300+
301+ before ( ( ) => {
302+ var fn = _ . mapCall ( model . test , {
303+ argTwo : 'arg2'
304+ } )
305+
306+ result = fn ( actor , message )
307+ } )
308+
309+ it ( 'should call the function with correct arguments' , ( ) => {
310+ result . should . eql ( [ actor , 1 , 'two' , true ] )
311+ } )
312+ } )
313+
314+ describe ( 'and no map' , ( ) => {
315+ var message = { argOne : 1 , arg2 : 'two' , argThree : true }
316+ var result
317+
318+ before ( ( ) => {
319+ var fn = _ . mapCall ( model . test , true )
320+ result = fn ( actor , message )
321+ } )
322+
323+ it ( 'should call the function with correct arguments' , ( ) => {
324+ result . should . eql ( [ actor , 1 , undefined , true ] )
325+ } )
326+ } )
327+ } )
328+
329+ describe ( 'with no matches' , ( ) => {
330+ describe ( 'and a map' , ( ) => {
331+ var message = { arg1 : 1 , arg2 : 'two' , arg3 : true }
332+ var result
333+
334+ before ( ( ) => {
335+ var fn = _ . mapCall ( model . test , {
336+ argOne : 'arg1' ,
337+ argTwo : 'arg2' ,
338+ argThree : 'arg3'
339+ } )
340+
341+ result = fn ( actor , message )
342+ } )
343+
344+ it ( 'should call the function with correct arguments' , ( ) => {
345+ result . should . eql ( [ actor , 1 , 'two' , true ] )
346+ } )
347+ } )
348+
349+ describe ( 'and no valid map' , ( ) => {
350+ var message = { arg1 : 1 , arg2 : 'two' , arg3 : true }
351+ var result
352+
353+ before ( ( ) => {
354+ var fn = _ . mapCall ( model . test , true )
355+ result = fn ( actor , message )
356+ } )
357+
358+ it ( 'should call the function with undefined arguments' , ( ) => {
359+ result . should . eql ( [ actor , undefined , undefined , undefined ] )
360+ } )
361+ } )
362+ } )
363+ } )
364+
365+ describe ( 'Object manipulation' , function ( ) {
366+ it ( 'should omit keys' , function ( ) {
367+ _ . omit ( { a : 1 , b : 2 , c : 3 , d : 4 , e : 5 , f : 6 } , 'c' , 'f' , 'i' )
368+ . should . eql ( { a : 1 , b : 2 , d : 4 , e : 5 } )
369+
370+ _ . omit ( { a : 1 , b : 2 , c : 3 , d : 4 , e : 5 , f : 6 } , [ 'a' , 'd' , 'g' ] )
371+ . should . eql ( { b : 2 , c : 3 , e : 5 , f : 6 } )
372+ } )
373+
374+ it ( 'should transform object' , function ( ) {
375+ _ . transform ( { a : 1 , b : 2 , c : 3 , d : 4 } , { b : 'beta' , c : 'gamma' } )
376+ . should . eql ( { a : 1 , beta : 2 , gamma : 3 , d : 4 } )
377+
378+ _ . transform ( { a : 1 , b : 2 , c : 3 , d : 4 } , { b : 'beta' , c : 'gamma' , d : 'delta' , e : 'echo' } , 'a' , 'd' )
379+ . should . eql ( { beta : 2 , gamma : 3 } )
380+ } )
381+
382+ it ( 'should populate missing keys from defaults' , function ( ) {
383+ const default1 = { b : 2 , c : 4 , d : 4 }
384+ const default2 = { a : 1 , b : 1 }
385+ const settings = { c : 3 }
386+ const result = _ . defaults ( settings , default1 , default2 )
387+ result . should . eql ( { a : 1 , b : 2 , c : 3 , d : 4 } )
388+ } )
389+
390+ it ( 'should create a valid deep clone' , function ( ) {
391+ class Item {
392+ constructor ( a , b , c ) {
393+ this . a = a
394+ this . b = b
395+ this . c = c
396+ }
397+ }
398+ const source = {
399+ a : 1 ,
400+ b : 'two' ,
401+ c : true ,
402+ d : [ 1 , 'two' , true ] ,
403+ e : {
404+ a : 2 ,
405+ b : [ 'three' , 'four' , [ 5 , 6 , 7 ] ]
406+ } ,
407+ f : [
408+ { a : 'one' , b : 'two' } ,
409+ { a : 'one' , b : 'two' , c : [ 1 , 2 , 3 , Date . now ( ) ] } ,
410+ { a : 'one' , b : 'two' } ,
411+ { items : [
412+ new Item ( 1 , 2 , 3 ) ,
413+ new Item ( '4' , 'five' , { six : 6 } ) ,
414+ new Item ( true , Date . now ( ) , 9 ) ,
415+ ] }
416+ ]
417+ }
418+ const copy = _ . clone ( source )
419+ copy . should . eql ( source )
420+ copy . f [ 3 ] . items [ 1 ] . c . six = 7
421+ copy . should . not . eql ( source )
422+ } )
423+ } )
424+
425+ describe ( 'Promise helpers' , function ( ) {
426+ describe ( 'when applying promises to a function call' , function ( ) {
427+ it ( 'should resolve arguments first' , function ( ) {
428+ return _ . applyWhen ( ( a , b , c , d ) => {
429+ return ( a + b + c ) / d
430+ } , [
431+ 1 ,
432+ Promise . resolve ( 2 ) ,
433+ Promise . resolve ( 3 ) ,
434+ 2
435+ ] ) . should . eventually . equal ( 3 )
436+ } )
437+
438+ it ( 'should reject on failed argument' , function ( ) {
439+ return _ . applyWhen ( ( a , b , c , d ) => {
440+ return ( a + b + c ) / d
441+ } , [
442+ 1 ,
443+ Promise . reject ( new Error ( 'no' ) ) ,
444+ Promise . resolve ( 3 ) ,
445+ 2
446+ ] ) . should . be . rejectedWith ( 'no' )
447+ } )
448+ } )
449+
450+ describe ( 'when calling promise functions in a sequence' , function ( ) {
451+ describe ( 'calling functions from an array' , function ( ) {
452+ it ( 'should resolve to an ordered array' , function ( ) {
453+ return _ . sequence ( [
454+ ( ) => Promise . resolve ( 1 ) ,
455+ ( ) => 2 ,
456+ ( ) => Promise . resolve ( 3 )
457+ ] ) . should . eventually . eql ( [ 1 , 2 , 3 ] )
458+ } )
459+ } )
460+
461+ describe ( 'calling function arguments' , function ( ) {
462+ it ( 'should resolve to an ordered array' , function ( ) {
463+ return _ . sequence (
464+ ( ) => Promise . resolve ( 1 ) ,
465+ ( ) => 2 ,
466+ ( ) => Promise . resolve ( 3 )
467+ ) . should . eventually . eql ( [ 1 , 2 , 3 ] )
468+ } )
469+ } )
470+ } )
471+ } )
234472} )
0 commit comments