Skip to content

Commit 7888d05

Browse files
committed
Tests
- unit tests for Observer: - unobserve() - ensurePath() - ensurePaths() - Multiple observers - unit test for sd-model: - should only lock update if it has no filters - functional test for sd-model in validation.js: - should remember and restore selection position when it has filters - functional test for Observer.ensurePaths(): - should allow setting a nested scope object to {}
1 parent 3eba564 commit 7888d05

File tree

6 files changed

+139
-4
lines changed

6 files changed

+139
-4
lines changed

test/functional/fixtures/nested-props.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ <h3>Computed property that concats the two: <span sd-text="d"></span></h3>
1717
<form id="form"><input name="msg" sd-model="msg"></form>
1818
<p class="hidden">{{sum}}</p>
1919
<button class="four" sd-on="click:four">four</button>
20-
<button class="five" sd-on="click:setEmpty">empty</button>
20+
<button class="empty1" sd-on="click:setEmpty">empty a.b</button>
21+
<button class="empty2" sd-on="click:setEmpty2">empty a</button>
2122
</div>
2223
<script>
2324
Seed.config({debug: true})
@@ -66,6 +67,9 @@ <h3>Computed property that concats the two: <span sd-text="d"></span></h3>
6667
},
6768
setEmpty: function () {
6869
this.a.b = {}
70+
},
71+
setEmpty2: function () {
72+
this.a = {}
6973
}
7074
}
7175
})

test/functional/fixtures/validation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</head>
1414
<body>
1515
<div id="test">
16-
email: <input type="text" sd-model="a | email" sd-class="valid:validation.email">
16+
email: <input type="text" sd-model="a | email" sd-class="valid:validation.email" name="email">
1717
</div>
1818
<script>
1919
var RE = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

test/functional/specs/nested-props.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
casper.test.begin('Nested Properties', 15, function (test) {
1+
casper.test.begin('Nested Properties', 20, function (test) {
22

33
casper
44
.start('./fixtures/nested-props.html', function () {
@@ -35,6 +35,16 @@ casper.test.begin('Nested Properties', 15, function (test) {
3535
this.click('.four')
3636
test.assertSelectorHasText('.hidden', '4')
3737

38+
// set a nested object to {}
39+
this.click('.empty1')
40+
test.assertSelectorHasText('h1 span', '')
41+
test.assertSelectorHasText('h3 span', 'Oh yeah 3')
42+
43+
this.click('.empty2')
44+
test.assertSelectorHasText('h1 span', '')
45+
test.assertSelectorHasText('h2 span', '')
46+
test.assertSelectorHasText('h3 span', 'Oh yeah ')
47+
3848
})
3949
.run(function () {
4050
test.done()

test/functional/specs/validation.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
casper.test.begin('Validation', 2, function (test) {
1+
casper.test.begin('Validation', 4, function (test) {
22

33
casper
44
.start('./fixtures/validation.html', function () {
55
test.assertElementCount('.valid', 0)
66
this.sendKeys('input', '@hello.com')
77
test.assertElementCount('.valid', 1)
8+
9+
this.evaluate(function () {
10+
document.querySelector('input').setSelectionRange(4,4)
11+
})
12+
13+
this.sendKeys('input', 'hoho')
14+
test.assertElementCount('.valid', 1)
15+
test.assertField('email', '[email protected]')
816
})
917
.run(function () {
1018
test.done()

test/unit/specs/directives.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ describe('UNIT: Directives', function () {
359359
var triggered = false
360360
dir.key = 'foo'
361361
dir.vm = { $set: function (key, val) {
362+
assert.ok(dir.lock, 'the directive should be locked if it has no filters')
362363
assert.strictEqual(key, 'foo')
363364
assert.strictEqual(val, 'bar')
364365
triggered = true
@@ -378,6 +379,20 @@ describe('UNIT: Directives', function () {
378379
assert.ok(removed)
379380
})
380381

382+
it('should not lock during vm.$set if it has filters', function () {
383+
var triggered = false
384+
var dir = mockDirective('model', 'input', 'text')
385+
dir.filters = []
386+
dir.bind()
387+
dir.vm = {$set:function () {
388+
assert.notOk(dir.lock)
389+
triggered = true
390+
}}
391+
dir.el.value = 'foo'
392+
dir.el.dispatchEvent(mockHTMLEvent('input'))
393+
assert.ok(triggered)
394+
})
395+
381396
after(function () {
382397
document.body.removeChild(dir.el)
383398
})

test/unit/specs/observer.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,104 @@ describe('UNIT: Observer', function () {
302302

303303
})
304304

305+
describe('Multiple observers', function () {
306+
307+
var ob1 = new Emitter(),
308+
ob2 = new Emitter(),
309+
obj = {a:1}
310+
ob1.proxies = {}
311+
ob2.proxies = {}
312+
Observer.observe(obj, 'test', ob1)
313+
Observer.observe(obj, 'test', ob2)
314+
315+
var ob1Called = false,
316+
ob2Called = false
317+
318+
ob1.on('set', function () {
319+
ob1Called = true
320+
})
321+
ob2.on('set', function () {
322+
ob2Called = true
323+
})
324+
325+
it('should trigger events for multiple observers observing the same object', function () {
326+
obj.a = 2
327+
assert.ok(ob1Called)
328+
assert.ok(ob2Called)
329+
})
330+
331+
})
332+
333+
describe('.unobserve()', function () {
334+
335+
var ob1 = new Emitter(),
336+
ob2 = new Emitter(),
337+
obj = {a:1}
338+
ob1.proxies = {}
339+
ob2.proxies = {}
340+
Observer.observe(obj, 'test', ob1)
341+
Observer.observe(obj, 'test', ob2)
342+
Observer.unobserve(obj, 'test', ob1)
343+
344+
var ob1Called = false
345+
ob1.on('set', function () {
346+
ob1Called = true
347+
})
348+
var ob2Called = false
349+
ob2.on('set', function () {
350+
ob2Called = true
351+
})
352+
353+
it('should set observer proxies path to null', function () {
354+
assert.strictEqual(ob1.proxies['test.'], null)
355+
})
356+
357+
it('should turn off corresponding event listeners', function () {
358+
var callbacks = obj.__observer__._callbacks
359+
for (var e in callbacks) {
360+
assert.strictEqual(callbacks[e].length, 1)
361+
}
362+
})
363+
364+
it('should no longer emit events', function () {
365+
obj.a = 2
366+
assert.notOk(ob1Called)
367+
assert.ok(ob2Called)
368+
})
369+
370+
})
371+
372+
describe('.ensurePath()', function () {
373+
374+
it('should ensure a path can be accessed without error', function () {
375+
var obj = {},
376+
path = 'a.b.c'
377+
Observer.ensurePath(obj, path)
378+
assert.strictEqual(obj.a.b.c, undefined)
379+
})
380+
381+
})
382+
383+
describe('.ensurePaths()', function () {
384+
385+
it('should ensure path for all paths that start with the given key', function () {
386+
var key = 'a',
387+
obj = {},
388+
paths = {
389+
'a.b.c': 1,
390+
'a.d': 2,
391+
'e.f': 3,
392+
'g': 4
393+
}
394+
Observer.ensurePaths(key, obj, paths)
395+
assert.strictEqual(obj.b.c, undefined)
396+
assert.strictEqual(obj.d, undefined)
397+
assert.notOk('f' in obj)
398+
assert.strictEqual(Object.keys(obj).length, 2)
399+
})
400+
401+
})
402+
305403
function setTestFactory (opts) {
306404
return function () {
307405
var ob = new Emitter(),

0 commit comments

Comments
 (0)