Skip to content

Commit

Permalink
test for template parser
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 26, 2014
1 parent ed688dc commit 405b5fe
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 128 deletions.
2 changes: 1 addition & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function (grunt) {
src: 'src/**/*.js'
},
test: {
src: 'test/*/specs/*.js'
src: 'test/**/*.js'
}
},

Expand Down
1 change: 0 additions & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ p.shift = function () {
p.get = function (key, returnEntry) {
var entry = this._keymap[key]
if (entry === undefined) return
console.log('cache hit!')
if (entry === this.tail) {
return returnEntry
? entry
Expand Down
27 changes: 8 additions & 19 deletions src/parse/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,14 @@ function stringToFragment (templateString) {
node = node.lastChild
}

if (node.firstChild === node.lastChild) {
// one element
frag.appendChild(node.firstChild)
templateCache.put(templateString, frag)
return frag
} else {
// multiple nodes, return a fragment
/* jshint boss: true */
var child
while (child = node.firstChild) {
if (node.nodeType === 1) {
frag.appendChild(child)
}
var child
while (child = node.firstChild) {
if (node.nodeType === 1) {
frag.appendChild(child)
}
}
}

templateCache.put(templateString, frag)
return frag
}
Expand All @@ -101,12 +93,9 @@ function nodeToFragment (node) {
if (tag === 'TEMPLATE' && node.content) {
return node.content
}
// script tag
if (tag === 'SCRIPT') {
return stringToFragment(node.textContent)
}
// non-script node. not recommended...
return toFragment(node.outerHTML)
return tag === 'SCRIPT'
? stringToFragment(node.textContent)
: stringToFragment(node.outerHTML)
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
var config = require('./config')
var slice = [].slice
var defer =
win.requestAnimationFrame ||
win.webkitRequestAnimationFrame ||
win.setTimeout

/**
* Defer a task to the start of the next event loop
*
* @param {Function} fn
*/

var defer = typeof window === 'undefined'
? setTimeout
: (window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
setTimeout)

exports.nextTick = function (fn) {
return defer(fn, 0)
}
Expand All @@ -22,6 +23,8 @@ exports.nextTick = function (fn) {
* @param {Number} [i] - start index
*/

var slice = [].slice

exports.toArray = function (list, i) {
return slice.call(list, i || 0)
}
Expand Down
68 changes: 34 additions & 34 deletions test/unit/data_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ describe('Scope', function () {
})

it('should copy over data properties', function () {
expect(vm.$scope.a).toEqual(vm.$data.a)
expect(vm.$scope.b).toEqual(vm.$data.b)
expect(vm.$scope.a).toBe(vm.$data.a)
expect(vm.$scope.b).toBe(vm.$data.b)
})

it('should proxy these properties', function () {
expect(vm.a).toEqual(vm.$scope.a)
expect(vm.b).toEqual(vm.$scope.b)
expect(vm.a).toBe(vm.$scope.a)
expect(vm.b).toBe(vm.$scope.b)
})

it('should trigger set events', function () {
Expand All @@ -37,12 +37,12 @@ describe('Scope', function () {

// set on scope
vm.$scope.a = 2
expect(spy.callCount).toEqual(1)
expect(spy.callCount).toBe(1)
expect(spy).toHaveBeenCalledWith('a', 2, u)

// set on vm
vm.b.c = 3
expect(spy.callCount).toEqual(2)
expect(spy.callCount).toBe(2)
expect(spy).toHaveBeenCalledWith('b.c', 3, u)
})

Expand All @@ -54,12 +54,12 @@ describe('Scope', function () {

// add on scope
vm.$scope.$add('c', 123)
expect(spy.callCount).toEqual(1)
expect(spy.callCount).toBe(1)
expect(spy).toHaveBeenCalledWith('c', 123, u)

// delete on scope
vm.$scope.$delete('c')
expect(spy.callCount).toEqual(2)
expect(spy.callCount).toBe(2)
expect(spy).toHaveBeenCalledWith('c', u, u)

// vm $add/$delete are tested in the api suite
Expand All @@ -82,37 +82,37 @@ describe('Scope', function () {
})

it('should retain data reference', function () {
expect(vm.$data).toEqual(data)
expect(vm.$data).toBe(data)
})

it('should sync set', function () {
// vm -> data
vm.a = 2
expect(data.a).toEqual(2)
expect(data.a).toBe(2)
// data -> vm
data.b = {d:3}
expect(vm.$scope.b).toEqual(data.b)
expect(vm.b).toEqual(data.b)
expect(vm.$scope.b).toBe(data.b)
expect(vm.b).toBe(data.b)
})

it('should sync add', function () {
// vm -> data
vm.$scope.$add('c', 123)
expect(data.c).toEqual(123)
expect(data.c).toBe(123)
// data -> vm
data.$add('d', 456)
expect(vm.$scope.d).toEqual(456)
expect(vm.d).toEqual(456)
expect(vm.$scope.d).toBe(456)
expect(vm.d).toBe(456)
})

it('should sync delete', function () {
// vm -> data
vm.$scope.$delete('d')
expect(data.hasOwnProperty('d')).toBeFalsy()
expect(data.hasOwnProperty('d')).toBe(false)
// data -> vm
data.$delete('c')
expect(vm.$scope.hasOwnProperty('c')).toBeFalsy()
expect(vm.hasOwnProperty('c')).toBeFalsy()
expect(vm.$scope.hasOwnProperty('c')).toBe(false)
expect(vm.hasOwnProperty('c')).toBe(false)
})

})
Expand All @@ -136,8 +136,8 @@ describe('Scope', function () {
})

it('child should inherit parent data on scope', function () {
expect(child.$scope.b).toEqual(parent.b) // object
expect(child.$scope.c).toEqual(parent.c) // primitive value
expect(child.$scope.b).toBe(parent.b) // object
expect(child.$scope.c).toBe(parent.c) // primitive value
})

it('child should not ineherit data on instance', function () {
Expand All @@ -146,14 +146,14 @@ describe('Scope', function () {
})

it('child should shadow parent property with same key', function () {
expect(parent.a).toEqual('parent a')
expect(child.$scope.a).toEqual('child a')
expect(child.a).toEqual('child a')
expect(parent.a).toBe('parent a')
expect(child.$scope.a).toBe('child a')
expect(child.a).toBe('child a')
})

it('setting scope properties on child should affect parent', function () {
child.$scope.c = 'modified by child'
expect(parent.c).toEqual('modified by child')
expect(parent.c).toBe('modified by child')
})

it('events on parent should propagate down to child', function () {
Expand All @@ -162,27 +162,27 @@ describe('Scope', function () {
var spy = jasmine.createSpy('inheritance')
child._observer.on('set', spy)
parent.c = 'c changed'
expect(spy.callCount).toEqual(1)
expect(spy.callCount).toBe(1)
expect(spy).toHaveBeenCalledWith('c', 'c changed', u)

spy = jasmine.createSpy('inheritance')
child._observer.on('add', spy)
parent.$scope.$add('e', 123)
expect(spy.callCount).toEqual(1)
expect(spy.callCount).toBe(1)
expect(spy).toHaveBeenCalledWith('e', 123, u)

spy = jasmine.createSpy('inheritance')
child._observer.on('delete', spy)
parent.$scope.$delete('e')
expect(spy.callCount).toEqual(1)
expect(spy.callCount).toBe(1)
expect(spy).toHaveBeenCalledWith('e', u, u)

spy = jasmine.createSpy('inheritance')
child._observer.on('mutate', spy)
parent.arr.reverse()
expect(spy.mostRecentCall.args[0]).toEqual('arr')
expect(spy.mostRecentCall.args[1]).toEqual(parent.arr)
expect(spy.mostRecentCall.args[2].method).toEqual('reverse')
expect(spy.mostRecentCall.args[0]).toBe('arr')
expect(spy.mostRecentCall.args[1]).toBe(parent.arr)
expect(spy.mostRecentCall.args[2].method).toBe('reverse')

})

Expand All @@ -192,7 +192,7 @@ describe('Scope', function () {
var spy = jasmine.createSpy('inheritance')
child._observer.on('set', spy)
parent.a = 'a changed'
expect(spy.callCount).toEqual(0)
expect(spy.callCount).toBe(0)
})

})
Expand Down Expand Up @@ -220,12 +220,12 @@ describe('Scope', function () {
child.a = 3

// make sure data sync is working
expect(parent.arr[0].a).toEqual(3)
expect(parent.arr[0].a).toBe(3)

expect(parentSpy.callCount).toEqual(1)
expect(parentSpy.callCount).toBe(1)
expect(parentSpy).toHaveBeenCalledWith('arr.0.a', 3, u)

expect(childSpy.callCount).toEqual(2)
expect(childSpy.callCount).toBe(2)
expect(childSpy).toHaveBeenCalledWith('a', 3, u)
expect(childSpy).toHaveBeenCalledWith('arr.0.a', 3, u)
})
Expand Down
Loading

0 comments on commit 405b5fe

Please sign in to comment.