File

projects/angular/testing/src/utilities/fake-file-list.ts

Description

A class that implements the FileList interface. This class facilitates easy creation of FileList mocks, for UTs.

Implements

FileList

Index

Properties
Methods
Accessors

Constructor

constructor(files: File[])

Creates a mock for FileList in order to easily test file centric scenarios with inputs.

Parameters :
Name Type Optional Description
files File[] No

A list of files.

Properties

Public files
Type : File[]
Default value : []
A list of files.

Methods

add
add(...files: File[])

Add files to the collection.

Parameters :
Name Type Optional Description
files File[] No

The files that will be added to the collection.

Returns : void
item
item(idx: number)

Retrieve an item at the specified index.

Parameters :
Name Type Optional Description
idx number No

The accesed index.

Returns : File

The file at the requested idx.

()
Returns : IterableIterator<File>

Indexable

[index: number]: File

Accessors

length
getlength()

The total file count.

export class FakeFileList implements FileList {
    [index: number]: File;

    /**
     * The total file count.
     *
     */
    get length() {
        return this.files.length;
    }

    /**
     * Creates a `mock` for `FileList` in order to easily test file centric scenarios with `input`s.
     *
     * @param [files=[]] A list of files.
     * @returns The mocked `FileList` instance.
     */
    constructor(public files: File[] = []) {
        files.forEach((file, idx) => {
            this[idx] = file;
        });
    }

    [Symbol.iterator](): IterableIterator<File> {
        return this.files[Symbol.iterator]();
    }

    /**
     * Retrieve an item at the specified index.
     *
     * @param idx The accesed index.
     * @returns The file at the requested `idx`.
     */
    item(idx: number): File {
        return this[idx];
    }

    /**
     * Add files to the collection.
     *
     * @param files The files that will be added to the collection.
     */
    add(...files: File[]): void {
        files.forEach(file => {
            this[this.files.length] = file;
            this.files.push(file);
        });
    }
}

results matching ""

    No results matching ""