Skip to content

feat(cdk/testing/TestElement): Need a way to set properties on TestElement #26424

Open

Description

Feature Description

I would like to see a method on TestElement that allows for setting properties on the underlying element. Specifically, for setting the files property on an input element.

Use Case

I am creating a feature for an app to allow users to change their profile picture (avatar). This feature is similar to the one on Google Accounts (shown below).

image

I ran into a problem trying to create a ComponentHarness that would allow me to simulate the change event on the underlying input element with a list of files. The files need to be set on the underlying input element before dispatching a change event. There is currently no way to set properties on the underlying element before dispatching the event. Currently since I am only using the ComponentHarness for unit tests I simple cast the TestElement.

  async setFiles(files: File[]) {
    const input = await this._input();

    if (files && input instanceof UnitTestElement) {
      Object.defineProperty((input as UnitTestElement).element, 'files', {
        configurable: true,
        enumerable: true,
        writable: true,
        value: files,
      });
    }

    input.dispatchEvent('change');
  }

The other way to do this, AFAIK is to extend TestElement and TestBedHarnessEnvironment to return the extended TestElement.

I am not sure if creating a generic setProperties method on TestElement similar to being able to set EventData on events is the way to go or if a specific setInputFiles similar to setInputValue should be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: cdk/testingfeatureThis issue represents a new feature or feature request rather than a bug or bug fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions