test for computed properties and _.bind

This commit is contained in:
Evan You 2014-07-31 08:43:25 -04:00
parent f2798b6924
commit 18e50c4570
3 changed files with 69 additions and 1 deletions

View File

@ -7,7 +7,7 @@
exports.bind = function (fn, ctx) {
return function () {
fn.apply(ctx, arguments)
return fn.apply(ctx, arguments)
}
}

View File

@ -232,4 +232,62 @@ describe('Scope', function () {
})
describe('computed', function () {
var vm = new Vue({
data: {
a: 'a',
b: 'b'
},
computed: {
c: function () {
expect(this).toBe(vm)
return this.a + this.b
},
d: {
get: function () {
expect(this).toBe(vm)
return this.a + this.b
},
set: function (newVal) {
expect(this).toBe(vm)
var vals = newVal.split(' ')
this.a = vals[0]
this.b = vals[1]
}
}
}
})
it('get', function () {
expect(vm.c).toBe('ab')
expect(vm.d).toBe('ab')
})
it('set', function () {
vm.c = 123 // should do nothing
vm.d = 'c d'
expect(vm.a).toBe('c')
expect(vm.b).toBe('d')
expect(vm.c).toBe('cd')
expect(vm.d).toBe('cd')
})
it('inherit', function () {
var child = new Vue({ parent: vm })
expect(child.$scope.c).toBe('cd')
child.$scope.d = 'e f'
expect(vm.a).toBe('e')
expect(vm.b).toBe('f')
expect(vm.c).toBe('ef')
expect(vm.d).toBe('ef')
expect(child.$scope.a).toBe('e')
expect(child.$scope.b).toBe('f')
expect(child.$scope.c).toBe('ef')
expect(child.$scope.d).toBe('ef')
})
})
})

View File

@ -5,6 +5,16 @@ config.silent = true
describe('Util', function () {
describe('Language Enhancement', function () {
it('bind', function () {
var original = function (a) {
return this.a + a
}
var ctx = { a: 'ctx a ' }
var bound = _.bind(original, ctx)
var res = bound('arg a')
expect(res).toBe('ctx a arg a')
})
it('toArray', function () {
// should make a copy of original array