@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
93 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearSelectorSpies = clearSelectorSpies;
exports.initializeSelectorSpies = initializeSelectorSpies;
exports.spyOnSelector = spyOnSelector;
exports.clearSelectorSpy = clearSelectorSpy;
exports.select = select;
const tslib_1 = require("tslib");
const store_1 = require("@ngrx/store");
const rxjs_1 = require("rxjs");
const typings_1 = require("./typings");
// eslint-disable-next-line import/export -- legacy code
tslib_1.__exportStar(require("@ngrx/store"), exports);
/** Global variable that holds the registered spies */
let registeredSpies = [];
/**
*
* PRIVATE METHODS
*
*/
/**
* Searches for the index of given selector
* @param selectFn Selector function to be searched
*/
const getSpyIndex = (selectFn) => registeredSpies.findIndex((spy) => spy.selector === selectFn);
/**
* Searches for the given selector spy. Returns undefined if none is registered
* @param selectFn Selector function to be searched
*/
const getSpy = (selectFn) => {
const index = getSpyIndex(selectFn);
return index >= 0 ? registeredSpies[index] : undefined;
};
/**
* Uses the spy as the return of a select
* @param spy The selector spy
*/
function useSpy(spy) {
return (0, typings_1.isSelectorSpyCall)(spy) ? spy.fakeResult : () => (0, rxjs_1.of)(spy.fakeResult);
}
/**
*
* PUBLIC METHODS
*
*/
/**
* Clears all the registered selector spies
*/
function clearSelectorSpies() {
registeredSpies = [];
}
/**
* Initialize the selector spies structure
*/
function initializeSelectorSpies() {
clearSelectorSpies();
}
/**
* Spy on a selector function
* @param selector Selector function to be spied on
* @param fakeResult The fake result to be used when a select is triggered
*/
function spyOnSelector(selector, fakeResult) {
const spyIndex = getSpyIndex(selector);
if (spyIndex >= 0) {
registeredSpies[spyIndex].fakeResult = fakeResult;
}
else {
registeredSpies.push({ selector: selector, fakeResult });
}
}
/**
* Removes the spy on a given selector function
* @param selector Selector function that is being spied on
*/
function clearSelectorSpy(selector) {
const spyIndex = getSpyIndex(selector);
if (spyIndex >= 0) {
registeredSpies.splice(spyIndex, 1);
}
}
/**
* Highjacked ngrx/store select. It checks if the selector function is being spied on
* and returns it's fake result. Otherwise, it runs the original ngrx/store select
* @param selectFn Selector function to be used
* @param props optional properties to be used by the selector
*/
// eslint-disable-next-line import/export -- legacy code
function select(selectFn, props) {
const spy = getSpy(selectFn);
return spy ? useSpy(spy) : (0, store_1.select)(selectFn, props);
}
//# sourceMappingURL=testable-select.js.map