eslint-plugin-ember icon indicating copy to clipboard operation
eslint-plugin-ember copied to clipboard

New Rule: No this.element in tests

Open mehulkar opened this issue 5 years ago • 6 comments

Couldn't find a lint rule for this, but I would like to catch accidental usages of this.element in integration tests. I think that's an anitpattern now?

mehulkar avatar Jan 28 '21 08:01 mehulkar

Is it? It's still in component test blueprints

jaydgruber avatar Feb 01 '21 03:02 jaydgruber

hmm, does that work with Glimmer components?

mehulkar avatar Feb 01 '21 21:02 mehulkar

Ohhh I see, you're talking about the "wrapper element" of the component. Yeah that would not make sense with Glimmer components.

I think though, that this.element in a test is instead referring to the test container element. #ember-testing or similar.

I can't find a great link that spells that out, but a lot of the test helper files refer to this as rootElement or something similar. And I log out this.element in an integration test in my app I get: <div id="ember-testing" class="ember-application">

jaydgruber avatar Feb 01 '21 22:02 jaydgruber

ah ok, my mistake then. I thought it referred to the component's outer element. Maybe I'm thinking of this.$() or whatever the jquery thing was.

I'm ok with closing this if this.element is ok to keep using. But this maybe it still makes sense to nudge towards find() ?

mehulkar avatar Feb 02 '21 02:02 mehulkar

this.element refers to the main test container element and works in all types of test that actually do rendering (setupApplicationTest / setupRenderingTest).

The meaning of this.element has a couple of small quirks depending on the status of the various optional features, but in octane apps it is very clear.

rwjblue avatar Feb 04 '21 03:02 rwjblue

But this maybe it still makes sense to nudge towards find() ?

In general I dislike suggesting find for a few reasons:

  • needing a special find API makes it seem like there is some sort of magic going on (in the past there was), the fact that it is "just" a DOM element that you can interact with is much clearer with this.element over find
  • forgetting to import leads to mega trolling (window.find is a thing)
  • usage of assert.dom accommodate lots of the use cases where manual querying is done

Ironically, I didn't even want to add a find. The only reason I did was to make migration from the older apis easier, though at this point it would probably be hard to deprecate.

rwjblue avatar Feb 04 '21 03:02 rwjblue