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.
9 var originalGlobalPromise = Promise;
11 QUnit.module('spy_promise', {
12 beforeEach: function(/** QUnit.Assert*/ assert) {
13 assertInitialState(assert);
14 base.SpyPromise.reset(); // Defend against broken tests.
16 afterEach: function(/** QUnit.Assert*/ assert) {
17 assertInitialState(assert);
21 function assertInitialState(/** QUnit.Assert */ assert) {
22 assert.equal(Promise, originalGlobalPromise);
24 !base.SpyPromise.isSettleAllRunning(),
25 'settleAll should not be running');
27 base.SpyPromise.unsettledCount, 0,
28 'base.SpyPromise.unsettledCount should be zero ' +
29 'before/after any test finishes');
33 * @param {!QUnit.Assert} assert
36 function finish(assert) {
37 return base.SpyPromise.settleAll().then(function() {
39 base.SpyPromise.unsettledCount, 0,
40 'base.SpyPromise.unsettledCount should be zero ' +
41 'after settleAll finishes.');
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);
54 assert.equal(Promise, originalGlobalPromise);
55 assert.equal(base.SpyPromise.unsettledCount, 0);
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);
72 QUnit.test('new/then', function(assert) {
73 var done = assert.async();
74 new base.SpyPromise(function(resolve, reject) {
76 }).then(function(/**string*/ value) {
77 assert.equal(base.SpyPromise.unsettledCount, 0);
78 assert.equal(value, 'hello');
81 assert.equal(base.SpyPromise.unsettledCount, 1);
82 return finish(assert);
85 QUnit.test('new/catch', function(assert) {
86 var done = assert.async();
87 new base.SpyPromise(function(resolve, reject) {
89 }).catch(function(/**string*/ value) {
90 assert.equal(base.SpyPromise.unsettledCount, 0);
91 assert.equal(value, 'hello');
94 assert.equal(base.SpyPromise.unsettledCount, 1);
95 return finish(assert);
98 QUnit.test('new+throw/catch', function(assert) {
99 var done = assert.async();
100 new base.SpyPromise(function(resolve, reject) {
102 }).catch(function(/**string*/ value) {
103 assert.equal(base.SpyPromise.unsettledCount, 0);
104 assert.equal(value, 'hello');
107 assert.equal(base.SpyPromise.unsettledCount, 1);
108 return finish(assert);
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');
118 assert.equal(base.SpyPromise.unsettledCount, 1);
119 return finish(assert);
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');
129 assert.equal(base.SpyPromise.unsettledCount, 1);
130 return finish(assert);
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');
140 assert.equal(base.SpyPromise.unsettledCount, 1);
141 return finish(assert);
144 QUnit.test('all', function(assert) {
145 var done = assert.async();
146 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]).
148 /** @param {string} value */
150 assert.equal(base.SpyPromise.unsettledCount, 0);
151 assert.deepEqual(value, [1, 2]);
154 assert.equal(base.SpyPromise.unsettledCount, 1);
155 return finish(assert);
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');
168 assert.equal(base.SpyPromise.unsettledCount, 1);
169 return finish(assert);
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');
177 }).then(function(/**string*/ value) {
178 assert.equal(value, 'goodbye');
181 return finish(assert);
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');
190 }).catch(function(/**string*/ value) {
191 assert.equal(value, 'goodbye');
194 return finish(assert);
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');
202 }).then(function(/**string*/ value) {
203 assert.equal(value, 'goodbye');
206 return finish(assert);
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');
215 }).catch(function(/**string*/ value) {
216 assert.equal(value, 'goodbye');
219 return finish(assert);
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());
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());
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());