Index

core/testing/src/mocks/element.mock.ts

Properties
Methods

Constructor

constructor(tagName?: string)
Parameters:
Name Type Optional
tagName string Yes

Properties

Public children
Type: any[]
Default value: []
Public Optional className
Type: string
Public Optional dir
Type: "ltr" | "rtl"
Public Optional parentElement
Type: MockElement
Public Optional tagName
Type: string

Methods

Public appendChild
appendChild(node: any)
Parameters:
Name Type Optional
node any No
Returns: void
Public extractAttributesFromSelector
extractAttributesFromSelector(selector: string)

Extracts an array of {name, value} objects mapping the attributes from the specified selector string. Attributes with no value will be mapped with wildcard value (i.e. '**').

Parameters:
Name Type Optional
selector string No
Returns: any
Public querySelectorAll
querySelectorAll(selector: string)
Parameters:
Name Type Optional
selector string No
Returns: any[]
Public remove
remove()
Returns: void
Public removeChild
removeChild(node: any)
Parameters:
Name Type Optional
node any No
Returns: void
export class MockElement
{
    public parentElement?: MockElement;
    public children     : any[] = [];

    public dir?      : 'ltr' | 'rtl';
    public className?: string;

    constructor(public tagName?: string) { }

    public remove(): void
    {
        if (this.parentElement) this.parentElement.removeChild(this);
    }

    public removeChild(node: any): void
    {
        if (node.parentElement !== this) return;

        const index = this.children.indexOf(node);

        if (index > -1)
        {
            this.children.splice(index, 1);

            node.parentElement = null;
        }
    }

    public appendChild(node: any): void
    {
        this.children.push(node);

        node.parentElement = this;

        if (node.onload instanceof Function) setTimeout(node.onload, 0);
    }

    public querySelectorAll(selector: string): any[]
    {
        throw new Error(`
            Providing a general implementation for querySelectorAll() to support all cases is to complex.
            Use jest.spyOn() and fake this to provide an implementation for the specific use case.
            See MockElement.extractXXXFromSelector() methods for utils.
        `);
    }

    /**
     * Extracts an array of {name, value} objects mapping the attributes from the specified selector string.
     * Attributes with no value will be mapped with wildcard value (i.e. '**').
     *
     * @param {string} selector
     * @returns {*}
     */
    public extractAttributesFromSelector(selector: string): any
    {
        // Searches for [key="value"] and [key] groups and extracts the attribute and value from each
        const regex = /(?:(\[(?<attr>\w+)(?:="(?<value>[^\]]+)")?)\]*)/g;
        let match: RegExpExecArray | null;
        
        const attributes = [];
        
        while ((match = regex.exec(selector)) !== null)
        {
            // This is necessary to avoid infinite loops with zero-width matches
            if (match.index === regex.lastIndex) regex.lastIndex++;

            attributes.push({ name: match.groups?.['attr'], value: match.groups?.['value'] || '**' });
        }

        return attributes;
    }
}

results matching ""

    No results matching ""