| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 var originalGlobalPromise = Promise; | |
| 10 | |
| 11 QUnit.module('spy_promise', { | |
| 12 beforeEach: function(/** QUnit.Assert*/ assert) { | |
| 13 assertInitialState(assert); | |
| 14 base.SpyPromise.reset(); // Defend against broken tests. | |
| 15 }, | |
| 16 afterEach: function(/** QUnit.Assert*/ assert) { | |
| 17 assertInitialState(assert); | |
| 18 } | |
| 19 }); | |
| 20 | |
| 21 function assertInitialState(/** QUnit.Assert */ assert) { | |
| 22 assert.equal(Promise, originalGlobalPromise); | |
| 23 assert.ok( | |
| 24 !base.SpyPromise.isSettleAllRunning(), | |
| 25 'settleAll should not be running'); | |
| 26 assert.equal( | |
| 27 base.SpyPromise.unsettledCount, 0, | |
| 28 'base.SpyPromise.unsettledCount should be zero ' + | |
| 29 'before/after any test finishes'); | |
| 30 } | |
| 31 | |
| 32 /** | |
| 33 * @param {!QUnit.Assert} assert | |
| 34 * @return {!Promise} | |
| 35 */ | |
| 36 function finish(assert) { | |
| 37 return base.SpyPromise.settleAll().then(function() { | |
| 38 assert.equal( | |
| 39 base.SpyPromise.unsettledCount, 0, | |
| 40 'base.SpyPromise.unsettledCount should be zero ' + | |
| 41 'after settleAll finishes.'); | |
| 42 }); | |
| 43 } | |
| 44 | |
| 45 QUnit.test('run', function(assert) { | |
| 46 var done = assert.async(); | |
| 47 assert.notEqual(base.SpyPromise, originalGlobalPromise); | |
| 48 return base.SpyPromise.run(function() { | |
| 49 assert.equal(Promise, base.SpyPromise); | |
| 50 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 51 var dummy1 = new Promise(function(resolve) { resolve(null); }); | |
| 52 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 53 }).then(function() { | |
| 54 assert.equal(Promise, originalGlobalPromise); | |
| 55 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 56 done(); | |
| 57 }); | |
| 58 }); | |
| 59 | |
| 60 QUnit.test('activate/restore', function(assert) { | |
| 61 assert.notEqual(base.SpyPromise, originalGlobalPromise); | |
| 62 base.SpyPromise.activate(); | |
| 63 assert.notEqual(base.SpyPromise, originalGlobalPromise); | |
| 64 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 65 var dummy1 = new Promise(function(resolve) { resolve(null); }); | |
| 66 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 67 base.SpyPromise.restore(); | |
| 68 assert.equal(Promise, originalGlobalPromise); | |
| 69 return finish(assert); | |
| 70 }); | |
| 71 | |
| 72 QUnit.test('new/then', function(assert) { | |
| 73 var done = assert.async(); | |
| 74 new base.SpyPromise(function(resolve, reject) { | |
| 75 resolve('hello'); | |
| 76 }).then(function(/**string*/ value) { | |
| 77 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 78 assert.equal(value, 'hello'); | |
| 79 done(); | |
| 80 }); | |
| 81 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 82 return finish(assert); | |
| 83 }); | |
| 84 | |
| 85 QUnit.test('new/catch', function(assert) { | |
| 86 var done = assert.async(); | |
| 87 new base.SpyPromise(function(resolve, reject) { | |
| 88 reject('hello'); | |
| 89 }).catch(function(/**string*/ value) { | |
| 90 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 91 assert.equal(value, 'hello'); | |
| 92 done(); | |
| 93 }); | |
| 94 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 95 return finish(assert); | |
| 96 }); | |
| 97 | |
| 98 QUnit.test('new+throw/catch', function(assert) { | |
| 99 var done = assert.async(); | |
| 100 new base.SpyPromise(function(resolve, reject) { | |
| 101 throw 'hello'; | |
| 102 }).catch(function(/**string*/ value) { | |
| 103 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 104 assert.equal(value, 'hello'); | |
| 105 done(); | |
| 106 }); | |
| 107 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 108 return finish(assert); | |
| 109 }); | |
| 110 | |
| 111 QUnit.test('resolve/then', function(assert) { | |
| 112 var done = assert.async(); | |
| 113 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { | |
| 114 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 115 assert.equal(value, 'hello'); | |
| 116 done(); | |
| 117 }); | |
| 118 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 119 return finish(assert); | |
| 120 }); | |
| 121 | |
| 122 QUnit.test('reject/then', function(assert) { | |
| 123 var done = assert.async(); | |
| 124 base.SpyPromise.reject('hello').then(null, function(/**string*/ value) { | |
| 125 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 126 assert.equal(value, 'hello'); | |
| 127 done(); | |
| 128 }); | |
| 129 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 130 return finish(assert); | |
| 131 }); | |
| 132 | |
| 133 QUnit.test('reject/catch', function(assert) { | |
| 134 var done = assert.async(); | |
| 135 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { | |
| 136 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 137 assert.equal(value, 'hello'); | |
| 138 done(); | |
| 139 }); | |
| 140 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 141 return finish(assert); | |
| 142 }); | |
| 143 | |
| 144 QUnit.test('all', function(assert) { | |
| 145 var done = assert.async(); | |
| 146 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]). | |
| 147 then( | |
| 148 /** @param {string} value */ | |
| 149 function(value) { | |
| 150 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 151 assert.deepEqual(value, [1, 2]); | |
| 152 done(); | |
| 153 }); | |
| 154 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 155 return finish(assert); | |
| 156 }); | |
| 157 | |
| 158 QUnit.test('race', function(assert) { | |
| 159 var done = assert.async(); | |
| 160 var fast = Promise.resolve('fast'); | |
| 161 var slow = new Promise(function() {}); // never settled | |
| 162 base.SpyPromise.race([fast, slow]). | |
| 163 then(function(/**string*/ value) { | |
| 164 assert.equal(base.SpyPromise.unsettledCount, 0); | |
| 165 assert.equal(value, 'fast'); | |
| 166 done(); | |
| 167 }); | |
| 168 assert.equal(base.SpyPromise.unsettledCount, 1); | |
| 169 return finish(assert); | |
| 170 }); | |
| 171 | |
| 172 QUnit.test('resolve/then/then', function(assert) { | |
| 173 var done = assert.async(); | |
| 174 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { | |
| 175 assert.equal(value, 'hello'); | |
| 176 return 'goodbye'; | |
| 177 }).then(function(/**string*/ value) { | |
| 178 assert.equal(value, 'goodbye'); | |
| 179 done(); | |
| 180 }); | |
| 181 return finish(assert); | |
| 182 }); | |
| 183 | |
| 184 | |
| 185 QUnit.test('resolve/then+throw/catch', function(assert) { | |
| 186 var done = assert.async(); | |
| 187 base.SpyPromise.resolve('hello').then(function(/**string*/ value) { | |
| 188 assert.equal(value, 'hello'); | |
| 189 throw 'goodbye'; | |
| 190 }).catch(function(/**string*/ value) { | |
| 191 assert.equal(value, 'goodbye'); | |
| 192 done(); | |
| 193 }); | |
| 194 return finish(assert); | |
| 195 }); | |
| 196 | |
| 197 QUnit.test('reject/catch/then', function(assert) { | |
| 198 var done = assert.async(); | |
| 199 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { | |
| 200 assert.equal(value, 'hello'); | |
| 201 return 'goodbye'; | |
| 202 }).then(function(/**string*/ value) { | |
| 203 assert.equal(value, 'goodbye'); | |
| 204 done(); | |
| 205 }); | |
| 206 return finish(assert); | |
| 207 }); | |
| 208 | |
| 209 | |
| 210 QUnit.test('reject/catch+throw/catch', function(assert) { | |
| 211 var done = assert.async(); | |
| 212 base.SpyPromise.reject('hello').catch(function(/**string*/ value) { | |
| 213 assert.equal(value, 'hello'); | |
| 214 throw 'goodbye'; | |
| 215 }).catch(function(/**string*/ value) { | |
| 216 assert.equal(value, 'goodbye'); | |
| 217 done(); | |
| 218 }); | |
| 219 return finish(assert); | |
| 220 }); | |
| 221 | |
| 222 QUnit.test('settleAll timeout = 100', function(assert) { | |
| 223 var done = assert.async(); | |
| 224 var startTime = Date.now(); | |
| 225 var neverResolved = new base.SpyPromise(function() {}); | |
| 226 return base.SpyPromise.settleAll(100).catch(function(error) { | |
| 227 assert.ok(error instanceof Error); | |
| 228 assert.ok(startTime + 200 < Date.now()); | |
| 229 done(); | |
| 230 }); | |
| 231 }); | |
| 232 | |
| 233 QUnit.test('settleAll timeout = 500', function(assert) { | |
| 234 var done = assert.async(); | |
| 235 var startTime = Date.now(); | |
| 236 var neverResolved = new base.SpyPromise(function() {}); | |
| 237 return base.SpyPromise.settleAll(500).catch(function(error) { | |
| 238 assert.ok(startTime + 750 < Date.now()); | |
| 239 done(); | |
| 240 }); | |
| 241 }); | |
| 242 | |
| 243 QUnit.test('settleAll timeout = 1000', function(assert) { | |
| 244 var done = assert.async(); | |
| 245 var startTime = Date.now(); | |
| 246 var neverResolved = new base.SpyPromise(function() {}); | |
| 247 return base.SpyPromise.settleAll(1000).catch(function(error) { | |
| 248 assert.ok(startTime + 1500 < Date.now()); | |
| 249 done(); | |
| 250 }); | |
| 251 }); | |
| 252 | |
| 253 })(); | |
| OLD | NEW |