From 16be7f916342dae6b2f5fb6c5dcaa411dffe8fe4 Mon Sep 17 00:00:00 2001 From: Marc Laval Date: Thu, 22 Sep 2016 16:48:48 +0200 Subject: [PATCH] fix: make fetch promise patching safe Closes #451 --- karma.conf.js | 1 + lib/zone.ts | 2 +- package.json | 1 + test/common/Promise.spec.ts | 33 ++++++++++++++++++++++----------- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index ba6cb3ad1..69efa8d99 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -6,6 +6,7 @@ module.exports = function (config) { files: [ 'node_modules/systemjs/dist/system-polyfills.js', 'node_modules/systemjs/dist/system.src.js', + 'node_modules/whatwg-fetch/fetch.js', {pattern: 'test/assets/**/*.*', watched: true, served: true, included: false}, {pattern: 'build/**/*.js.map', watched: true, served: true, included: false}, {pattern: 'build/**/*.js', watched: true, served: true, included: false}, diff --git a/lib/zone.ts b/lib/zone.ts index 66bfbffe5..03e4b2385 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -1155,7 +1155,7 @@ const Zone: ZoneType = (function(global: any) { } // ignore output to prevent error; fetchPromise.then(() => null, () => null); - if (fetchPromise.constructor != NativePromise) { + if (fetchPromise.constructor != NativePromise && fetchPromise.constructor != ZoneAwarePromise) { patchThen(fetchPromise.constructor); } } diff --git a/package.json b/package.json index 250c2c8e3..1004667d0 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@types/systemjs": "^0.19.30", "concurrently": "^2.2.0", "es6-promise": "^3.0.2", + "whatwg-fetch": "^1.0.0", "gulp": "^3.8.11", "gulp-rename": "^1.2.2", "gulp-rollup": "^2.3.0", diff --git a/test/common/Promise.spec.ts b/test/common/Promise.spec.ts index 4b41cf685..6a3ece99d 100644 --- a/test/common/Promise.spec.ts +++ b/test/common/Promise.spec.ts @@ -341,17 +341,22 @@ describe('Promise', ifEnvSupports('Promise', function () { }); }); - it('should work for blob response', function(done) { - testZone.run(function() { - global['fetch']('/base/test/assets/sample.json').then(function(response: any) { + it('should work for blob response', function (done) { + testZone.run(function () { + global['fetch']('/base/test/assets/sample.json').then(function (response:any) { var fetchZone = Zone.current; expect(fetchZone).toBe(testZone); - response.blob().then(function(blob) { - expect(Zone.current).toBe(fetchZone); - expect(blob instanceof Blob).toEqual(true); + // Android 4.3- doesn't support response.blob() + if (response.blob) { + response.blob().then(function (blob) { + expect(Zone.current).toBe(fetchZone); + expect(blob instanceof Blob).toEqual(true); + done(); + }); + } else { done(); - }); + } }); }); }); @@ -362,13 +367,19 @@ describe('Promise', ifEnvSupports('Promise', function () { var fetchZone = Zone.current; expect(fetchZone).toBe(testZone); - response.arrayBuffer().then(function(blob) { - expect(Zone.current).toBe(fetchZone); - expect(blob instanceof ArrayBuffer).toEqual(true); + // Android 4.3- doesn't support response.arrayBuffer() + if (response.arrayBuffer) { + response.arrayBuffer().then(function (blob) { + expect(Zone.current).toBe(fetchZone); + expect(blob instanceof ArrayBuffer).toEqual(true); + done(); + }); + } else { done(); - }); + } }); }); }); + })); }));