Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Jesthowtomockafakereturnvalue

I have the following class in my project where i want to write a test case for using Jest.

What i want to test is if the completeFiles array contains, for example, 5 items if the this.props.file contains 5 items. For this i have to mock the getFiles method so that it can return 5 items. After that i have to check if the completeFiles array contains these 5 items.

Currently im struggling on how to get this done. I have been looking into Sinon and Jest function mocks but i can't get any of them to do what i want. Any hope someone can provide a sample test case with some explanation on what is happening? I would love to learn more on how to test something like this.

export class FileList  {
    removeFile = (fileName) => {
        super.removeFile(fileName);
        this.props.onFileRemoved(fileName);
    }

    updateFileName(fileName) {
        this.props.onFileChanged(fileName);
    }

    getFileData() {
        const { fileData } = this.props;
        return fileData.length > 0 ? fileData : 0;
    }

    render() {
        const { file } = this.props;
        let completeFiles = [];
        this.getFileData().forEach((fileData) => {
            completeFiles.push(
                <FileContainer
                    key={fileData.id}
                    file={file}
                    onDelete={this.removeFile}
                    onNameChange={this.updateFileName}
                />,
            );
        });

        return completeFiles;
    }
}

Comments