Ver Fonte

unit test for API (src/main.js)

Evan You há 12 anos atrás
pai
commit
00114112a1

+ 1 - 1
Gruntfile.js

@@ -71,7 +71,7 @@ module.exports = function( grunt ) {
             },
             component: {
                 files: ['src/**/*.js', 'component.json'],
-                tasks: ['component_build:dev']
+                tasks: ['component_build']
             }
         }
 

+ 25 - 0
examples/exp-handler.html

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <title></title>
+        <meta charset="utf-8">
+    </head>
+    <body>
+        <div id="demo">
+            <p sd-text="one + ' ' + two + '!'"></p>
+            <input sd-value="one"> <input sd-value="two">
+            <button sd-on="click: one = 'hohoho'"></button>
+        </div>
+        <script src="../dist/seed.js"></script>
+        <script>
+            seed.config({ debug: true })
+            var demo = new seed.ViewModel({
+                el: '#demo',
+                data: {
+                    one: 'Hello',
+                    two: 'World'
+                }
+            })
+        </script>
+    </body>
+</html>

+ 1 - 1
src/compiler.js

@@ -314,7 +314,7 @@ CompilerProto.createBinding = function (key, isExp) {
     var bindings = this.bindings,
         binding  = new Binding(this, key, isExp)
 
-    if (binding.isExp) {
+    if (isExp) {
         // a complex expression binding
         // we need to generate an anonymous computed property for it
         var result = ExpParser.parse(key)

+ 3 - 3
src/directive.js

@@ -31,9 +31,9 @@ function Directive (directiveName, expression, rawKey) {
         }
     }
 
-    this.directiveName = directiveName
-    this.expression    = expression.trim()
-    this.rawKey        = rawKey
+    this.name       = directiveName
+    this.expression = expression.trim()
+    this.rawKey     = rawKey
     
     parseKey(this, rawKey)
 

+ 6 - 0
src/directives/on.js

@@ -1,3 +1,5 @@
+var utils = require('../utils')
+
 function delegateCheck (current, top, identifier) {
     if (current[identifier]) {
         return current
@@ -26,6 +28,10 @@ module.exports = {
 
         this.unbind(true)
         if (!handler) return
+        if (typeof handler !== 'function') {
+            utils.warn('Expression is not allowed where a handler is expected.')
+            return
+        }
 
         var compiler = this.compiler,
             event    = this.arg,

+ 8 - 2
src/main.js

@@ -35,7 +35,7 @@ api.config = function (opts) {
 /*
  *  Compile a node
  */
-api.compile = function (el) {
+api.compile = function (el, opts) {
     el = typeof el === 'string'
         ? document.querySelector(el)
         : el
@@ -46,7 +46,9 @@ api.compile = function (el) {
         Ctor = utils.getVM(vmExp)
         el.removeAttribute(vmAttr)
     }
-    return new Ctor({ el: el })
+    opts = opts || {}
+    opts.el = el
+    return new Ctor(opts)
 }
 
 /*
@@ -61,6 +63,10 @@ ViewModel.extend = function (options) {
         if (options.init) {
             opts.init = options.init
         }
+        if (options.data) {
+            opts.data = opts.data || {}
+            utils.extend(opts.data, options.data)
+        }
         ViewModel.call(this, opts)
     }
     var proto = ExtendedVM.prototype = Object.create(ViewModel.prototype)

+ 12 - 0
test/e2e/runner.html

@@ -14,6 +14,18 @@
         <script>
             mocha.setup('bdd')
             var assert = chai.assert
+            function mock (id, html, opts) {
+                var el = document.createElement('div')
+                el.id = id
+                el.innerHTML = html
+                if (opts) {
+                    for (var attr in opts) {
+                        el.setAttribute(attr, opts[attr])
+                    }
+                }
+                document.getElementById('test').appendChild(el)
+                return el
+            }
         </script>
         <script src="specs/basic.js"></script>
         <script src="specs/expressions.js"></script>

+ 17 - 0
test/unit/runner.html

@@ -14,6 +14,23 @@
 		<script>
 			mocha.setup('bdd')
 			var assert = chai.assert
+
+			function mock (id, html, opts) {
+				var el = document.createElement('div')
+				el.id = id
+				el.innerHTML = html
+				if (opts) {
+					for (var attr in opts) {
+						el.setAttribute(attr, opts[attr])
+					}
+				}
+				document.getElementById('test').appendChild(el)
+				return el
+			}
+
+			function $ (selector) {
+				return document.querySelector(selector).innerHTML
+			}
 		</script>
 		<script src="specs/binding.js"></script>
 		<script src="specs/directive.js"></script>

+ 101 - 14
test/unit/specs/api.js

@@ -22,7 +22,17 @@ describe('UNIT: API', function () {
         })
 
         it('should register VM in utils if options.id exists', function () {
-            var Test = seed.ViewModel.extend({ id: 'test' }),
+            var Test = seed.ViewModel.extend({
+                    id: 'test',
+                    data: {
+                        test: 'I have a viewmodel!'
+                    },
+                    props: {
+                        hello: function () {
+                            return 'hello'
+                        }
+                    }
+                }),
                 utils = require('seed/src/utils')
             assert.strictEqual(utils.getVM('test'), Test)
         })
@@ -40,16 +50,23 @@ describe('UNIT: API', function () {
 
     describe('compile()', function () {
 
-        it('should querySelector target node if arg is a string', function () {
-            // body...
-        })
-
         it('should directly compile if arg is a node', function () {
-            // body...
+            var testId = 'compile-1'
+            mock(testId, '{{test}}')
+            var vm = seed.compile('#' + testId, { data: { test: testId } })
+            assert.ok(vm instanceof seed.ViewModel)
+            assert.strictEqual($('#' + testId), testId)
         })
 
         it('should use correct VM constructor if sd-viewmodel is present', function () {
-            // body...
+            var testId = 'compile-2'
+            mock(testId, '{{test}}', {
+                'sd-viewmodel': 'test' // see register VM test above
+            })
+            var vm = seed.compile('#' + testId)
+            assert.ok(vm instanceof seed.ViewModel)
+            assert.strictEqual(vm.hello(), 'hello', 'should inherit options.props')
+            assert.strictEqual($('#' + testId), 'I have a viewmodel!', 'should inherit options.data')
         })
 
     })
@@ -57,35 +74,105 @@ describe('UNIT: API', function () {
     describe('config()', function () {
         
         it('should work when changing prefix', function () {
-            // body...
+            var testId = 'config-1'
+            seed.config({
+                prefix: 'test'
+            })
+            mock(testId, '<span test-text="test"></span>')
+            seed.compile('#' + testId, { data: { test: testId }})
+            assert.strictEqual($('#' + testId + ' span'), testId)
         })
 
         it('should work when changing interpolate tags', function () {
-            // body...
+            var testId = 'config-2'
+            seed.config({
+                interpolateTags: {
+                    open: '<%',
+                    close: '%>'
+                }
+            })
+            mock(testId, '<% test %>')
+            seed.compile('#' + testId, { data: { test: testId }})
+            assert.strictEqual($('#' + testId), testId)
+        })
+
+        after(function () {
+            seed.config({
+                prefix: 'sd',
+                interpolateTags: {
+                    open: '{{',
+                    close: '}}'
+                }
+            })
         })
 
     })
 
     describe('filter()', function () {
+
+        var reverse = function (input) {
+            return input.split('').reverse().join('')
+        }
         
         it('should create custom filter', function () {
-            // body...
+            var testId = 'filter-1',
+                msg = '12345'
+            seed.filter('reverse', reverse)
+            mock(testId, '{{ test | reverse }}')
+            seed.compile('#' + testId, { data: { test: msg }})
+            assert.strictEqual($('#' + testId), '54321')
         })
 
         it('should return filter function if only one arg is given', function () {
-            // body...
+            var f = seed.filter('reverse')
+            assert.strictEqual(f, reverse)
         })
 
     })
 
     describe('directive()', function () {
+
+        var dirTest
         
-        it('should create custom directive', function () {
-            // body...
+        it('should create custom directive with set function only', function () {
+            var testId = 'directive-1',
+                msg = 'wowow'
+            seed.directive('test', function (value) {
+                this.el.setAttribute(testId, value + '123')
+            })
+            mock(testId, '<span sd-test="test"></span>')
+            seed.compile('#' + testId, { data: { test: msg }})
+            var el = document.querySelector('#' + testId + ' span')
+            assert.strictEqual(el.getAttribute(testId), msg + '123')
+        })
+
+        it('should create custom directive with object', function () {
+            var testId = 'directive-2',
+                msg = 'wowaaaa?'
+            dirTest = {
+                bind: function (value) {
+                    this.el.setAttribute(testId + 'bind', msg + 'bind')
+                },
+                update: function (value) {
+                    this.el.setAttribute(testId + 'update', msg + 'update')
+                },
+                unbind: function () {
+                    this.el.removeAttribute(testId + 'bind')
+                }
+            }
+            seed.directive('test2', dirTest)
+            mock(testId, '<span sd-test2="test"></span>')
+            var vm = seed.compile('#' + testId, { data: { test: msg }}),
+                el = document.querySelector('#' + testId + ' span')
+            assert.strictEqual(el.getAttribute(testId + 'bind'), msg + 'bind', 'should have called bind()')
+            assert.strictEqual(el.getAttribute(testId + 'update'), msg + 'update', 'should have called update()')
+            vm.$destroy() // assuming this works
+            assert.notOk(el.getAttribute(testId + 'bind'), 'should have called unbind()')
         })
 
         it('should return directive object/fn if only one arg is given', function () {
-            // body...
+            var dir = seed.directive('test2')
+            assert.strictEqual(dir, dirTest)
         })
 
     })

+ 2 - 0
test/unit/specs/filters.js

@@ -1,3 +1,5 @@
+var filters = require('seed/src/filters')
+
 describe('UNIT: Filters', function () {
     // body...
 })