-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(runtime): add patch for `removeChild` on `scoped` components This commit adds a patch for an HtmlElement's `removeChild` method Fixes: #3278 #2259 STENCIL-937 Co-authored-by: johnjenkins <johnljenkins@hotmail.com> * add e2e tests * PR feedback Co-authored-by: Ryan Waskiewicz <ryanwaskiewicz@gmail.com> --------- Co-authored-by: johnjenkins <johnljenkins@hotmail.com> Co-authored-by: Ryan Waskiewicz <ryanwaskiewicz@gmail.com>
- Loading branch information
1 parent
c6b723f
commit 956c196
Showing
6 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Component, h } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'remove-child-patch', | ||
scoped: true, | ||
}) | ||
export class RemoveChildPatch { | ||
render() { | ||
return ( | ||
<div> | ||
<p>I'm not in a slot</p> | ||
<div class="slot-container"> | ||
<slot>Slot fallback content</slot> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!doctype html> | ||
<meta charset="utf8" /> | ||
<script src="./build/testapp.esm.js" type="module"></script> | ||
<script src="./build/testapp.js" nomodule></script> | ||
|
||
<remove-child-patch> | ||
<span>Slotted 1</span> | ||
<span>Slotted 2</span> | ||
</remove-child-patch> | ||
|
||
<button id="remove-child-button" type="button">Remove Last Child</button> | ||
<button id="remove-child-div-button" type="button">Remove Child Div</button> | ||
|
||
<script> | ||
document.querySelector('#remove-child-button').addEventListener('click', () => { | ||
const el = document.querySelector('remove-child-patch'); | ||
const slotContainer = el.querySelector('.slot-container'); | ||
const elementToRemove = slotContainer.children[slotContainer.children.length - 1]; | ||
el.removeChild(elementToRemove); | ||
}); | ||
|
||
document.querySelector('#remove-child-div-button').addEventListener('click', () => { | ||
const el = document.querySelector('remove-child-patch'); | ||
const elementToRemove = el.querySelector('div'); | ||
el.removeChild(elementToRemove); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { setupDomTests, waitForChanges } from '../util'; | ||
|
||
/** | ||
* Tests for the patched `removeChild()` method on `scoped` components. | ||
*/ | ||
describe('remove-child-patch', () => { | ||
const { setupDom, tearDownDom } = setupDomTests(document); | ||
|
||
let app: HTMLElement | undefined; | ||
let host: HTMLElement | undefined; | ||
|
||
beforeEach(async () => { | ||
app = await setupDom('/remove-child-patch/index.html'); | ||
host = app.querySelector('remove-child-patch'); | ||
}); | ||
|
||
afterEach(tearDownDom); | ||
|
||
it('should remove the last slotted node', async () => { | ||
expect(host).toBeDefined(); | ||
|
||
const slotContainer = host.querySelector('.slot-container'); | ||
expect(slotContainer).toBeDefined(); | ||
const slottedSpans = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpans.length).toBe(2); | ||
|
||
document.querySelector('button').click(); | ||
await waitForChanges(); | ||
|
||
const slottedSpansAfter = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpansAfter.length).toBe(1); | ||
}); | ||
|
||
it('should show slot-fb if the last slotted node is removed', async () => { | ||
expect(host).toBeDefined(); | ||
|
||
const slotContainer = host.querySelector('.slot-container'); | ||
expect(slotContainer).toBeDefined(); | ||
const slottedSpans = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpans.length).toBe(2); | ||
|
||
const button = document.querySelector<HTMLButtonElement>('#remove-child-button'); | ||
button.click(); | ||
await waitForChanges(); | ||
button.click(); | ||
await waitForChanges(); | ||
|
||
const slottedSpansAfter = slotContainer.querySelectorAll('span'); | ||
expect(slottedSpansAfter.length).toBe(0); | ||
expect(slotContainer.textContent.trim()).toBe('Slot fallback content'); | ||
}); | ||
|
||
it('should still be able to remove nodes not slotted', async () => { | ||
expect(host).toBeDefined(); | ||
|
||
expect(host.querySelector('div')).toBeDefined(); | ||
|
||
const button = document.querySelector<HTMLButtonElement>('#remove-child-div-button'); | ||
button.click(); | ||
await waitForChanges(); | ||
|
||
expect(host.querySelector('div')).toBeNull(); | ||
}); | ||
}); |