|
3 | 3 | describe('Promise', ifEnvSupports('Promise', function () { |
4 | 4 | var testZone = window.zone.fork(); |
5 | 5 |
|
6 | | - it('should work with .then', function (done) { |
7 | | - var resolve; |
8 | | - |
9 | | - testZone.run(function() { |
10 | | - new Promise(function (resolveFn) { |
11 | | - resolve = resolveFn; |
12 | | - }).then(function () { |
13 | | - expect(window.zone).toBeDirectChildOf(testZone); |
14 | | - done(); |
| 6 | + describe('Promise API', function () { |
| 7 | + it('should work with .then', function (done) { |
| 8 | + var resolve; |
| 9 | + |
| 10 | + testZone.run(function() { |
| 11 | + new Promise(function (resolveFn) { |
| 12 | + resolve = resolveFn; |
| 13 | + }).then(function () { |
| 14 | + expect(window.zone).toBeDirectChildOf(testZone); |
| 15 | + done(); |
| 16 | + }); |
15 | 17 | }); |
| 18 | + |
| 19 | + resolve(); |
16 | 20 | }); |
17 | 21 |
|
18 | | - resolve(); |
| 22 | + it('should work with .catch', function (done) { |
| 23 | + var reject; |
| 24 | + |
| 25 | + testZone.run(function() { |
| 26 | + new Promise(function (resolveFn, rejectFn) { |
| 27 | + reject = rejectFn; |
| 28 | + }).catch(function () { |
| 29 | + expect(window.zone).toBeDirectChildOf(testZone); |
| 30 | + done(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + reject(); |
| 35 | + }); |
19 | 36 | }); |
20 | 37 |
|
21 | | - it('should work with .catch', function (done) { |
22 | | - var reject; |
| 38 | + describe('fetch', ifEnvSupports('fetch', function () { |
| 39 | + it('should work for text response', function(done) { |
| 40 | + testZone.run(function() { |
| 41 | + fetch('/base/test/assets/sample.json').then(function(response) { |
| 42 | + var fetchZone = zone; |
| 43 | + expect(fetchZone).toBeDirectChildOf(testZone); |
23 | 44 |
|
24 | | - testZone.run(function() { |
25 | | - new Promise(function (resolveFn, rejectFn) { |
26 | | - reject = rejectFn; |
27 | | - }).catch(function () { |
28 | | - expect(window.zone).toBeDirectChildOf(testZone); |
29 | | - done(); |
| 45 | + response.text().then(function(text) { |
| 46 | + expect(zone).toBeDirectChildOf(fetchZone); |
| 47 | + expect(text.trim()).toEqual('{"hello": "world"}'); |
| 48 | + done(); |
| 49 | + }); |
| 50 | + }); |
30 | 51 | }); |
31 | 52 | }); |
32 | 53 |
|
33 | | - reject(); |
34 | | - }); |
| 54 | + it('should work for json response', function(done) { |
| 55 | + testZone.run(function() { |
| 56 | + fetch('/base/test/assets/sample.json').then(function(response) { |
| 57 | + var fetchZone = zone; |
| 58 | + expect(fetchZone).toBeDirectChildOf(testZone); |
| 59 | + |
| 60 | + response.json().then(function(obj) { |
| 61 | + expect(zone).toBeDirectChildOf(fetchZone); |
| 62 | + expect(obj.hello).toEqual('world'); |
| 63 | + done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should work for blob response', function(done) { |
| 70 | + testZone.run(function() { |
| 71 | + fetch('/base/test/assets/sample.json').then(function(response) { |
| 72 | + var fetchZone = zone; |
| 73 | + expect(fetchZone).toBeDirectChildOf(testZone); |
| 74 | + |
| 75 | + response.blob().then(function(blob) { |
| 76 | + expect(zone).toBeDirectChildOf(fetchZone); |
| 77 | + expect(blob instanceof Blob).toEqual(true); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should work for arrayBuffer response', function(done) { |
| 85 | + testZone.run(function() { |
| 86 | + fetch('/base/test/assets/sample.json').then(function(response) { |
| 87 | + var fetchZone = zone; |
| 88 | + expect(fetchZone).toBeDirectChildOf(testZone); |
| 89 | + |
| 90 | + response.arrayBuffer().then(function(blob) { |
| 91 | + expect(zone).toBeDirectChildOf(fetchZone); |
| 92 | + expect(blob instanceof ArrayBuffer).toEqual(true); |
| 93 | + done(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + })); |
35 | 99 |
|
36 | 100 | })); |
0 commit comments