-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine): enable
attachInternals
API (#3670)
- Loading branch information
Showing
16 changed files
with
280 additions
and
43 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
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
1 change: 1 addition & 0 deletions
1
...integration-karma/test/component/LightningElement.attachInternals/api/ai/basic/basic.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 @@ | ||
<template></template> |
3 changes: 3 additions & 0 deletions
3
...c/integration-karma/test/component/LightningElement.attachInternals/api/ai/basic/basic.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,3 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class extends LightningElement {} |
3 changes: 3 additions & 0 deletions
3
...ation-karma/test/component/LightningElement.attachInternals/api/ai/lightDom/lightDom.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 lwc:render-mode="light"> | ||
<p>Hello, Light DOM</p> | ||
</template> |
9 changes: 9 additions & 0 deletions
9
...gration-karma/test/component/LightningElement.attachInternals/api/ai/lightDom/lightDom.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 } from 'lwc'; | ||
|
||
export default class extends LightningElement { | ||
static renderMode = 'light'; | ||
|
||
connectedCallback() { | ||
this.internals = this.attachInternals(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ion-karma/test/component/LightningElement.attachInternals/api/ai/shadowDom/shadowDom.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 @@ | ||
<template></template> |
19 changes: 19 additions & 0 deletions
19
...ation-karma/test/component/LightningElement.attachInternals/api/ai/shadowDom/shadowDom.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,19 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
|
||
export default class extends LightningElement { | ||
internals; | ||
|
||
connectedCallback() { | ||
this.internals = this.attachInternals(); | ||
} | ||
|
||
@api | ||
callAttachInternals() { | ||
this.internals = this.attachInternals(); | ||
} | ||
|
||
@api | ||
hasElementInternalsBeenSet() { | ||
return Boolean(this.internals); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
.../@lwc/integration-karma/test/component/LightningElement.attachInternals/api/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,83 @@ | ||
import { createElement } from 'lwc'; | ||
import { customElementConnectedErrorListener } from 'test-utils'; | ||
|
||
import ShadowDomCmp from 'ai/shadowDom'; | ||
import LightDomCmp from 'ai/lightDom'; | ||
import BasicCmp from 'ai/basic'; | ||
|
||
const testConnectedCallbackError = (elm, msg) => { | ||
const error = customElementConnectedErrorListener(() => { | ||
document.body.appendChild(elm); | ||
}); | ||
expect(error).not.toBeUndefined(); | ||
expect(error.message).toBe(msg); | ||
}; | ||
|
||
const createTestElement = (name, def) => { | ||
const elm = createElement(name, { is: def }); | ||
document.body.appendChild(elm); | ||
return elm; | ||
}; | ||
|
||
if (typeof ElementInternals !== 'undefined') { | ||
if (process.env.NATIVE_SHADOW) { | ||
describe('native shadow', () => { | ||
let elm; | ||
beforeEach(() => { | ||
elm = createTestElement('ai-shadow-component', ShadowDomCmp); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(elm); | ||
}); | ||
|
||
it('should be able to create ElementInternals object', () => { | ||
expect(elm.hasElementInternalsBeenSet()).toBeTruthy(); | ||
}); | ||
|
||
it('should throw an error when called twice on the same element', () => { | ||
// The error type is different between browsers | ||
expect(() => elm.callAttachInternals()).toThrowError(); | ||
}); | ||
}); | ||
} else { | ||
describe('synthetic shadow', () => { | ||
it('should throw error when used inside a component', () => { | ||
const elm = createElement('ai-synthetic-shadow-component', { is: ShadowDomCmp }); | ||
testConnectedCallbackError( | ||
elm, | ||
'attachInternals API is not supported in light DOM or synthetic shadow.' | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
describe('light DOM', () => { | ||
it('should throw error when used inside a component', () => { | ||
const elm = createElement('ai-light-dom-component', { is: LightDomCmp }); | ||
testConnectedCallbackError( | ||
elm, | ||
'attachInternals API is not supported in light DOM or synthetic shadow.' | ||
); | ||
}); | ||
}); | ||
} else { | ||
it('should throw an error when used with unsupported browser environments', () => { | ||
const elm = createElement('ai-unsupported-env-component', { is: ShadowDomCmp }); | ||
testConnectedCallbackError( | ||
elm, | ||
'attachInternals API is not supported in this browser environment.' | ||
); | ||
}); | ||
} | ||
|
||
it('should not be callable outside a component', () => { | ||
const elm = createTestElement('ai-component', BasicCmp); | ||
if (process.env.NODE_ENV === 'production') { | ||
expect(elm.attachInternals).toBeUndefined(); | ||
} else { | ||
expect(() => elm.attachInternals).toLogErrorDev( | ||
/Error: \[LWC error]: attachInternals cannot be accessed outside of a component\. Use this.attachInternals instead\./ | ||
); | ||
} | ||
}); |
1 change: 1 addition & 0 deletions
1
...t/component/LightningElement.attachInternals/elementInternals/ei/component/component.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 @@ | ||
<template></template> |
22 changes: 22 additions & 0 deletions
22
...est/component/LightningElement.attachInternals/elementInternals/ei/component/component.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,22 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
import { ariaProperties } from 'test-utils'; | ||
|
||
export default class extends LightningElement { | ||
@api | ||
internals; | ||
|
||
@api | ||
template; | ||
|
||
connectedCallback() { | ||
this.internals = this.attachInternals(); | ||
this.template = super.template; | ||
} | ||
|
||
@api | ||
setAllAriaProps(value) { | ||
for (const prop of ariaProperties) { | ||
this.internals[prop] = value; | ||
} | ||
} | ||
} |
Oops, something went wrong.