Constructor
new Trigger(wrapper, triggerFn)
- Source:
Creates a new Trigger
Example
var wrapper = new Wrapper(something);
wrapper.triggerAlways() // every time the wrapper is called...
.expectArgs("abc", 123) // ...throws error if the wrong args are not `"abc"` and `123`...
.actionReturn(true); // ...and always change the return value of the function to `true`
Parameters:
| Name | Type | Description |
|---|---|---|
wrapper |
Wrapper | The |
triggerFn |
function | The function that will be called to determine whether or not the |
Methods
(static) expectCallArgs(…args) → {Trigger|Boolean}
- Source:
Evaluates whether the arguments to a function match the ...args.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
The list of arguments to validate for the function call. |
Throws:
-
-
If called with more or less than one argument
- Type
- TypeError
-
-
-
If called by a
Triggerthat doesn't match the expected return value - Type
- ExpectError
-
Returns:
When called on a Trigger, the expectation is stored for future evaluation and the Trigger value is returned to make this chainable.
When called on a Operation, the expectation is evaluated immediately and true is returned if the expectation passed; false if it failed.
- Type
- Trigger | Boolean
(static) expectCallContext(context) → {Trigger|Boolean}
- Source:
Evaluates whether the context (this) of a function call matches the context parameter.
Parameters:
| Name | Type | Description |
|---|---|---|
context |
Object | The expected |
Throws:
-
-
If called on a wrapped function, or with more or less than one argument
- Type
- TypeError
-
-
-
If called by a
Triggerthat doesn't match the expected call context (thisvalue) - Type
- ExpectError
-
Returns:
When called on a Trigger, the expectation is stored for future evaluation and the Trigger value is returned to make this chainable.
When called on a Operation, the expectation is evaluated immediately and true is returned if the expectation passed; false if it failed.
- Type
- Trigger | Boolean
(static) expectCustom(cb) → {Trigger|Boolean}
- Source:
Evaluates the callback function
Parameters:
| Name | Type | Description |
|---|---|---|
cb |
Operation~customExpectCallback | Callback function that will determine whether the expecation passes or fails. See customExpectCallback for more details. |
Throws:
-
-
If called with more or less than one argument, or the first argument isn't a
Function - Type
- TypeError
-
-
-
If called by a
Triggerand the custom function returns anErroror aString - Type
- ExpectError
-
Returns:
When called on a Trigger, the expectation is stored for future evaluation and the Trigger value is returned to make this chainable.
When called on a Operation, the expectation is evaluated immediately and true is returned if the expectation passed; false if it failed.
- Type
- Trigger | Boolean
(static) expectException(exception) → {Trigger|Boolean}
- Source:
Expects that the function call or property set / get threw an Error that strictly matches the exception arguemnt.
Parameters:
| Name | Type | Description |
|---|---|---|
exception |
Error | null | The |
Throws:
-
-
If called with more or less than one argument, or with an argument that isn't an
Errorornull - Type
- TypeError
-
-
-
If called by a
Triggerthat doesn't throw the expected exception (Error) or throwns an unexpected exception (if the expected exception wasnull) - Type
- ExpectError
-
Returns:
When called on a Trigger, the expectation is stored for future evaluation and the Trigger value is returned to make this chainable.
When called on a Operation, the expectation is evaluated immediately and true is returned if the expectation passed; false if it failed.
- Type
- Trigger | Boolean
(static) expectReturn(retVal) → {Trigger|Boolean}
- Source:
Evaluates whether the call or get returned the value retVal.
Parameters:
| Name | Type | Description |
|---|---|---|
retVal |
any | The value that is expected to be returned from the function call or property getter. |
Throws:
-
-
If called with more or less than one argument
- Type
- TypeError
-
-
-
If called by a
Triggerthat doesn't match the expected return value - Type
- ExpectError
-
Returns:
When called on a Trigger, the expectation is stored for future evaluation and the Trigger value is returned to make this chainable.
When called on a Operation, the expectation is evaluated immediately and true is returned if the expectation passed; false if it failed.
- Type
- Trigger | Boolean
(static) expectSetVal(setVal) → {Trigger|Boolean}
- Source:
Evaluates the value that is set on a property during assignment (e.g. - obj.prop = setVal) and expects the value to
strictly equal the setVal argument.
Parameters:
| Name | Type | Description |
|---|---|---|
setVal |
any | The value that is expected to be set on the property. An |
Throws:
-
-
If called with more or less than one argument
- Type
- TypeError
-
-
-
If called by a
Triggerthat doesn't receive the expected set argument - Type
- ExpectError
-
Returns:
When called on a Trigger, the expectation is stored for future evaluation and the Trigger value is returned to make this chainable.
When called on a Operation, the expectation is evaluated immediately and true is returned if the expectation passed; false if it failed.
- Type
- Trigger | Boolean
actionCallArgs(…args) → {Trigger}
- Source:
When triggered, sets the call args to the function to the arguments specified by args
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
The arguments to be passed to the wrapped function |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionCallbackArgs(…args) → {Trigger}
- Source:
When triggered, this action sets the arguments of the callback function. If no callback function is specified, this action has no meaningful impact.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
any |
<repeatable> |
The arguments to pass to the callback function. |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionCallbackContext(context) → {Trigger}
- Source:
When triggered, this action sets the this context of the callback function. If no callback function is
specified, this action has no meaningful impact.
Parameters:
| Name | Type | Description |
|---|---|---|
context |
any | The |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionCallbackFunction(fn) → {Trigger}
- Source:
When triggered, this action will callback the function specified by fn. It may be combined with
actionCallbackContext and actionCallbackArgs
to specify the this context and arguments to pass to the callback.
Parameters:
| Name | Type | Description |
|---|---|---|
fn |
function | The function to be called as a callback. This function is called after the completion of all |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionCallbackToArg(num) → {Trigger}
- Source:
Similar to actionCallbackFunction, but treats one the arguments specified by the
index num as the callback function. It will throw an Error if the argument at num is not a function. It may be combined with
actionCallbackContext and actionCallbackArgs
to specify the this context and arguments to pass to the callback.
Parameters:
| Name | Type | Description |
|---|---|---|
num |
Number | The index into the array of arguments, where the resulting argument will be treated as a callback function.
This function is called after the completion of all |
Throws:
-
If the argument at
numis not a function. - Type
- Error
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionCallContext(args) → {Trigger}
- Source:
When triggered, sets the context (this value) of the function to the object specified by ctx
Parameters:
| Name | Type | Description |
|---|---|---|
args |
ctx | The context to be passed to the wrapped function |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionCustom(fn) → {Trigger}
- Source:
Specifies a custom action that will be run when triggered.
Parameters:
| Name | Type | Description |
|---|---|---|
fn |
Trigger~customActionCallback | The function to be run when this action is triggered. |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionReturn(retVal) → {Trigger}
- Source:
When triggered, this action will change the return value of a function call or property get to the value specified by retVal.
Parameters:
| Name | Type | Description |
|---|---|---|
retVal |
any | The value that will be returned by the function or property when this action is triggered. |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionReturnContext() → {Trigger}
- Source:
When triggered, this action sets the return value to the this value of the function.
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionReturnFromArg(num) → {Trigger}
- Source:
When triggered this action will set the return value to the value of the argumennt list specified by the
index num
Example
var anon = new Wrapper();
w.triggerAlways().actionReturnFromArg(2);
var ret = anon("I", "like", "sleeping");
console.log (ret); // "sleeping"
Parameters:
| Name | Type | Description |
|---|---|---|
num |
Number | The index of the argument list to return when the wrapper is called. As an index, this
|
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionReturnFromContext(prop) → {Trigger}
- Source:
When triggered, this action sets the return value to a property on the this value that is specified by
the prop value.
Parameters:
| Name | Type | Description |
|---|---|---|
prop |
String | The property on the |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionReturnRejectedPromise(err) → {Trigger}
- Source:
Similar to actionReturnPromise, this action causes the Wrapper to return a
Promise, but in this case the Promise is one that has been rejected (that is, failed).
Parameters:
| Name | Type | Description |
|---|---|---|
err |
Error | null | An instance of |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionReturnResolvedPromise(retValopt) → {Trigger}
- Source:
When triggered, this action causes the Wrapper to return a resolved (that is, successful) Promise. If a
retVal argument is provided, it will be the value that the Promise resolves to. If no retVal is provided,
then whatever value the Wrapper would have returned is wrapped in a promise.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
retVal |
any |
<optional> |
The optional return value to wrap. If this is not specified, the value that is returned
by the |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionSetVal(setVal) → {Trigger}
- Source:
When triggered, this action behaves as if assigning the value setVal to the property.
Parameters:
| Name | Type | Description |
|---|---|---|
setVal |
any | The value to be assigned to the property. May be |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
actionThrowException(err) → {Trigger}
- Source:
When triggered, this action throws the error specified by the err argument.
Parameters:
| Name | Type | Description |
|---|---|---|
err |
Error | null | An instance of |
Returns:
Returns this Trigger, so that further actions or expectations can be chained.
- Type
- Trigger
Type Definitions
customActionCallback(current)
- Source:
This is the callback for actionCustom. Note that actions get called every time a Trigger
executes, which will usually be twice for everytime a Wrapper is called -- once before the call, to
modify arguments and context, and once after the call to modify exceptions and return values. Use the
current.preCall and current.postCall properties to determine which half of the Wrapper call is
occuring.
Parameters:
| Name | Type | Description |
|---|---|---|
current |
Operation | The currently executing function or property set / get. |
triggerCustomCallback(curr) → {Boolean}
- Source:
This is the callback that is passed to triggerOnCustom. The most obvious thing to point out
is that it returns true when the Trigger should execute, and false when the Trigger
shouldn't execute; however, there are some unexpected behaviors with this callback that should
be observed.
First, every Trigger callback is called twice for every time the Wrapper is executed.
The callback is called once just before the Wrapper executes the wrapped function / setter / getter
providing the callback with the opportunity to evaluate arguments and context before they are used. The
callback is called again just after the wrapped function / setter / getter is called, giving the
callback the opportunity to evaluate return values and exceptions.
The first argument to the callback is curr, which is a Operation. curr has the property preCall
set to true if the callback is being called before the function / getter / setter; and has the
property postCall that is set to true if the callback is being called after the function /
getter / setter. curr also has the property curr.wrapper, which references the Wrapper.
Also note that throwing an exception in a custom trigger is not advised since it may adversely effect
the behavior of the Wrapper and the running of any subsequent Triggers. If you want the wrapper to
throw an exception, set single.exception to a new Error(); however, this is best done through an
actionThrowException anyway.
Parameters:
| Name | Type | Description |
|---|---|---|
curr |
Operation | The current function call or property set / get. |
Returns:
Returns true if the actions and expectations associated with the Trigger
should run. Returns false if this Trigger should not be executed.
- Type
- Boolean