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

feat(zonespec): add a spec for synchronous tests #294

Merged
merged 1 commit into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(zonespec): add a spec for synchronous tests
Will throw an exception if any asynchornous microtask or macrotask is scheduled. Allows event tasks to be scheduled.
  • Loading branch information
vikerman committed Mar 22, 2016
commit 27aedfefb16f4f9a1668ebb966fd79c5943bd6e1
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__;