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