-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for disconnectedCallback (#1094)
* test: add tests for disconnectedCallback * fix: prettier errors * fix: pr review feedback
- Loading branch information
1 parent
4d54d08
commit fa645e2
Showing
9 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...ages/integration-karma/test/component/LightningElement.disconnectedCallback/index.spec.js
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,87 @@ | ||
import { createElement } from 'test-utils'; | ||
|
||
import Slotted from 'x/slotted'; | ||
import Test from 'x/test'; | ||
|
||
function testDisconnectSlot(name, fn) { | ||
it(`should invoke the disconnectedCallback when root element is removed from the DOM via ${name}`, done => { | ||
const elm = createElement('x-test', { is: Test }); | ||
elm.disconnect = function(context) { | ||
expect(context instanceof Test).toBe(true); | ||
done(); | ||
}; | ||
|
||
fn(elm); | ||
}); | ||
} | ||
|
||
testDisconnectSlot('Node.removeChild', elm => { | ||
document.body.appendChild(elm); | ||
document.body.removeChild(elm); | ||
}); | ||
|
||
testDisconnectSlot('Node.replaceChild', elm => { | ||
const newChild = document.createElement('div'); | ||
document.body.appendChild(elm); | ||
document.body.replaceChild(newChild, elm); | ||
}); | ||
|
||
describe('disconnectedCallback for host with slots', () => { | ||
let parentDisconnectSpy; | ||
let slotIgnoringChildSpy; | ||
let slotAcceptingChildSpy; | ||
let parent; | ||
|
||
beforeAll(() => { | ||
// Ignore the engine logging about passing slot content to a component that does not accept slot | ||
spyOn(console, 'group'); | ||
spyOn(console, 'log'); | ||
spyOn(console, 'groupEnd'); | ||
}); | ||
|
||
beforeEach(() => { | ||
parentDisconnectSpy = jasmine.createSpy(); | ||
slotIgnoringChildSpy = jasmine.createSpy(); | ||
slotAcceptingChildSpy = jasmine.createSpy(); | ||
parent = createElement('x-slotted', { is: Slotted }); | ||
document.body.appendChild(parent); | ||
parent.disconnect = parentDisconnectSpy; | ||
parent.shadowRoot.querySelector('x-accepting-slots').disconnect = slotAcceptingChildSpy; | ||
parent.shadowRoot.querySelector('x-ignoring-slots').disconnect = slotIgnoringChildSpy; | ||
}); | ||
|
||
it('should invoke disconnectedCallback on host and all children components', () => { | ||
document.body.removeChild(parent); | ||
expect(parentDisconnectSpy).toHaveBeenCalledTimes(1); | ||
expect(slotAcceptingChildSpy).toHaveBeenCalledTimes(1); | ||
expect(slotIgnoringChildSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
/** | ||
* Automation for issue #1090 | ||
* In this scenario the slot content from the parent's template is unrendered. | ||
* disconnecting the slot receiver was causing errors | ||
**/ | ||
it('should invoke disconnectedCallback on child that has unrendered slot content', () => { | ||
parent.hideChildIgnoresSlots = true; | ||
|
||
return Promise.resolve(() => { | ||
expect(parentDisconnectSpy).not.toHaveBeenCalled(); | ||
expect(slotIgnoringChildSpy).toHaveBeenCalledTimes(1); | ||
expect(slotAcceptingChildSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should invoke disconnectedCallback on child that has rendered slot content', () => { | ||
const slotContent = parent.shadowRoot.querySelector('x-test.slotted'); | ||
const slotContentDisconnectSpy = jasmine.createSpy(); | ||
slotContent.disconnect = slotContentDisconnectSpy; | ||
parent.hideChildAcceptsSlots = true; | ||
return Promise.resolve(() => { | ||
expect(parentDisconnectSpy).not.toHaveBeenCalled(); | ||
expect(slotAcceptingChildSpy).toHaveBeenCalledTimes(1); | ||
expect(slotContentDisconnectSpy).toHaveBeenCalledTimes(1); | ||
expect(slotIgnoringChildSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
...test/component/LightningElement.disconnectedCallback/x/acceptingSlots/acceptingSlots.html
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,5 @@ | ||
<template> | ||
<slot> | ||
<div class="default"></div> | ||
</slot> | ||
</template> |
9 changes: 9 additions & 0 deletions
9
...a/test/component/LightningElement.disconnectedCallback/x/acceptingSlots/acceptingSlots.js
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,9 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
|
||
export default class AcceptingSlots extends LightningElement { | ||
@api disconnect; | ||
|
||
disconnectedCallback() { | ||
this.disconnect(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...a/test/component/LightningElement.disconnectedCallback/x/ignoringSlots/ignoringSlots.html
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,3 @@ | ||
<template> | ||
ignore slot content from parent | ||
</template> |
9 changes: 9 additions & 0 deletions
9
...rma/test/component/LightningElement.disconnectedCallback/x/ignoringSlots/ignoringSlots.js
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,9 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
|
||
export default class IgnoringSlots extends LightningElement { | ||
@api disconnect; | ||
|
||
disconnectedCallback() { | ||
this.disconnect(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...gration-karma/test/component/LightningElement.disconnectedCallback/x/slotted/slotted.html
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,16 @@ | ||
<template> | ||
<div> | ||
<template if:false={hideChildAcceptsSlots}> | ||
<x-accepting-slots> | ||
<div class="slotted"></div> | ||
<x-test class="slotted"></x-test> | ||
</x-accepting-slots> | ||
</template> | ||
<template if:false={hideChildIgnoresSlots}> | ||
<x-ignoring-slots> | ||
<div class="ignored-slot"></div> | ||
<x-test class="ignored-slot"></x-test> | ||
</x-ignoring-slots> | ||
</template> | ||
</div> | ||
</template> |
11 changes: 11 additions & 0 deletions
11
...tegration-karma/test/component/LightningElement.disconnectedCallback/x/slotted/slotted.js
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,11 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
|
||
export default class Slotted extends LightningElement { | ||
@api disconnect; | ||
@api hideChildIgnoresSlots = false; | ||
@api hideChildAcceptsSlots = false; | ||
|
||
disconnectedCallback() { | ||
this.disconnect(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...s/integration-karma/test/component/LightningElement.disconnectedCallback/x/test/test.html
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,2 @@ | ||
<template> | ||
</template> |
11 changes: 11 additions & 0 deletions
11
...ges/integration-karma/test/component/LightningElement.disconnectedCallback/x/test/test.js
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,11 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
|
||
export default class Test extends LightningElement { | ||
@api disconnect; | ||
|
||
disconnectedCallback() { | ||
if (this.disconnect) { | ||
this.disconnect(this); | ||
} | ||
} | ||
} |