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.

[testing-helpers] Allow to inject wrapper element in fixture

See original GitHub issue

Currently there’s no way to customize the creation of wrapper element used as parent to the template.

A use case is testing an element that uses the dependency injection technique described by Polymer dev here and here. It relies on a parent element that listen to an event dispatched in the connectedCallback

An actual example of testing is here. app-header element depends on a context provided by a parent element. Currently there’s no way to properly test it.

This is something i can work if approved

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
blikblumcommented, Dec 14, 2019

by making the updateComplete of you class to include all it’s children

  1. Changing the implementation of the component (with complexity increase and performance penalty) just to allow testing does not sound good

  2. Even considering the above, context provider elements does not deals with children rendering at all

As a user you create the fixture and therefore you should also know if the element needs some more time then the default…

Yes, the solution is to await the rendering for the child element like below:

const wrapper = await fixture(html`
  <my-context .data=${...}>
       <my-el></my-el>
  </my-context>
`;

const el = wrapper.querySelector('my-el');
await el.updateComplete

With the following caveats

  • adds yet more boilerplate and the whole reason of testing-helpers is to avoid them
  • the test must be aware how my-el is implemented (skatejs / lit / stencil)
  • it makes harder to visualize what component you are testing. Compare with the below:
const el = await fixture(html`<my-el></my-el>`, { parent: contextEl });

Finally, the code required to implement the ability to define the the wrapper/parent is moving one line in one place (the remaining code just pass options argument down the call stack).

Its less than the required code to implement one test case with the proposed solution

0reactions
LarsDenBakkercommented, Dec 23, 2019

Allowing setting the wrapper element does seem like a simple solution as well given the complexities. @daKmoR what do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Components – Testing Angular
Let us wrap the code in a Jasmine test suite. describe('CounterComponent', () => { let fixture: ComponentFixture<CounterComponent>; ...
Read more >
Write your tests - PatternFly Elements
A test fixture renders a piece of HTML and injects into the DOM so that you can test the behavior of your component....
Read more >
Testing Your Web Components · Blog
Creating a custom element for a demo todo list in a test driven ... The @open-wc/testing-helpers give a function fixture that allows us...
Read more >
web component - Lit Element, unit testing - Nested Elements
For this we have created another component, <carousel-helper> , which has the code to accept an array of markups and render it as...
Read more >
open-wc/testing-helpers - npm
You don't need to use lit-html in your project to use the test fixtures, it just renders standard HTML. Test a custom element....
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