This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__; |