goog.testing.MockClock
Provided By | |
---|---|
Extends | |
All Implemented Interfaces |
Class for unit testing code that uses setTimeout and clearTimeout.
NOTE: If you are using MockClock to test code that makes use of goog.fx.Animation, then you must either:
- Install and dispose of the MockClock in setUpPage() and tearDownPage() respectively (rather than setUp()/tearDown()).
or
- Ensure that every test clears the animation queue by calling
mockClock.tick(x) at the end of each test function (where
x
is large enough to complete all animations).
Otherwise, if any animation is left pending at the time that MockClock.dispose() is called, that will permanently prevent any future animations from playing on the page.
new MockClock( opt_autoInstall )
Parameters |
|
---|
Instance Methods
this.addOnDisposeCallback<T>( callback, opt_scope ) → void
void
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added. If a callback is added to an already disposed Disposable, it will be called immediately.
Defined by | |||||||||
---|---|---|---|---|---|---|---|---|---|
Parameters |
|
this.dispose() → ?
?
Disposes of the object and its resources.
Overrides | ||
---|---|---|
Specified by | ||
Parameters | None. | |
Returns |
|
this.disposeInternal() → void
void
Performs appropriate cleanup. See description of goog.disposable.IDisposable
for examples. Classes that extend goog.Disposable
should override this
method. Not reentrant. To avoid calling it twice, it must only be called from
the subclass' disposeInternal
method. Everywhere else the public dispose
method must be used. For example:
mypackage.MyClass = function() { mypackage.MyClass.base(this, 'constructor'); // Constructor logic specific to MyClass. ... }; goog.inherits(mypackage.MyClass, goog.Disposable); mypackage.MyClass.prototype.disposeInternal = function() { // Dispose logic specific to MyClass. ... // Call superclass's disposeInternal at the end of the subclass's, like // in C++, to avoid hard-to-catch issues. mypackage.MyClass.base(this, 'disposeInternal'); };
Overrides | |
---|---|
Parameters | None. |
this.doTimeWarpAsync( newDate ) → Promise
Promise
Instantly adjusts the clock's current time to a new timestamp. Unlike tick(),
this method skips over the intervening time, so that setInterval()
calls or
recurring setTimeout()
s will only run once.
This mimics the behavior of setting the system clock, rather than waiting for time to pass.
CAUTION: This is an advanced feature. Use this method to set the clock to be a specific date, which is much faster than calling tick() with a large value. This lets you test code against arbitrary dates.
MOE:begin_strip See go/mockclock-time-travel for how & why to use this method. MOE:end_strip
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
this.getCallbacksTriggered() → number
number
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.getCurrentTime() → number
number
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.getDisposed() → boolean
boolean
warning Deprecated | Use |
---|
Defined by | |||
---|---|---|---|
Parameters | None. | ||
Returns |
|
this.getTimeoutDelay() → number
number
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.getTimeoutsMade() → number
number
Parameters | None. | ||
---|---|---|---|
Returns |
|
this.install() → void
void
Installs the MockClock by overriding the global object's implementation of setTimeout, setInterval, clearTimeout and clearInterval.
this.isDisposed() → boolean
boolean
Overrides | |||
---|---|---|---|
Specified by | |||
Parameters | None. | ||
Returns |
|
this.isSynchronous() → boolean
boolean
Whether the MockClock is configured to run synchronously.
This allows MockClock consumers to decide whether to tick synchronously or asynchronously.
Parameters | None. | |
---|---|---|
Returns |
|
this.isTimeoutSet( timeoutKey ) → boolean
boolean
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
this.registerDisposable( disposable ) → void
void
Associates a disposable object with this object so that they will be disposed together.
Defined by | |||||
---|---|---|---|---|---|
Parameters |
|
this.reset( retainAsyncQueue ) → void
void
Resets the MockClock, removing all timeouts that are scheduled and resets the fake timer count.
Parameters |
|
---|
this.setTimeoutDelay( delay ) → void
void
Sets the amount of time between when a timeout is scheduled to fire and when it actually fires.
Parameters |
|
---|
this.tick( opt_millis ) → number
number
this.tickAsyncMustSettlePromise<T>( millis, promise ) → Promise<(T|null)>
Promise<(T|null)>
Asynchronously increments the MockClock's time by a given number of milliseconds, returning the settled promise value.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
| ||||||||
Throws |
|
this.tickPromise<T>( promise, opt_millis ) → T
T
Takes a promise and then ticks the mock clock. If the promise successfully resolves, returns the value produced by the promise. If the promise is rejected, it throws the rejection as an exception. If the promise is not resolved at all, throws an exception. Also ticks the general clock by the specified amount. Only works with goog.Thenable, hence goog.Promise. Does NOT work with native browser promises.
warning Deprecated | Treating Promises as synchronous values is incompatible with native promises and async functions. More generally, this code relies on promises "pumped" by setTimeout which is not done in production code, even for goog.Promise and results unnatural timing between resolved promises callback and setTimeout/setInterval callbacks in tests. |
---|
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.uninstall( resetScheduler ) → void
void
Removes the MockClock's hooks into the global object's functions and revert to their original values.
Parameters |
|
---|
this.unmockDateNow() → void
void
Unmocks the Date.now() function for tests that aren't expecting it to be mocked. See b/141619890.
Instance Properties
this.creationStack → (string|undefined)
(string|undefined)
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Static Functions
MockClock.createAsyncMockClock() → goog.testing.MockClock
goog.testing.MockClock
Creates an async-only MockClock that can only be ticked asynchronously.
Async-only MockClocks rely on native Promise resolution instead of patching async run behavior to force GoogPromise to resolve synchronously. As a result, async MockClocks must be ticked with tickAsync() instead of tick().
Async-only MockClocks will always use the default async scheduler and will never reset the async queue when uninstalled.
Parameters | None. | |
---|---|---|
Returns |
|
Static Properties
MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT → number
number
Default wait timeout for mocking requestAnimationFrame (in milliseconds).
MockClock.nextId → number
number
ID to use for next timeout. Timeout IDs must never be reused, even across MockClock instances.