-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): Add testScheduler.run() helper
- Loading branch information
Showing
4 changed files
with
150 additions
and
5 deletions.
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
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 |
---|---|---|
|
@@ -6,9 +6,18 @@ import { TestMessage } from './TestMessage'; | |
import { SubscriptionLog } from './SubscriptionLog'; | ||
import { Subscription } from '../Subscription'; | ||
import { VirtualTimeScheduler, VirtualAction } from '../scheduler/VirtualTimeScheduler'; | ||
import { AsyncScheduler } from '../scheduler/AsyncScheduler'; | ||
|
||
const defaultMaxFrame: number = 750; | ||
|
||
interface RunHelpers { | ||
cold: typeof TestScheduler.prototype.createColdObservable; | ||
hot: typeof TestScheduler.prototype.createHotObservable; | ||
flush: typeof TestScheduler.prototype.flush; | ||
expectObservable: typeof TestScheduler.prototype.expectObservable; | ||
expectSubscriptions: typeof TestScheduler.prototype.expectSubscriptions; | ||
} | ||
|
||
interface FlushableTest { | ||
ready: boolean; | ||
actual?: any[]; | ||
|
@@ -129,10 +138,16 @@ export class TestScheduler extends VirtualTimeScheduler { | |
} | ||
|
||
super.flush(); | ||
const readyFlushTests = this.flushTests.filter(test => test.ready); | ||
while (readyFlushTests.length > 0) { | ||
const test = readyFlushTests.shift(); | ||
this.assertDeepEqual(test.actual, test.expected); | ||
const { flushTests } = this; | ||
const flushTestsCopy = flushTests.slice(); | ||
|
||
for (let i = 0, l = flushTests.length; i < l; i++) { | ||
const test = flushTestsCopy[i]; | ||
if (test.ready) { | ||
// remove it from the original array, not our copy | ||
flushTests.splice(i, 1); | ||
This comment has been minimized.
Sorry, something went wrong.
cartant
Collaborator
|
||
this.assertDeepEqual(test.actual, test.expected); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -243,4 +258,20 @@ export class TestScheduler extends VirtualTimeScheduler { | |
} | ||
return testMessages; | ||
} | ||
|
||
run<T>(callback: (helpers: RunHelpers) => T): T { | ||
AsyncScheduler.delegate = this; | ||
const helpers = { | ||
cold: this.createColdObservable.bind(this), | ||
hot: this.createHotObservable.bind(this), | ||
flush: this.flush.bind(this), | ||
expectObservable: this.expectObservable.bind(this), | ||
expectSubscriptions: this.expectSubscriptions.bind(this), | ||
}; | ||
const ret = callback(helpers); | ||
this.flush(); | ||
AsyncScheduler.delegate = undefined; | ||
|
||
return ret; | ||
} | ||
} |
In the generated code, this call to super is anotated with PURE, and the the angular cli v6 erase the call when compiling with production settings. Then, the code throws a error in runtime.