Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat(zonespec): add a spec for synchronous tests
Browse files Browse the repository at this point in the history
Will throw an exception if any asynchornous microtask or macrotask is scheduled. Allows event tasks to be scheduled.
  • Loading branch information
vikerman authored and mhevery committed Mar 23, 2016
1 parent f3f8628 commit 0a6a434
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
7 changes: 6 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
]);


Expand Down
29 changes: 29 additions & 0 deletions lib/zone-spec/sync-test.ts
Original file line number Diff line number Diff line change
@@ -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;
})();
1 change: 1 addition & 0 deletions test/browser_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
47 changes: 47 additions & 0 deletions test/sync-test.spec.ts
Original file line number Diff line number Diff line change
@@ -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__;

0 comments on commit 0a6a434

Please sign in to comment.