-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(facade/lang): add ES6 Array ponyfills
- Loading branch information
Showing
3 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import {expect} from 'chai'; | ||
import { | ||
resolveDirectiveNameFromSelector, | ||
assign, | ||
stringify, | ||
hasCtorInjectables, | ||
firstToLowerCase, | ||
firstToUpperCase, | ||
find, | ||
findIndex | ||
} from '../../src/facade/lang'; | ||
|
||
describe.only( `facade`, ()=> { | ||
|
||
describe( 'makeSelector', ()=> { | ||
|
||
it( 'should accept element selector and create camelCase from it', ()=> { | ||
|
||
const selector = 'hello-world'; | ||
expect( resolveDirectiveNameFromSelector( selector ) ).to.equal( 'helloWorld' ); | ||
|
||
} ); | ||
it( 'should accept attribute selector and create camelCase from it', ()=> { | ||
|
||
const selector = '[im-your-father]'; | ||
expect( resolveDirectiveNameFromSelector( selector ) ).to.equal( 'imYourFather' ); | ||
|
||
} ); | ||
it( 'should throw error when not valid element or attribute selector provided', ()=> { | ||
|
||
let selector = 'yabba daba'; | ||
|
||
expect( ()=>resolveDirectiveNameFromSelector( selector ) ).to | ||
.throw( 'Only selectors matching element names or base attributes are supported, got: yabba daba' ); | ||
|
||
} ); | ||
|
||
} ); | ||
describe( `assign`, ()=> { | ||
|
||
it( `should extend object exactly as Object.assign`, ()=> { | ||
|
||
const one = {foo:'yay'}; | ||
const two = {foo:'nay',boo:'low'}; | ||
|
||
const actual = assign(one,two); | ||
const expected = {foo:'nay',boo:'low'}; | ||
|
||
expect( actual ).to.deep.equal( expected ); | ||
expect( assign( {}, { one: 1 }, { two: 2 } ) ).to.deep.equal( { one: 1, two: 2 } ); | ||
|
||
} ); | ||
|
||
} ); | ||
|
||
describe( 'stringify', ()=> { | ||
|
||
it( 'should return name property if it exist on provided type', ()=> { | ||
|
||
function foo() {} | ||
|
||
function boo() { | ||
return 'hello'; | ||
} | ||
|
||
class Moo {} | ||
|
||
expect( stringify( foo ) ).to.equal( 'foo' ); | ||
expect( stringify( boo ) ).to.equal( 'boo' ); | ||
expect( stringify( Moo ) ).to.equal( 'Moo' ); | ||
|
||
} ); | ||
|
||
it( 'should return first line string of function definition if the function is anonymous', ()=> { | ||
|
||
let anonFn = function () {}; | ||
let anonFnMultiLine = function () { | ||
console.log( 'yoo' ); | ||
return null; | ||
}; | ||
|
||
expect( stringify(anonFn) ).to.equal( 'function () { }' ); | ||
expect( stringify(anonFnMultiLine) ).to.equal( 'function () {' ); | ||
|
||
} ); | ||
|
||
it( `should return string of provided type if it isn't a function`, ()=> { | ||
|
||
const obj = { hello: 'world' }; | ||
|
||
expect( stringify( 'hello' ) ).to.equal( 'hello' ); | ||
expect( stringify( null ) ).to.equal( 'null' ); | ||
expect( stringify( undefined ) ).to.equal( 'undefined' ); | ||
expect( stringify( [ 1, 2 ] ) ).to.equal( `1,2` ); | ||
expect( stringify( obj ) ).to.equal( '[object Object]' ); | ||
|
||
} ); | ||
|
||
} ); | ||
|
||
describe( 'hasInjectables', ()=> { | ||
|
||
it( 'should check if Type has $inject as array and its not empty', ()=> { | ||
|
||
class Moo { | ||
static $inject = [ 'hello' ]; | ||
} | ||
class NoMames { | ||
static $inject = []; | ||
} | ||
const obj = { $inject: null }; | ||
const name = 'jabba'; | ||
|
||
expect( hasCtorInjectables( Moo ) ).to.equal( true ); | ||
expect( hasCtorInjectables( NoMames ) ).to.equal( false ); | ||
expect( hasCtorInjectables( obj ) ).to.equal( false ); | ||
expect( hasCtorInjectables( name ) ).to.equal( false ); | ||
|
||
} ); | ||
|
||
|
||
} ); | ||
|
||
describe( 'firstLowerCase', function () { | ||
|
||
it( 'should return string with first char lowercase', ()=> { | ||
|
||
expect( firstToLowerCase( 'JediMaster' ) ).to.equal( 'jediMaster' ); | ||
|
||
} ); | ||
|
||
} ); | ||
describe( 'firstUpperCase', function () { | ||
|
||
it( 'should return string with first char uppercase', ()=> { | ||
|
||
expect( firstToUpperCase( 'jediMaster' ) ).to.equal( 'JediMaster' ); | ||
|
||
} ); | ||
|
||
} ); | ||
|
||
describe( `ES6 Array ponyfills`, ()=> { | ||
|
||
it( `should find array item or undefined if not found`, ()=> { | ||
|
||
let arr:any[] = [ 1, 2, 3, 4, 5 ]; | ||
let found = find( arr, ( el )=> el === 2 ); | ||
expect( found ).equal( 2 ); | ||
|
||
arr = [{name: 'adam'}, {name: 'eve'}, {name: 'john'}]; | ||
found = find(arr, (el)=>el.name === 'eve'); | ||
expect(found).to.deep.equal({name: 'eve'}); | ||
|
||
} ); | ||
it( `should find array item position or -1 if not found`, ()=> { | ||
|
||
const arr = [10, 20, 30, 40]; | ||
|
||
expect( findIndex( arr, ( x )=>x === 30 ) ).to.equal( 2 ); | ||
expect( findIndex( arr, ( x )=>x === 'noop' ) ).to.equal( -1 ); | ||
|
||
} ); | ||
|
||
} ); | ||
|
||
} ); |