Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 (function() { | |
| 6 | |
| 7 'use strict'; | |
| 8 | |
| 9 module('base'); | |
| 10 | |
| 11 test('mix(dest, src) should copy properties from |src| to |dest|', | |
| 12 function() { | |
| 13 var src = { a: 'a', b: 'b'}; | |
| 14 var dest = { c: 'c'}; | |
| 15 | |
| 16 base.mix(dest, src); | |
| 17 deepEqual(dest, {a: 'a', b: 'b', c: 'c'}); | |
| 18 }); | |
| 19 | |
| 20 test('mix(dest, src) should assert if properties are overwritten', | |
| 21 function() { | |
| 22 var src = { a: 'a', b: 'b'}; | |
| 23 var dest = { a: 'a'}; | |
| 24 | |
| 25 sinon.spy(base.debug, 'assert'); | |
| 26 | |
| 27 try { | |
| 28 base.mix(dest, src); | |
| 29 } catch (e) { | |
| 30 } finally { | |
| 31 sinon.assert.called(base.debug.assert); | |
| 32 base.debug.assert.restore(); | |
| 33 } | |
| 34 }); | |
| 35 | |
| 36 test('values(obj) should return an array containing the values of |obj|', | |
| 37 function() { | |
| 38 var output = base.values({ a: 'a', b: 'b'}); | |
| 39 | |
| 40 notEqual(output.indexOf('a'), -1, '"a" should be in the output'); | |
| 41 notEqual(output.indexOf('b'), -1, '"b" should be in the output'); | |
| 42 }); | |
| 43 | |
| 44 test('dispose(obj) should invoke the dispose method on |obj|', | |
| 45 function() { | |
| 46 var obj = { | |
| 47 dispose: sinon.spy() | |
| 48 }; | |
| 49 base.dispose(obj); | |
| 50 sinon.assert.called(obj.dispose); | |
| 51 }); | |
| 52 | |
| 53 test('dispose(obj) should not crash if |obj| is null', | |
| 54 function() { | |
| 55 expect(0); | |
| 56 base.dispose(null); | |
| 57 }); | |
| 58 | |
| 59 QUnit.asyncTest('Promise.sleep(delay) should fulfill the promise after |delay|', | |
| 60 function() { | |
| 61 base.Promise.sleep(100).then(function(){ | |
| 62 ok(true); | |
| 63 QUnit.start(); | |
| 64 }); | |
| 65 this.clock.tick(101); | |
|
Jamie
2014/07/18 02:16:04
I think this should also assert somehow that the p
| |
| 66 }); | |
| 67 | |
| 68 var source = null; | |
|
Jamie
2014/07/18 02:16:04
I think two blank lines between modules would aid
| |
| 69 var listener = null; | |
| 70 | |
| 71 module('base.EventSource', { | |
| 72 setup: function() { | |
| 73 source = new base.EventSource(); | |
| 74 source.defineEvents(['foo', 'bar']); | |
| 75 listener = sinon.spy(); | |
| 76 source.addEventListener('foo', listener); | |
| 77 }, | |
| 78 teardown: function() { | |
| 79 source = null; | |
| 80 listener = null; | |
| 81 } | |
| 82 }); | |
| 83 | |
| 84 test('raiseEvent() should invoke the listener', function() { | |
| 85 source.raiseEvent('foo'); | |
| 86 sinon.assert.called(listener); | |
| 87 }); | |
| 88 | |
| 89 test('raiseEvent() should invoke the listener with the correct event data', | |
| 90 function() { | |
| 91 var data = { | |
| 92 field: 'foo' | |
| 93 }; | |
| 94 source.raiseEvent('foo', data); | |
| 95 sinon.assert.calledWith(listener, data); | |
| 96 }); | |
| 97 | |
| 98 test( | |
| 99 'raiseEvent() should not invoke listeners that are added during raiseEvent', | |
| 100 function() { | |
| 101 source.addEventListener('foo', function() { | |
| 102 source.addEventListener('foo', function() { | |
| 103 ok(false); | |
| 104 }); | |
| 105 ok(true); | |
| 106 }); | |
| 107 source.raiseEvent('foo'); | |
| 108 }); | |
| 109 | |
| 110 test('raiseEvent() should not invoke listeners of a different event', | |
| 111 function() { | |
| 112 source.raiseEvent('bar'); | |
| 113 sinon.assert.notCalled(listener); | |
| 114 }); | |
| 115 | |
| 116 test('raiseEvent() should assert when undeclared events are raised', | |
| 117 function() { | |
| 118 sinon.spy(base.debug, 'assert'); | |
| 119 try { | |
| 120 source.raiseEvent('undefined'); | |
| 121 } catch (e) { | |
| 122 } finally { | |
| 123 sinon.assert.called(base.debug.assert); | |
| 124 base.debug.assert.restore(); | |
| 125 } | |
| 126 }); | |
| 127 | |
| 128 test( | |
| 129 'removeEventListener() should not invoke the listener in subsequent ' + | |
| 130 'calls to |raiseEvent|', | |
| 131 function() { | |
| 132 source.raiseEvent('foo'); | |
| 133 sinon.assert.calledOnce(listener); | |
| 134 | |
| 135 source.removeEventListener('foo', listener); | |
| 136 source.raiseEvent('foo'); | |
| 137 sinon.assert.calledOnce(listener); | |
| 138 }); | |
| 139 | |
| 140 test('removeEventListener() should work even if the listener ' + | |
| 141 'is removed during |raiseEvent|', | |
| 142 function() { | |
| 143 var sink = {}; | |
| 144 sink.listener = sinon.spy(function() { | |
| 145 source.removeEventListener('foo', sink.listener); | |
| 146 }); | |
| 147 | |
| 148 source.addEventListener('foo', sink.listener); | |
| 149 source.raiseEvent('foo'); | |
| 150 sinon.assert.calledOnce(sink.listener); | |
| 151 | |
| 152 source.raiseEvent('foo'); | |
| 153 sinon.assert.calledOnce(sink.listener); | |
| 154 }); | |
| 155 | |
| 156 })(); | |
| OLD | NEW |