diff --git a/gulpfile.js b/gulpfile.js index d2f8a0d41..7d80e43aa 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -102,6 +102,10 @@ gulp.task('build/async-test.js', function(cb) { return generateBrowserScript('./lib/zone-spec/async-test.ts', 'async-test.js', false, cb); }); +gulp.task('build/sync-test.js', function(cb) { + return generateBrowserScript('./lib/zone-spec/sync-test.ts', 'sync-test.js', false, cb); +}); + gulp.task('build', [ 'build/zone.js', 'build/zone.js.d.ts', @@ -113,7 +117,8 @@ gulp.task('build', [ 'build/long-stack-trace-zone.min.js', 'build/wtf.js', 'build/wtf.min.js', - 'build/async-test.js' + 'build/async-test.js', + 'build/sync-test.js' ]); diff --git a/lib/zone-spec/sync-test.ts b/lib/zone-spec/sync-test.ts new file mode 100644 index 000000000..59799fa4b --- /dev/null +++ b/lib/zone-spec/sync-test.ts @@ -0,0 +1,29 @@ +(function() { + class SyncTestZoneSpec implements ZoneSpec { + runZone = Zone.current; + + constructor(namePrefix: string) { + this.name = 'syncTestZone for ' + namePrefix; + } + + // ZoneSpec implementation below. + + name: string; + + onScheduleTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): Task { + switch (task.type) { + case 'microTask': + case 'macroTask': + throw new Error(`Cannot call ${task.source} from within a sync test.`); + case 'eventTask': + task = delegate.scheduleTask(target, task); + break; + } + return task; + } + } + + // Export the class so that new instances can be created with proper + // constructor params. + Zone['SyncTestZoneSpec'] = SyncTestZoneSpec; +})(); diff --git a/test/browser_entry_point.ts b/test/browser_entry_point.ts index 064bd6060..e95be90b6 100644 --- a/test/browser_entry_point.ts +++ b/test/browser_entry_point.ts @@ -13,6 +13,7 @@ import './test-env-setup'; // List all tests here: import './long-stack-trace-zone.spec'; import './async-test.spec'; +import './sync-test.spec'; import './microtasks.spec'; import './zone.spec'; import './integration/brick.spec'; diff --git a/test/sync-test.spec.ts b/test/sync-test.spec.ts new file mode 100644 index 000000000..22871475a --- /dev/null +++ b/test/sync-test.spec.ts @@ -0,0 +1,47 @@ +import '../lib/zone-spec/sync-test'; + +describe('SyncTestZoneSpec', () => { + var SyncTestZoneSpec = Zone['SyncTestZoneSpec']; + var testZoneSpec; + var syncTestZone; + + beforeEach(() => { + testZoneSpec = new SyncTestZoneSpec('name'); + syncTestZone = Zone.current.fork(testZoneSpec); + }); + + it('should fail on Promise.then', () => { + syncTestZone.run(() => { + expect(() => { Promise.resolve().then(function() {}); }) + .toThrow(new Error("Cannot call Promise.then from within a sync test.")); + }); + }); + + it('should fail on setTimeout', () => { + syncTestZone.run(() => { + expect(() => { setTimeout(() => { }, 100); }) + .toThrow(new Error("Cannot call setTimeout from within a sync test.")); + }); + }); + + it('should work with event tasks', () => { + syncTestZone.run(() => { + var button = document.createElement('button'); + document.body.appendChild(button); + var x = 1; + try { + button.addEventListener('click', () => { x++; }); + + button.click(); + expect(x).toEqual(2); + + button.click(); + expect(x).toEqual(3); + } finally { + document.body.removeChild(button); + } + }); + }); +}); + +export var __something__;