tzkmx
4/16/2015 - 11:30 PM

Init Namespace with Jasmine Tests (+extend)

Init Namespace with Jasmine Tests (+extend)

extend = (inject, template) ->
  (template[prop] = inject[prop]) for own prop of inject
  null

init_namespace = (nameString, obj, container) ->
    container ?= window
    names = nameString.split('.')
    (container[name] or= {} ;
    container = container[name]) while name = names.shift()
    extend(obj, container)

describe "Namespace DRY initializer", ->
    describe "A simple name init", ->
        it "a simple name should be available in global object", ->
            init_namespace "simple"
            expect(window.simple).toBeDefined()

    describe "A two levels nested name path", ->
        beforeEach ->
            window.simple = null
            init_namespace "simple.nested"

        it "makes available its top name in container", ->
            expect(window.simple).toBeDefined()
        it "makes available its child name in container", ->
            expect(window.simple.nested).toBeDefined()

    describe "A custom passed object", ->
        it "set its properties to ns' deepest child", ->
            window.simple = null
            init_namespace("simple.nested.ns", 
                      custom: true
                      queryable: false
                )
            ns=window.simple.nested.ns
            expect(ns.custom).toBeTruthy()
            expect(ns.queryable).toBeFalsy()