Skip to content

Commit

Permalink
add tests for object outputing + avoid duplicate events
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan You committed Feb 13, 2014
1 parent f2e32ab commit 77ffd53
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
14 changes: 10 additions & 4 deletions src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ function convert (obj, key) {
unobserve(oldVal, key, observer)
values[key] = newVal
copyPaths(newVal, oldVal)
observer.emit('set', key, newVal)
// an immediate property should notify its parent
// to emit set for itself too
observer.emit('set', key, newVal, true)
observe(newVal, key, observer)
}
})
Expand Down Expand Up @@ -264,10 +266,14 @@ function observe (obj, rawPath, observer) {
get: function (key) {
observer.emit('get', path + key)
},
set: function (key, val) {
set: function (key, val, propagate) {
observer.emit('set', path + key, val)
// also notify observer that the object itself chagned
if (rawPath) observer.emit('set', rawPath, obj)
// also notify observer that the object itself changed
// but only do so when it's a immediate property. this
// avoids duplicate event firing.
if (rawPath && propagate) {
observer.emit('set', rawPath, obj, true)
}
},
mutate: function (key, val, mutation) {
// if the Array is a root value
Expand Down
17 changes: 17 additions & 0 deletions test/functional/fixtures/output-object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="test">
<p id="data">{{$data}}</p>
<p id="obj">{{test}}</p>
</div>

<script src="../../../dist/vue.js"></script>

<script>
var test = new Vue({
el: '#test',
data: {
test: {
prop: 1
}
}
})
</script>
37 changes: 37 additions & 0 deletions test/functional/specs/output-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
casper.test.begin('Outputting Objects', 8, function (test) {

casper
.start('./fixtures/output-object.html')
.then(function () {
test.assertSelectorHasText('#data', '{"test":{"prop":1}}')
test.assertSelectorHasText('#obj', '{"prop":1}')
})
// setting a nested property
.thenEvaluate(function () {
test.test.prop = 2
})
.then(function () {
test.assertSelectorHasText('#data', '{"test":{"prop":2}}')
test.assertSelectorHasText('#obj', '{"prop":2}')
})
// setting a nested object
.thenEvaluate(function () {
test.test = { hi:3 }
})
.then(function () {
test.assertSelectorHasText('#data', '{"test":{"hi":3}}')
test.assertSelectorHasText('#obj', '{"hi":3}')
})
// setting $data
.thenEvaluate(function () {
test.$data = { test: { swapped: true } }
})
.then(function () {
test.assertSelectorHasText('#data', '{"test":{"swapped":true}}')
test.assertSelectorHasText('#obj', '{"swapped":true}')
})
.run(function () {
test.done()
})

})
32 changes: 9 additions & 23 deletions test/unit/specs/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ describe('UNIT: Observer', function () {
assert.ok(obj.__observer__.values)
})

var o1 = { a: 1, b: { c: 2 } }
it('should emit set events with correct path', setTestFactory({
obj: { a: 1, b: { c: 2 } },
obj: o1,
expects: [
{ key: 'test.a', val: 1 },
{ key: 'test.b.c', val: 3 }
{ key: 'test', val: o1, skip: true },
{ key: 'test.b.c', val: 3 },
{ key: 'test.b', val: o1.b, skip: true },
{ key: 'test', val: o1, skip: true }
],
path: 'test'
}))

var o2 = { a: 1, b: { c: 2 } }
it('should emit multiple events when a nested object is set', setTestFactory({
obj: { a: 1, b: { c: 2 } },
obj: o2,
expects: [
{ key: 'test.b', val: { c: 3 } },
{ key: 'test', val: o2, skip: true },
{ key: 'test.b.c', val: 3, skip: true }
],
path: 'test'
Expand Down Expand Up @@ -433,26 +439,6 @@ describe('UNIT: Observer', function () {

})

// describe('.copyPaths()', function () {

// it('should ensure path for all paths that start with the given key', function () {
// var key = 'a',
// obj = {},
// paths = {
// 'a.b.c': 1,
// 'a.d': 2,
// 'e.f': 3,
// 'g': 4
// }
// Observer.ensurePaths(key, obj, paths)
// assert.strictEqual(obj.b.c, undefined)
// assert.strictEqual(obj.d, undefined)
// assert.notOk('f' in obj)
// assert.strictEqual(Object.keys(obj).length, 2)
// })

// })

function setTestFactory (opts) {
return function () {
var ob = new Emitter(),
Expand Down

0 comments on commit 77ffd53

Please sign in to comment.