Skip to content

Commit

Permalink
test(synthetic-shadow): add more tests/benchmarks (#4187)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed May 1, 2024
1 parent 6a1b9b3 commit bd7bc7f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { IS_SYNTHETIC_SHADOW_LOADED } from 'test-utils';

// From @lwc/shared/src/keys.ts
const KEY__SHADOW_RESOLVER = '$shadowResolver$';
const KEY__SHADOW_STATIC = '$shadowStaticNode$';

if (IS_SYNTHETIC_SHADOW_LOADED) {
describe('sets shadow resolver correctly on static trees', () => {
const fragments = [
'<div></div>',
'<div>hello</div>',
'<div><!-- foo --></div>',
'<section><div></div><div></div></section>',
'<section><div>hello</div><div></div></section>',
'<section><div></div><div>hello</div></section>',
'<section><div>hello</div><div>hello</div></section>',
'<section><div><!-- foo --></div><div></div></section>',
'<section><div></div><div><!-- foo --></div></section>',
'<section><div><!-- foo --></div><div><!-- foo --></div></section>',
'<section><div>hello</div><div><!-- foo --></div></section>',
];

const scenarios = [
{},
{
withParent: true,
},
{
withParent: true,
withRightSibling: true,
},
{
withParent: true,
withLeftSibling: true,
},
{
withParent: true,
withLeftSibling: true,
withRightSibling: true,
},
];

fragments.forEach((fragment) => {
scenarios.forEach(({ withParent, withRightSibling, withLeftSibling }) => {
describe(`${withParent ? 'with parent' : 'without parent'}${
withLeftSibling ? ' and left sibling' : ''
}${withRightSibling ? ' and right sibling' : ''}`, () => {
it(fragment, () => {
const parent = document.createElement('div');
parent.innerHTML = fragment;
const root = parent.firstChild;

if (!withParent) {
parent.removeChild(root);
}
const leftSibling = document.createElement('div');
if (withLeftSibling) {
parent.insertBefore(leftSibling, root);
}
const rightSibling = document.createElement('div');
if (withRightSibling) {
parent.appendChild(rightSibling);
}

const resolver = () => {};
root[KEY__SHADOW_RESOLVER] = resolver;
root[KEY__SHADOW_STATIC] = true;

// ensure the shadow resolver is set on all nodes in the tree
const expectShadowResolver = (node) => {
expect(node[KEY__SHADOW_RESOLVER]).toBe(resolver);
for (const childNode of Array.from(node.childNodes)) {
expectShadowResolver(childNode);
}
};
expectShadowResolver(root);

// ensure we don't traverse up past the root
for (const node of [parent, leftSibling, rightSibling]) {
expect(node[KEY__SHADOW_RESOLVER]).toBeUndefined();
}
});
});
});
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import '@lwc/synthetic-shadow';
import { createElement } from '@lwc/engine-dom';

import Table from '@lwc/perf-benchmarks-components/dist/dom/benchmark/table/table.js';
import Store from '@lwc/perf-benchmarks-components/dist/dom/benchmark/store/store.js';
import { insertComponent, destroyComponent } from '../../../utils/utils.js';

benchmark(`dom/table/synthetic-shadow/create/10k`, () => {
let tableElement;

before(() => {
tableElement = createElement('benchmark-table', { is: Table });
return insertComponent(tableElement);
});

run(() => {
const store = new Store();
store.runLots();
tableElement.rows = store.data;
});

after(() => {
destroyComponent(tableElement);
});
});

0 comments on commit bd7bc7f

Please sign in to comment.