Allow users to set a default spy strategy

This commit is contained in:
Elliot Nelson
2019-06-02 09:05:45 -04:00
parent 0644731680
commit 96786c793f
5 changed files with 110 additions and 4 deletions

View File

@@ -208,6 +208,13 @@ getJasmineRequireObj().Env = function(j$) {
}
});
this.setDefaultSpyStrategy = function(defaultStrategyFn) {
if (!currentRunnable()) {
throw new Error('Default spy strategy must be set in a before function or a spec');
}
runnableResources[currentRunnable().id].defaultStrategyFn = defaultStrategyFn;
};
this.addSpyStrategy = function(name, fn) {
if (!currentRunnable()) {
throw new Error(
@@ -286,7 +293,8 @@ getJasmineRequireObj().Env = function(j$) {
spies: [],
customEqualityTesters: [],
customMatchers: {},
customSpyStrategies: {}
customSpyStrategies: {},
defaultStrategyFn: undefined
};
if (runnableResources[parentRunnableId]) {
@@ -296,6 +304,7 @@ getJasmineRequireObj().Env = function(j$) {
resources.customMatchers = j$.util.clone(
runnableResources[parentRunnableId].customMatchers
);
resources.defaultStrategyFn = runnableResources[parentRunnableId].defaultStrategyFn;
}
runnableResources[id] = resources;
@@ -720,6 +729,15 @@ getJasmineRequireObj().Env = function(j$) {
return {};
},
function getDefaultStrategyFn() {
var runnable = currentRunnable();
if (runnable) {
return runnableResources[runnable.id].defaultStrategyFn;
}
return undefined;
},
function getPromise() {
return customPromise || global.Promise;
}