Explorar o código

test for utils

Evan You %!s(int64=12) %!d(string=hai) anos
pai
achega
84010129e2
Modificáronse 3 ficheiros con 101 adicións e 1 borrados
  1. 1 1
      src/utils.js
  2. 1 0
      test/unit/runner.html
  3. 99 0
      test/unit/specs/utils.js

+ 1 - 1
src/utils.js

@@ -37,6 +37,7 @@ module.exports = {
 
     /**
      *  Accurate type check
+     *  internal use only, so no need to check for NaN
      */
     typeOf: function (obj) {
         return toString.call(obj).slice(8, -1)
@@ -58,7 +59,6 @@ module.exports = {
      *  simple extend
      */
     extend: function (obj, ext, protective) {
-        if (!ext) return
         for (var key in ext) {
             if (protective && obj[key]) continue
             obj[key] = ext[key]

+ 1 - 0
test/unit/runner.html

@@ -31,6 +31,7 @@
 				return el
 			}
 		</script>
+		<script src="specs/utils.js"></script>
 		<script src="specs/binding.js"></script>
 		<script src="specs/directive.js"></script>
 		<script src="specs/observer.js"></script>

+ 99 - 0
test/unit/specs/utils.js

@@ -0,0 +1,99 @@
+var utils = require('seed/src/utils')
+
+describe('UNIT: Utils', function () {
+    
+    describe('hash', function () {
+
+        it('should return an Object with null prototype', function () {
+            var hash = utils.hash()
+            assert.strictEqual(Object.getPrototypeOf(hash), null)
+        })
+
+    })
+
+    describe('defProtected', function () {
+        
+        it('should define a protected property', function () {
+            var a = {}
+            utils.defProtected(a, 'test', 1)
+
+            var count = 0
+            for (var key in a) {
+                count++
+            }
+            assert.strictEqual(count, 0, 'inenumerable')
+            assert.strictEqual(JSON.stringify(a), '{}', 'unstringifiable')
+
+            a.test = 2
+            assert.strictEqual(a.test, 1, 'unconfigurable')
+        })
+
+        it('should take enumerable option', function () {
+            var a = {}
+            utils.defProtected(a, 'test', 1, true)
+
+            var count = 0
+            for (var key in a) {
+                count++
+            }
+            assert.strictEqual(count, 1, 'enumerable')
+            assert.strictEqual(JSON.stringify(a), '{"test":1}', 'stringifiable')
+        })
+
+    })
+
+    describe('typeOf', function () {
+        
+        it('should return correct type', function () {
+            var tof = utils.typeOf
+            assert.equal(tof({}), 'Object')
+            assert.equal(tof([]), 'Array')
+            assert.equal(tof(1), 'Number')
+            assert.equal(tof(''), 'String')
+            assert.equal(tof(true), 'Boolean')
+            assert.equal(tof(null), 'Null')
+            assert.equal(tof(undefined), 'Undefined')
+        })
+
+    })
+
+    describe('toText', function () {
+
+        var txt = utils.toText
+
+        it('should do nothing for strings and numbers', function () {
+            assert.strictEqual(txt('hihi'), 'hihi')
+            assert.strictEqual(txt(123), 123)
+        })
+        
+        it('should output empty string if value is not string or number', function () {
+            assert.strictEqual(txt({}), '')
+            assert.strictEqual(txt([]), '')
+            assert.strictEqual(txt(false), '')
+            assert.strictEqual(txt(true), '')
+            assert.strictEqual(txt(undefined), '')
+            assert.strictEqual(txt(null), '')
+            assert.strictEqual(txt(NaN), '')
+        })
+
+    })
+
+    describe('extend', function () {
+        
+        it('should extend the obj with extension obj', function () {
+            var a = {a: 1}, b = {a: {}, b: 2}
+            utils.extend(a, b)
+            assert.strictEqual(a.a, b.a)
+            assert.strictEqual(a.b, b.b)
+        })
+
+        it('should respect the protective option', function () {
+            var a = {a: 1}, b = {a: {}, b: 2}
+            utils.extend(a, b, true)
+            assert.strictEqual(a.a, 1)
+            assert.strictEqual(a.b, b.b)
+        })
+
+    })
+
+})