Skip to content

Commit

Permalink
chore: update grid and menu to use testing helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajdeep Chandra authored and Westbrook Johnson committed Apr 2, 2024
1 parent 407f3ca commit 20e31f4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 105 deletions.
54 changes: 4 additions & 50 deletions packages/menu/test/menu-memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,9 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { expect, fixture, nextFrame } from '@open-wc/testing';
import { html, render } from '@spectrum-web-components/base';
import { fixture } from '@open-wc/testing';
import { singleSelect } from '../stories/menu.stories.js';
import { usedHeapMB } from '../../../test/testing-helpers.js';
import { Menu } from '@spectrum-web-components/menu';
import { testForMemoryLeaks } from '../../../test/testing-helpers.js';

describe('Menu memory usage', () => {
it('releases references on disconnect', async function () {
if (!window.gc || !('measureUserAgentSpecificMemory' in performance))
this.skip();

this.timeout(10000);

const iterations = 50;
let active = false;

const el = await fixture<HTMLElement>(
html`
<div></div>
`
);

async function toggle(
forced: boolean | undefined = undefined
): Promise<void> {
active = forced != null ? forced : !active;
render(active ? singleSelect() : html``, el);
await nextFrame();
await nextFrame();
}

// "shake things out" to get a good first reading
for (let i = 0; i < 5; i++) {
await toggle();
}
await toggle(false);
const beforeMB = await usedHeapMB();

for (let i = 0; i < iterations; i++) {
await toggle();
}
await toggle(false);
const afterMB = await usedHeapMB();

expect(
afterMB.total - beforeMB.total,
`Total | before: ${beforeMB.total}, after: ${afterMB.total}
DOM | before: ${beforeMB.dom}, after: ${afterMB.dom}
JS | before: ${beforeMB.js}, after: ${afterMB.js}
Shared | before: ${beforeMB.shared}, after: ${afterMB.shared}`
).to.be.lte(0);
});
});
testForMemoryLeaks(async () => await fixture<Menu>(singleSelect()));
59 changes: 4 additions & 55 deletions tools/grid/test/grid-memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,9 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { expect, fixture, nextFrame } from '@open-wc/testing';
import { html, render } from '@spectrum-web-components/base';
import { fixture } from '@open-wc/testing';
import { Default } from '../stories/grid.stories.js';
import { usedHeapMB } from '../../../test/testing-helpers.js';
import { Grid } from '@spectrum-web-components/grid';
import { testForMemoryLeaks } from '../../../test/testing-helpers.js';

describe('Grid memory usage', () => {
it('releases references on disconnect', async function () {
if (!window.gc || !('measureUserAgentSpecificMemory' in performance))
this.skip();

this.timeout(10000);

const iterations = 50;
let active = false;

const el = await fixture<HTMLElement>(
html`
<div></div>
`
);

async function toggle(
forced: boolean | undefined = undefined
): Promise<void> {
active = forced != null ? forced : !active;
render(active ? Default() : html``, el);
await nextFrame();
await nextFrame();
}

// "shake things out" to get a good first reading
for (let i = 0; i < 5; i++) {
await toggle();
}
await toggle(false);
const beforeMB = await usedHeapMB();

for (let i = 0; i < iterations; i++) {
await toggle();
}
await toggle(false);
const afterMB = await usedHeapMB();

/**
* An actually leak here shapes up to be more than 10MB per test,
* we could be more linient later, if needed, but the test currently
* shows less heap after the test cycle.
*/
expect(
afterMB.total - beforeMB.total,
`Total | before: ${beforeMB.total}, after: ${afterMB.total}
DOM | before: ${beforeMB.dom}, after: ${afterMB.dom}
JS | before: ${beforeMB.js}, after: ${afterMB.js}
Shared | before: ${beforeMB.shared}, after: ${afterMB.shared}`
).to.be.lte(0);
});
});
testForMemoryLeaks(async () => await fixture<Grid>(Default()));

0 comments on commit 20e31f4

Please sign in to comment.