Skip to content

Commit b762707

Browse files
authoredFeb 23, 2022
feat(testing-helpers): Allow configuring of render function (#2412)
1 parent 5f8be58 commit b762707

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed
 

Diff for: ‎.changeset/selfish-wombats-chew.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@open-wc/testing-helpers": minor
3+
"@open-wc/testing": minor
4+
---
5+
6+
Feat/customize render

Diff for: ‎docs/docs/testing/helpers.md

+10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ const el = await fixture(html` <my-el></my-el> `, { parentNode });
127127
<div style="position:absolute;"><my-el></my-el></div>
128128
```
129129
130+
## Customize the render function
131+
132+
It could be the case that you need to customize the render function for compatibility reasons, for example if you have multiple versions of lit-html throughout your project. You can achieve this like so:
133+
134+
```js
135+
import { render } from 'custom-lit-html-version';
136+
137+
const el = await fixture(html`<my-el></my-el>`, { render });
138+
```
139+
130140
## Timings
131141
132142
By default fixture awaits the elements "update complete" Promise.

Diff for: ‎packages/testing-helpers/src/fixture-no-side-effect.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isValidRenderArg } from './lib.js';
44

55
/**
66
* @typedef {object} FixtureOptions
7+
* @property {*} [render] optional render function to use
78
* @property {Element} [parentNode] optional parent node to render the fixture's template to
89
* @property {import('@open-wc/scoped-elements').ScopedElementsMap} [scopedElements] optional scoped-elements
910
* definition map
@@ -24,7 +25,7 @@ export function fixtureSync(template, options) {
2425
if (typeof template === 'string') {
2526
return stringFixtureSync(template, options);
2627
}
27-
if (isValidRenderArg(template)) {
28+
if (!!options?.render || isValidRenderArg(template)) {
2829
return litFixtureSync(template, options);
2930
}
3031
throw new Error(
@@ -57,7 +58,7 @@ export async function fixture(template, options) {
5758
if (typeof template === 'string') {
5859
return stringFixture(template, options);
5960
}
60-
if (isValidRenderArg(template)) {
61+
if (!!options?.render || isValidRenderArg(template)) {
6162
return litFixture(template, options);
6263
}
6364
throw new Error('Invalid template provided - string or lit-html TemplateResult is supported');

Diff for: ‎packages/testing-helpers/src/litFixture.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from 'lit/html.js';
1+
import { render as defaultRender } from 'lit/html.js';
22
import { isTemplateResult } from 'lit/directive-helpers.js';
33
import { fixtureWrapper } from './fixtureWrapper.js';
44
import { elementUpdated } from './elementUpdated.js';
@@ -26,6 +26,7 @@ const isUsefulNode = ({ nodeType, textContent }) => {
2626
*/
2727
export function litFixtureSync(template, options = {}, getScopedElementsTemplate) {
2828
const wrapper = /** @type {HTMLElement} */ (fixtureWrapper(options.parentNode));
29+
const render = options.render ?? defaultRender;
2930

3031
render(
3132
options.scopedElements ? getScopedElementsTemplate(template, options.scopedElements) : template,

Diff for: ‎packages/testing-helpers/test-web/fixture.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ describe('fixtureSync & fixture', () => {
6363
testElement(elementAsync);
6464
});
6565

66+
it('supports customized render functions', async () => {
67+
const render = sinon.stub();
68+
await fixture(html`<div></div>`, { render });
69+
expect(render.called).to.equal(true);
70+
});
71+
6672
it('supports lit-html TemplateResult with whitespace', async () => {
6773
/**
6874
* @param {Element} element

0 commit comments

Comments
 (0)
Please sign in to comment.