shallow-render
Version:
Shallow rendering test utility for Angular
89 lines • 4.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reflect = void 0;
const core_1 = require("@angular/core");
const reflection = new core_1.ɵReflectionCapabilities();
const getAnnotation = (type, thing) => {
const annotations = reflection.annotations(thing);
// Try to find the nearest known Type annotation and make sure that this annotation is an
// instance of the type we are looking for, so we can use it for resolution. Note: there might
// be multiple known annotations found due to the fact that Components can extend Directives (so
// both Directive and Component annotations would be present), so we always check if the known
// annotation has the right type.
for (let i = annotations.length - 1; i >= 0; i--) {
const annotation = annotations[i];
const isKnownType = annotation instanceof core_1.Directive ||
annotation instanceof core_1.Component ||
annotation instanceof core_1.Pipe ||
annotation instanceof core_1.NgModule;
if (isKnownType) {
return annotation instanceof type ? annotation : null;
}
}
return null;
};
const resolveDirectiveInputsAndOutputs = (componentOrDirective) => {
const metadata = exports.reflect.resolveDirective(componentOrDirective);
const interatorMap = [
{ key: 'inputs', type: core_1.Input },
{ key: 'outputs', type: core_1.Output },
];
return interatorMap.reduce((acc, { key, type }) => {
var _a;
const normalized = (_a = metadata[key]) === null || _a === void 0 ? void 0 : _a.map(a => (typeof a === 'string' ? { name: a } : a));
return Object.assign(Object.assign({}, acc), normalized === null || normalized === void 0 ? void 0 : normalized.reduce((acc2, i) => (Object.assign(Object.assign({}, acc2), { [i.name]: [{ type }] })), {}));
}, {});
};
exports.reflect = {
resolveComponent: (thing) => getAnnotation(core_1.Component, thing) || {},
resolveDirective: (thing) => getAnnotation(core_1.Directive, thing) || {},
resolveModule: (thing) => getAnnotation(core_1.NgModule, thing) || {},
resolvePipe: (thing) => getAnnotation(core_1.Pipe, thing),
isComponent: (thing) => !!getAnnotation(core_1.Component, thing),
isDirective: (thing) => !!getAnnotation(core_1.Directive, thing),
isNgModule: (thing) => !!getAnnotation(core_1.NgModule, thing),
isPipe: (thing) => !!getAnnotation(core_1.Pipe, thing),
isStandalone: (thing) => {
var _a, _b;
const directiveResult = getAnnotation(core_1.Directive, thing);
if (directiveResult) {
return (_a = directiveResult.standalone) !== null && _a !== void 0 ? _a : true;
}
const pipeResult = getAnnotation(core_1.Pipe, thing);
if (pipeResult) {
return (_b = pipeResult.standalone) !== null && _b !== void 0 ? _b : true;
}
return false;
},
getInputsAndOutputs(componentOrDirective) {
let propDecorators = {};
let currentComponent = componentOrDirective;
// Walk up the prototype tree to find inherited in/outputs
do
propDecorators = Object.assign(Object.assign(Object.assign({}, currentComponent.propDecorators), resolveDirectiveInputsAndOutputs(currentComponent)), propDecorators);
while ((currentComponent = Object.getPrototypeOf(currentComponent)) && typeof currentComponent === 'function');
const getAlias = (input, key) => {
var _a;
const firstArg = (_a = input.args) === null || _a === void 0 ? void 0 : _a[0];
if (!firstArg) {
return key;
}
if (typeof firstArg === 'string') {
return firstArg;
}
return (firstArg === null || firstArg === void 0 ? void 0 : firstArg.alias) || key;
};
return Object.entries(propDecorators).reduce((acc, [key, value]) => {
const input = value.find(v => v.type === core_1.Input);
if (input) {
return Object.assign(Object.assign({}, acc), { inputs: [...acc.inputs, { propertyName: key, alias: getAlias(input, key) }] });
}
const output = value.find(v => v.type === core_1.Output);
if (output) {
return Object.assign(Object.assign({}, acc), { outputs: [...acc.outputs, { propertyName: key, alias: getAlias(output, key) }] });
}
return acc;
}, { inputs: [], outputs: [] });
},
};
//# sourceMappingURL=reflect.js.map