vue2/test/unit/specs/viewmodel.js

230 lines
6.5 KiB
JavaScript
Raw Normal View History

2013-09-10 06:04:33 +08:00
/*
* Only tests the following:
* - .$get()
* - .$set()
* - .$watch()
* - .$unwatch()
*/
describe('UNIT: ViewModel', function () {
2013-09-11 06:57:47 +08:00
mock('vm-test', '{{a.b.c}}')
var data = {
b: {
c: 12345
}
},
arr = [1, 2, 3],
2013-10-12 06:13:57 +08:00
vm = new Seed({
2013-10-03 06:14:29 +08:00
el: '#vm-test',
2013-10-12 06:24:53 +08:00
scope: {
2013-09-11 06:57:47 +08:00
a: data,
b: arr
}
})
describe('.$get()', function () {
it('should retrieve correct value', function () {
assert.strictEqual(vm.$get('a.b.c'), data.b.c)
})
})
describe('.$set()', function () {
vm.$set('a.b.c', 54321)
it('should set correct value', function () {
assert.strictEqual(data.b.c, 54321)
})
})
describe('.$watch()', function () {
it('should trigger callback when a plain value changes', function () {
var val
vm.$watch('a.b.c', function (newVal) {
val = newVal
2013-09-11 06:57:47 +08:00
})
data.b.c = 'new value!'
assert.strictEqual(val, data.b.c)
})
it('should trigger callback when an object value changes', function () {
var val, subVal, rootVal,
target = { c: 'hohoho' }
vm.$watch('a.b', function (newVal) {
val = newVal
})
vm.$watch('a.b.c', function (newVal) {
subVal = newVal
})
vm.$watch('a', function (newVal) {
rootVal = newVal
})
data.b = target
assert.strictEqual(val, target)
assert.strictEqual(subVal, target.c)
vm.a = 'hehehe'
assert.strictEqual(rootVal, 'hehehe')
})
it('should trigger callback when an array mutates', function () {
var val, mut
vm.$watch('b', function (array, mutation) {
val = array
mut = mutation
})
arr.push(4)
assert.strictEqual(val, arr)
assert.strictEqual(mut.method, 'push')
assert.strictEqual(mut.args.length, 1)
assert.strictEqual(mut.args[0], 4)
})
})
describe('.$unwatch()', function () {
it('should unwatch the stuff', function () {
var triggered = false
vm.$watch('a.b.c', function () {
2013-09-11 06:57:47 +08:00
triggered = true
})
vm.$watch('a', function () {
2013-09-11 06:57:47 +08:00
triggered = true
})
vm.$watch('b', function () {
2013-09-11 06:57:47 +08:00
triggered = true
})
vm.$unwatch('a')
vm.$unwatch('b')
vm.$unwatch('a.b.c')
vm.a = { b: { c:123123 }}
vm.b.push(5)
assert.notOk(triggered)
})
})
2013-10-09 11:19:12 +08:00
describe('.$on', function () {
it('should register listener on vm\'s compiler\'s emitter', function () {
2013-10-12 06:13:57 +08:00
var t = new Seed(),
2013-10-09 11:19:12 +08:00
triggered = false,
msg = 'on test'
t.$on('test', function (m) {
assert.strictEqual(m, msg)
triggered = true
})
t.$compiler.emitter.emit('test', msg)
assert.ok(triggered)
})
})
2013-10-18 00:19:58 +08:00
describe('.$once', function () {
it('should invoke the listener only once', function () {
var t = new Seed(),
triggered = 0,
msg = 'on once'
t.$once('test', function (m) {
assert.strictEqual(m, msg)
triggered++
})
t.$compiler.emitter.emit('test', msg)
t.$compiler.emitter.emit('test', msg)
assert.strictEqual(triggered, 1)
})
})
2013-10-09 11:19:12 +08:00
describe('$off', function () {
it('should turn off the listener', function () {
2013-10-12 06:13:57 +08:00
var t = new Seed(),
2013-10-09 11:19:12 +08:00
triggered1 = false,
triggered2 = false,
f1 = function () {
triggered1 = true
},
f2 = function () {
triggered2 = true
}
t.$on('test', f1)
t.$on('test', f2)
t.$off('test', f1)
t.$compiler.emitter.emit('test')
assert.notOk(triggered1)
assert.ok(triggered2)
})
})
2013-10-09 11:19:12 +08:00
describe('.$broadcast()', function () {
it('should notify all child VMs', function () {
var triggered = 0,
msg = 'broadcast test'
2013-10-12 06:13:57 +08:00
var Child = Seed.extend({
2013-10-09 11:19:12 +08:00
init: function () {
this.$on('hello', function (m) {
assert.strictEqual(m, msg)
triggered++
})
}
})
2013-10-12 06:13:57 +08:00
var Test = Seed.extend({
2013-10-09 11:19:12 +08:00
template: '<div sd-viewmodel="test"></div><div sd-viewmodel="test"></div>',
2013-10-19 13:21:49 +08:00
viewmodels: {
2013-10-09 11:19:12 +08:00
test: Child
}
})
var t = new Test()
t.$broadcast('hello', msg)
assert.strictEqual(triggered, 2)
})
})
describe('.$emit', function () {
it('should notify all ancestor VMs', function (done) {
var topTriggered = false,
midTriggered = false,
msg = 'emit test'
2013-10-12 06:13:57 +08:00
var Bottom = Seed.extend({
2013-10-09 11:19:12 +08:00
init: function () {
var self = this
setTimeout(function () {
self.$emit('hello', msg)
assert.ok(topTriggered)
assert.ok(midTriggered)
done()
}, 0)
}
})
2013-10-12 06:13:57 +08:00
var Middle = Seed.extend({
2013-10-09 11:19:12 +08:00
template: '<div sd-viewmodel="bottom"></div>',
2013-10-19 13:21:49 +08:00
viewmodels: { bottom: Bottom },
2013-10-09 11:19:12 +08:00
init: function () {
this.$on('hello', function (m) {
assert.strictEqual(m, msg)
midTriggered = true
})
}
})
2013-10-12 06:13:57 +08:00
var Top = Seed.extend({
2013-10-09 11:19:12 +08:00
template: '<div sd-viewmodel="middle"></div>',
2013-10-19 13:21:49 +08:00
viewmodels: { middle: Middle },
2013-10-09 11:19:12 +08:00
init: function () {
this.$on('hello', function (m) {
assert.strictEqual(m, msg)
topTriggered = true
})
}
})
new Top()
2013-10-09 11:19:12 +08:00
})
})
2013-09-10 06:04:33 +08:00
})