question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CDK - HarnessPredicate: override or disable base options

See original GitHub issue

Feature Description

Option to disable or override the base options of a HarnessPredicate to keep our Harness implementation consistent.

Use Case

We have a component library and in some cases the FormControl is wrapped inside a custom component. For accessibility reasons we still want to link the label with the underlying FormControl. To do so, we need to remove the attribute reflection on the Host element to prevent having multiple Ids on the page.

// This code prevents the attribute reflection of the Id attribute into the DOM
@HostBinding('attr.id') null;

The problem is, that this breaks the lookup via selector on the base options. We would need to change the lookup to check the FormControl instead of the Host.

Of course, we could provide custom HarnessFilters. But then we would need to rename the options. It would be nice if we could override or disable the base options. Disabling or overriding the base options would allow us to keep a consistent interface throughout all components in our UI framework. Basically every Harness object would always provide the selector plus maybe something additional.

Possible solutions / approaches

Pass in custom match function

The HarnessPredicate could accept a custom match function

  constructor(public harnessType: ComponentHarnessConstructor<T>, 
  		     options: BaseHarnessFilters,
                     matchFunction?: ((option: BaseHarnessFilters) => Promise<boolean>) | null) 
{  }

Option to disable the base functions and then use the addOption to provide them yourself

  constructor(public harnessType: ComponentHarnessConstructor<T>, 
  		      options: BaseHarnessFilters,
                      disableBaseFunctions = false)
  {}

and then inside our Harness implementation:

 static with(options: BaseHarnessFilters): HarnessPredicate<CustomHarness> {
    return new HarnessPredicate(CustomHarness, options).addOption(
      'custom selector option',
      options.selector,
      async (harness: CustomHarness, selector: string) => {
        if (selector.startsWith('#')) {
          const inputField = await harness.getInputFieldElement();
          console.log(`#${inputField.getAttribute('id')}` === selector);
          return HarnessPredicate.stringMatches(
            `#${inputField.getAttribute('id')}`,
            selector
          );
        }
        return (await harness.host()).matchesSelector(selector);
      }
    );
  }

Let me know what you think of those proposed solutions. Is this something that you consider? If yes, I would be happy to open a PR.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mmalerbacommented, Sep 10, 2020

Do we actually need to change anything to support this? Why don’t they just not pass the options through in their with function, like this:

static with(options: BaseHarnessFilters): HarnessPredicate<CustomHarness> {
    return new HarnessPredicate(CustomHarness, {})
        .addOption('custom selector option', options.selector, (...) => {...})
}

Or is there something I’m missing?

1reaction
mmalerbacommented, Sep 10, 2020

I would prefer to keep it non-optional so that people don’t forget to pass it along as @devversion mentioned

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component Harnesses - Angular Material
A component harness is a class that lets a test interact with a component via a supported API. Each harness's API interacts with...
Read more >
Create a component harness for your tests with Angular CDK
Learn how to create and consume a custom component harness using Angular CDK. With a step-by-step case study, we run it in unit...
Read more >
How to disable or overwrite cdk-focused in Angular
If buttons are contained by a parent mat element, there is an option to disable autofocus with autoFocus @input property of some elements....
Read more >
Tidy up your tests using component test harnesses - YouTube
Angular CDK's test harnesses allow us to write component unit tests that are easier to create and maintain. In this talk, we'll cover...
Read more >
Stepper - Angular Material
Whether ripples should be disabled for the step headers. ... selected: CdkStep | undefined ... a HarnessPredicate configured with the given options.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found