halis-state
Version:
Manage immutable state with these helpers for Objects and Arrays
368 lines (264 loc) • 10.9 kB
JavaScript
;
const expect = require( 'chai' ).expect;
const util = require( '../src/state.js' );
describe( 'halis-state', () => {
let value, copy, merge;
describe( '#isObject()', () => {
it( 'should return true if passed an Object', () => {
expect( util.isObject( {} ) ).to.equal( true );
expect( util.isObject( { hello: 'world' } ) ).to.equal( true );
expect( util.isObject( new Date() ) ).to.equal( true );
});
it( 'should return false if passed an Array', () => {
expect( util.isObject( [] ) ).to.equal( false );
});
it( 'should return false if passed a Function', () => {
expect( util.isObject( function hello() { return 0; } ) ).to.equal( false );
});
it( 'should return false if passed null', () => {
expect( util.isObject( null ) ).to.equal( false );
});
it( 'should return false if passed undefined', () => {
expect( util.isObject() ).to.equal( false );
});
it( 'should return false if passed Number, Boolean or String', () => {
expect( util.isObject( 1 ) ).to.equal( false );
expect( util.isObject( false ) ).to.equal( false );
expect( util.isObject( 'asdf' ) ).to.equal( false );
});
});
describe( '#isArray()', () => {
it( 'should return true if passed an Array', () => {
expect( util.isArray( [] ) ).to.equal( true );
expect( util.isArray( [ 1, 2, 3 ] ) ).to.equal( true );
});
it( 'should return false if passed an Object', () => {
expect( util.isArray( { hello: 'world' } ) ).to.equal( false );
});
it( 'should return false if passed a Function', () => {
expect( util.isArray( function hello() { return 0; } ) ).to.equal( false );
});
it( 'should return false if passed null', () => {
expect( util.isArray( null ) ).to.equal( false );
});
it( 'should return false if passed undefined', () => {
expect( util.isArray() ).to.equal( false );
});
it( 'should return false if passed Number, Boolean or String', () => {
expect( util.isArray( 1 ) ).to.equal( false );
expect( util.isArray( false ) ).to.equal( false );
expect( util.isArray( 'asdf' ) ).to.equal( false );
});
});
describe( '#isString()', () => {
it( 'should return true if passed a String', () => {
expect( util.isString( 'asdf' ) ).to.equal( true );
});
it( 'should return false if passed an Object', () => {
expect( util.isString( { hello: 'world' } ) ).to.equal( false );
});
it( 'should return false if passed a Function', () => {
expect( util.isString( function hello() { return 0; } ) ).to.equal( false );
});
it( 'should return false if passed null', () => {
expect( util.isString( null ) ).to.equal( false );
});
it( 'should return false if passed undefined', () => {
expect( util.isString() ).to.equal( false );
});
it( 'should return false if passed Number or Boolean', () => {
expect( util.isString( 123 ) ).to.equal( false );
expect( util.isString( false ) ).to.equal( false );
});
});
describe( '#defaultObject()', () => {
it( 'should return {} if argument is null, undefined or not an Object', () => {
value = util.defaultObject( null );
expect( value ).to.deep.equal( {} );
value = util.defaultObject();
expect( value ).to.deep.equal( {} );
value = util.defaultObject( 'asdf' );
expect( value ).to.deep.equal( {} );
value = util.defaultObject( 124 );
expect( value ).to.deep.equal( {} );
});
it( 'should return the Object if it is an Object', () => {
value = util.defaultObject( {} );
expect( value ).to.deep.equal( {} );
value = util.defaultObject( { hello: 'world' } );
expect( value ).to.deep.equal( { hello: 'world' } );
});
});
describe( '#defaultArray()', () => {
it( 'should return [] if argument is null, undefined or not an Array', () => {
value = util.defaultArray( null );
expect( value ).to.deep.equal( [] );
value = util.defaultArray();
expect( value ).to.deep.equal( [] );
value = util.defaultArray( 'asdf' );
expect( value ).to.deep.equal( [] );
value = util.defaultArray( 124 );
expect( value ).to.deep.equal( [] );
});
it( 'should return the Array if it is an Array', () => {
value = util.defaultArray( [] );
expect( value ).to.deep.equal( [] );
value = util.defaultArray( [ 1, 2 ] );
expect( value ).to.deep.equal( [ 1, 2 ] );
});
});
describe( '#copyObject()', () => {
it( 'with 1 parameter it should return a copy of the object', () => {
value = { hello: 'world' };
copy = util.copyObject( value );
expect( value ).to.deep.equal( copy ); // has same properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
it( 'with 2 parameters it should return a copy of the merged objects', () => {
value = { hello: 'world' };
merge = { billy: 'bob' };
copy = util.copyObject( value, merge );
expect( copy ).to.deep.equal( { hello: 'world', billy: 'bob' } ); // has all properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
it( 'with null parameters should return {}', () => {
copy = util.copyObject( null, null );
expect( copy ).to.deep.equal( {} ); // has all properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
it( 'with undefined parameters should return {}', () => {
copy = util.copyObject();
expect( copy ).to.deep.equal( {} ); // has all properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
});
describe( '#copyArray()', () => {
it( 'with 1 parameter it should return a copy of the array', () => {
value = [ 1, 2 ];
copy = util.copyArray( value );
expect( value ).to.deep.equal( copy ); // has same properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
it( 'with 2 parameters it should return a copy of the merged arrays', () => {
value = [ 1, 2 ];
merge = [ 3, 4 ];
copy = util.copyArray( value, merge );
expect( copy ).to.deep.equal( [ 1, 2, 3,4 ] ); // has all properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
it( 'with null parameters should return {}', () => {
copy = util.copyArray( null, null );
expect( copy ).to.deep.equal( [] ); // has all properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
it( 'with undefined parameters should return {}', () => {
copy = util.copyArray();
expect( copy ).to.deep.equal( [] ); // has all properties
expect( value ).to.equal( value ); // references itself
expect( value ).to.not.equal( copy ); // different reference
});
});
describe( '#insertObject()', () => {
it( 'should copy the object and insert the new key', () => {
value = { hello: 'world' };
copy = util.insertObject( value, 'age', 35 );
expect( copy ).to.not.equal( value );
expect( copy.hello ).to.equal( 'world' );
expect( copy.age ).to.equal( 35 );
});
it( 'should null at the given key if value is not supplied', () => {
value = { hello: 'world' };
copy = util.insertObject( value, 'age' );
expect( copy ).to.not.equal( value );
expect( copy.hello ).to.equal( 'world' );
expect( copy.age ).to.equal( null );
});
it( 'should return the same object if the key is missing or the key is not a string', () => {
value = { hello: 'world' };
copy = util.insertObject( value );
expect( copy ).to.equal( value ); // same reference
expect( copy ).to.deep.equal( value );
});
});
describe( '#pushArray()', () => {
it( 'should copy the array and push the new value', () => {
value = [ 1, 2 ];
copy = util.pushArray( value, 3 );
expect( copy ).to.not.equal( value );
expect( copy ).to.deep.equal( [ 1, 2, 3 ] );
});
it( 'should return the original array if value not supplied', () => {
value = [ 1, 2 ];
copy = util.pushArray( value );
expect( copy ).to.equal( value );
expect( copy ).to.deep.equal( [ 1, 2 ] );
});
it( 'should return a copy of the array and push null if null is the value', () => {
value = [ 1, 2 ];
copy = util.pushArray( value, null );
expect( copy ).to.not.equal( value );
expect( copy ).to.deep.equal( [ 1, 2, null ] );
});
it( 'if array is null or not an array return a new array with the value', () => {
copy = util.pushArray( null, 1 );
expect( copy ).to.deep.equal( [ 1 ] );
copy = util.pushArray( true, 1 );
expect( copy ).to.deep.equal( [ 1 ] );
});
});
describe( '#removeArrayIndex()', () => {
it( 'should create a copy of the array without the specified index (if in bounds)', () => {
value = [ 1, 2, 3 ];
copy = util.removeArrayIndex( value, 1 );
expect( copy ).to.not.equal( value );
expect( copy ).to.deep.equal( [ 1, 3 ] );
});
it( 'should return the original array if index is out of bounds', () => {
value = [ 1, 2, 3 ];
copy = util.removeArrayIndex( value, 5 );
expect( copy ).to.equal( value );
expect( copy ).to.deep.equal( value );
});
it( 'should create a copy and remove the index from the end of the array with a negative index', () => {
value = [ 1, 2, 3 ];
copy = util.removeArrayIndex( value, -1 );
expect( copy ).to.not.equal( value );
expect( copy ).to.deep.equal( [ 1, 2 ] );
value = [ 1, 2, 3, 4, 5 ];
copy = util.removeArrayIndex( value, -2 );
expect( copy ).to.not.equal( value );
expect( copy ).to.deep.equal( [ 1, 2, 3, 5 ] );
});
});
describe( '#removeObjectKey()', () => {
it( 'should create a copy of the object without the specified key', () => {
value = { hello: 'world', name: 'bob' };
copy = util.removeObjectKey( value, 'hello' );
expect( copy ).to.not.equal( value );
expect( copy ).to.deep.equal( { name: 'bob' } );
});
it( 'if key parameter is missing or key is not a string return the original object', () => {
value = { hello: 'world', name: 'bob' };
copy = util.removeObjectKey( value );
expect( copy ).to.equal( value );
expect( copy ).to.deep.equal( { hello: 'world', name: 'bob' } );
value = { hello: 'world', name: 'bob' };
copy = util.removeObjectKey( value, 1 );
expect( copy ).to.equal( value );
expect( copy ).to.deep.equal( { hello: 'world', name: 'bob' } );
});
it( 'if key is not a property of object return the original object', () => {
value = { hello: 'world', name: 'bob' };
copy = util.removeObjectKey( value, 'woah' );
expect( copy ).to.equal( value );
expect( copy ).to.deep.equal( { hello: 'world', name: 'bob' } );
});
});
});