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');
35 function finish(/** QUnit.Assert */ assert) {
36 return base.SpyPromise.settleAll().then(function() {
38 base.SpyPromise.unsettledCount, 0,
39 'base.SpyPromise.unsettledCount should be zero ' +
40 'after settleAll finishes.');
44 QUnit.test('run', function(assert) {
45 var done = assert.async();
46 assert.notEqual(base.SpyPromise, originalGlobalPromise);
47 return base.SpyPromise.run(function() {
48 assert.equal(Promise, base.SpyPromise);
49 assert.equal(base.SpyPromise.unsettledCount, 0);
50 var dummy1 = new Promise(function(resolve) { resolve(null); });
51 assert.equal(base.SpyPromise.unsettledCount, 1);
53 assert.equal(Promise, originalGlobalPromise);
54 assert.equal(base.SpyPromise.unsettledCount, 0);
59 QUnit.test('activate/restore', function(assert) {
60 assert.notEqual(base.SpyPromise, originalGlobalPromise);
61 base.SpyPromise.activate();
62 assert.notEqual(base.SpyPromise, originalGlobalPromise);
63 assert.equal(base.SpyPromise.unsettledCount, 0);
64 var dummy1 = new Promise(function(resolve) { resolve(null); });
65 assert.equal(base.SpyPromise.unsettledCount, 1);
66 base.SpyPromise.restore();
67 assert.equal(Promise, originalGlobalPromise);
68 return finish(assert);
71 QUnit.test('new/then', function(assert) {
72 var done = assert.async();
73 new base.SpyPromise(function(resolve, reject) {
75 }).then(function(/**string*/ value) {
76 assert.equal(base.SpyPromise.unsettledCount, 0);
77 assert.equal(value, 'hello');
80 assert.equal(base.SpyPromise.unsettledCount, 1);
81 return finish(assert);
84 QUnit.test('new/catch', function(assert) {
85 var done = assert.async();
86 new base.SpyPromise(function(resolve, reject) {
88 }).catch(function(/**string*/ value) {
89 assert.equal(base.SpyPromise.unsettledCount, 0);
90 assert.equal(value, 'hello');
93 assert.equal(base.SpyPromise.unsettledCount, 1);
94 return finish(assert);
97 QUnit.test('new+throw/catch', function(assert) {
98 var done = assert.async();
99 new base.SpyPromise(function(resolve, reject) {
101 }).catch(function(/**string*/ value) {
102 assert.equal(base.SpyPromise.unsettledCount, 0);
103 assert.equal(value, 'hello');
106 assert.equal(base.SpyPromise.unsettledCount, 1);
107 return finish(assert);
110 QUnit.test('resolve/then', function(assert) {
111 var done = assert.async();
112 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
113 assert.equal(base.SpyPromise.unsettledCount, 0);
114 assert.equal(value, 'hello');
117 assert.equal(base.SpyPromise.unsettledCount, 1);
118 return finish(assert);
121 QUnit.test('reject/then', function(assert) {
122 var done = assert.async();
123 base.SpyPromise.reject('hello').then(null, function(/**string*/ value) {
124 assert.equal(base.SpyPromise.unsettledCount, 0);
125 assert.equal(value, 'hello');
128 assert.equal(base.SpyPromise.unsettledCount, 1);
129 return finish(assert);
132 QUnit.test('reject/catch', function(assert) {
133 var done = assert.async();
134 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
135 assert.equal(base.SpyPromise.unsettledCount, 0);
136 assert.equal(value, 'hello');
139 assert.equal(base.SpyPromise.unsettledCount, 1);
140 return finish(assert);
143 QUnit.test('all', function(assert) {
144 var done = assert.async();
145 base.SpyPromise.all([Promise.resolve(1), Promise.resolve(2)]).
146 then(function(/**string*/ value) {
147 assert.equal(base.SpyPromise.unsettledCount, 0);
148 assert.deepEqual(value, [1, 2]);
151 assert.equal(base.SpyPromise.unsettledCount, 1);
152 return finish(assert);
155 QUnit.test('race', function(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 assert.equal(base.SpyPromise.unsettledCount, 0);
162 assert.equal(value, 'fast');
165 assert.equal(base.SpyPromise.unsettledCount, 1);
166 return finish(assert);
169 QUnit.test('resolve/then/then', function(assert) {
170 var done = assert.async();
171 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
172 assert.equal(value, 'hello');
174 }).then(function(/**string*/ value) {
175 assert.equal(value, 'goodbye');
178 return finish(assert);
182 QUnit.test('resolve/then+throw/catch', function(assert) {
183 var done = assert.async();
184 base.SpyPromise.resolve('hello').then(function(/**string*/ value) {
185 assert.equal(value, 'hello');
187 }).catch(function(/**string*/ value) {
188 assert.equal(value, 'goodbye');
191 return finish(assert);
194 QUnit.test('reject/catch/then', function(assert) {
195 var done = assert.async();
196 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
197 assert.equal(value, 'hello');
199 }).then(function(/**string*/ value) {
200 assert.equal(value, 'goodbye');
203 return finish(assert);
207 QUnit.test('reject/catch+throw/catch', function(assert) {
208 var done = assert.async();
209 base.SpyPromise.reject('hello').catch(function(/**string*/ value) {
210 assert.equal(value, 'hello');
212 }).catch(function(/**string*/ value) {
213 assert.equal(value, 'goodbye');
216 return finish(assert);
219 QUnit.test('settleAll timeout = 100', function(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 assert.ok(error instanceof Error);
225 assert.ok(startTime + 200 < Date.now());
230 QUnit.test('settleAll timeout = 500', function(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 assert.ok(startTime + 750 < Date.now());
240 QUnit.test('settleAll timeout = 1000', function(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 assert.ok(startTime + 1500 < Date.now());