Skip to content

Commit

Permalink
test: Fixes for new happy-dom API
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmilton committed Jan 16, 2024
1 parent 6dc63d0 commit fc61226
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 8 additions & 2 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ function setupMocks(): void {
global.performance.measure = noop;
}

export function reset(): void {
export async function reset(): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (global.happyDOM) {
await happyDOM.abort();
window.close();
}

setupDOM();
setupMocks();
}

reset();
await reset();
12 changes: 8 additions & 4 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,17 @@ describe('onRemove', () => {
expect(onRemove).toHaveLength(2);
});

test('calls callback when watched element is removed', () => {
test('calls callback when watched element is removed', async () => {
const spy = mock(() => {});
const root = document.createElement('div');
document.body.appendChild(root);
onRemove(root, spy);
root.remove();
await happyDOM.waitUntilComplete();
expect(spy).toHaveBeenCalledTimes(1);
});

test('calls callback when parent parent element is removed', () => {
test('calls callback when parent parent element is removed', async () => {
const spy = mock(() => {});
const root = document.createElement('div');
const parent = document.createElement('div');
Expand All @@ -321,28 +322,31 @@ describe('onRemove', () => {
document.body.appendChild(root);
onRemove(child, spy);
root.remove();
await happyDOM.waitUntilComplete();
expect(spy).toHaveBeenCalledTimes(1);
});

test('does not call callback when nested child element is removed', () => {
test('does not call callback when nested child element is removed', async () => {
const spy = mock(() => {});
const root = document.createElement('div');
const child = document.createElement('div');
root.appendChild(child);
document.body.appendChild(root);
onRemove(root, spy);
child.remove();
await happyDOM.waitUntilComplete();
expect(spy).not.toHaveBeenCalled();
});

test('does not call callback when element is added or moved', () => {
test('does not call callback when element is added or moved', async () => {
const spy = mock(() => {});
const root = document.createElement('div');
const child = document.createElement('div');
document.body.appendChild(root);
onRemove(root, spy);
root.appendChild(child);
document.body.appendChild(root);
await happyDOM.waitUntilComplete();
expect(spy).not.toHaveBeenCalled();
});
});

0 comments on commit fc61226

Please sign in to comment.