package.esm2022.testing.src.defer.mjs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Angular - the core framework
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { ɵCONTAINER_HEADER_OFFSET as CONTAINER_HEADER_OFFSET, ɵDeferBlockState as DeferBlockState, ɵgetDeferBlocks as getDeferBlocks, ɵrenderDeferBlockState as renderDeferBlockState, ɵtriggerResourceLoading as triggerResourceLoading, } from '@angular/core';
/**
* Represents an individual defer block for testing purposes.
*
* @publicApi
*/
export class DeferBlockFixture {
/** @nodoc */
constructor(block, componentFixture) {
this.block = block;
this.componentFixture = componentFixture;
}
/**
* Renders the specified state of the defer fixture.
* @param state the defer state to render
*/
async render(state) {
if (!hasStateTemplate(state, this.block)) {
const stateAsString = getDeferBlockStateNameFromEnum(state);
throw new Error(`Tried to render this defer block in the \`${stateAsString}\` state, ` +
`but there was no @${stateAsString.toLowerCase()} block defined in a template.`);
}
if (state === DeferBlockState.Complete) {
await triggerResourceLoading(this.block.tDetails, this.block.lView, this.block.tNode);
}
// If the `render` method is used explicitly - skip timer-based scheduling for
// `@placeholder` and `@loading` blocks and render them immediately.
const skipTimerScheduling = true;
renderDeferBlockState(state, this.block.tNode, this.block.lContainer, skipTimerScheduling);
this.componentFixture.detectChanges();
}
/**
* Retrieves all nested child defer block fixtures
* in a given defer block.
*/
getDeferBlocks() {
const deferBlocks = [];
// An LContainer that represents a defer block has at most 1 view, which is
// located right after an LContainer header. Get a hold of that view and inspect
// it for nested defer blocks.
const deferBlockFixtures = [];
if (this.block.lContainer.length >= CONTAINER_HEADER_OFFSET) {
const lView = this.block.lContainer[CONTAINER_HEADER_OFFSET];
getDeferBlocks(lView, deferBlocks);
for (const block of deferBlocks) {
deferBlockFixtures.push(new DeferBlockFixture(block, this.componentFixture));
}
}
return Promise.resolve(deferBlockFixtures);
}
}
function hasStateTemplate(state, block) {
switch (state) {
case DeferBlockState.Placeholder:
return block.tDetails.placeholderTmplIndex !== null;
case DeferBlockState.Loading:
return block.tDetails.loadingTmplIndex !== null;
case DeferBlockState.Error:
return block.tDetails.errorTmplIndex !== null;
case DeferBlockState.Complete:
return true;
default:
return false;
}
}
function getDeferBlockStateNameFromEnum(state) {
switch (state) {
case DeferBlockState.Placeholder:
return 'Placeholder';
case DeferBlockState.Loading:
return 'Loading';
case DeferBlockState.Error:
return 'Error';
default:
return 'Main';
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVmZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy9jb3JlL3Rlc3Rpbmcvc3JjL2RlZmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVILE9BQU8sRUFDTCx3QkFBd0IsSUFBSSx1QkFBdUIsRUFFbkQsZ0JBQWdCLElBQUksZUFBZSxFQUNuQyxlQUFlLElBQUksY0FBYyxFQUNqQyxzQkFBc0IsSUFBSSxxQkFBcUIsRUFDL0MsdUJBQXVCLElBQUksc0JBQXNCLEdBQ2xELE1BQU0sZUFBZSxDQUFDO0FBSXZCOzs7O0dBSUc7QUFDSCxNQUFNLE9BQU8saUJBQWlCO0lBQzVCLGFBQWE7SUFDYixZQUNVLEtBQXdCLEVBQ3hCLGdCQUEyQztRQUQzQyxVQUFLLEdBQUwsS0FBSyxDQUFtQjtRQUN4QixxQkFBZ0IsR0FBaEIsZ0JBQWdCLENBQTJCO0lBQ2xELENBQUM7SUFFSjs7O09BR0c7SUFDSCxLQUFLLENBQUMsTUFBTSxDQUFDLEtBQXNCO1FBQ2pDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxLQUFLLEVBQUUsSUFBSSxDQUFDLEtBQUssQ0FBQyxFQUFFLENBQUM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