-
Notifications
You must be signed in to change notification settings - Fork 439
isTrustedSignalshould return false when no trusted set has been defined #5500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
126 changes: 126 additions & 0 deletions
126
packages/@lwc/integration-not-karma/test/signal/untrusted/index.spec.js
This file contains hidden or 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,126 @@ | ||
| import { createElement, setFeatureFlagForTest } from 'lwc'; | ||
|
|
||
| import Test from 'x/test'; | ||
| import { Signal } from 'x/signal'; | ||
| import { spyConsole } from '../../../helpers/console.js'; | ||
|
|
||
| const createElementSignalAndInsertIntoDom = async (object) => { | ||
| const elm = createElement('x-test', { is: Test }); | ||
| elm.object = object; | ||
| document.body.appendChild(elm); | ||
| await Promise.resolve(); | ||
| return elm; | ||
| }; | ||
|
|
||
| describe('signal reaction in lwc', () => { | ||
| let consoleSpy; | ||
|
|
||
| beforeAll(() => setFeatureFlagForTest('ENABLE_EXPERIMENTAL_SIGNALS', true)); | ||
| afterAll(() => setFeatureFlagForTest('ENABLE_EXPERIMENTAL_SIGNALS', false)); | ||
| beforeEach(() => (consoleSpy = spyConsole())); | ||
| afterEach(() => consoleSpy.reset()); | ||
|
|
||
| describe('with trusted signal set', () => { | ||
| describe('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION is enabled', () => { | ||
| beforeAll(() => setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true)); | ||
| afterAll(() => setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false)); | ||
| it('will not warn if rendering non-signal objects ', async () => { | ||
| const elm = await createElementSignalAndInsertIntoDom({ | ||
| value: 'non signal value', | ||
| }); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| expect(elm.shadowRoot.textContent).toBe('non signal value'); | ||
| }); | ||
| it('will not warn if rendering signal objects', async () => { | ||
| const signal = new Signal('signal value'); | ||
| const elm = await createElementSignalAndInsertIntoDom(signal); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| signal.value = 'new signal value'; | ||
| await Promise.resolve(); | ||
| expect(elm.shadowRoot.textContent).toBe('new signal value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION is disabled', () => { | ||
| beforeAll(() => | ||
| setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false) | ||
| ); | ||
| it('will not warn if rendering non-signal objects', async () => { | ||
| const elm = await createElementSignalAndInsertIntoDom({ | ||
| value: 'non signal value', | ||
| }); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| expect(elm.shadowRoot.textContent).toBe('non signal value'); | ||
| }); | ||
| it('will not warn if rendering signal objects', async () => { | ||
| const signal = new Signal('signal value'); | ||
| const elm = await createElementSignalAndInsertIntoDom(signal); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| signal.value = 'new signal value'; | ||
| await Promise.resolve(); | ||
| expect(elm.shadowRoot.textContent).toBe('new signal value'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('without trusted signal set', () => { | ||
| beforeAll(() => globalThis.__lwcResetTrustedSignalsSetForTest()); | ||
| describe('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION is enabled', () => { | ||
| beforeAll(() => setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true)); | ||
| afterAll(() => setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false)); | ||
| /** | ||
| * The legacy validation behavior was that this check should only | ||
| * be performed for runtimes that have provided a trustedSignals set. | ||
| * However, this resulted in a bug as all object values were | ||
| * being considered signals in environments where the trustedSignals | ||
| * set had not been defined. The runtime flag has been added as a killswitch | ||
| * in case the fix needs to be reverted. | ||
| */ | ||
| it('will warn if rendering non-signal objects ', async () => { | ||
| const elm = await createElementSignalAndInsertIntoDom({ | ||
| value: 'non signal value', | ||
| }); | ||
| expect(consoleSpy.calls.warn[0][0].message).toContain( | ||
| 'Attempted to subscribe to an object that has the shape of a signal but received the following error: TypeError: signal.subscribe is not a function' | ||
| ); | ||
| expect(elm.shadowRoot.textContent).toBe('non signal value'); | ||
| }); | ||
| it('will not warn if rendering signal objects', async () => { | ||
| const signal = new Signal('signal value'); | ||
| const elm = await createElementSignalAndInsertIntoDom(signal); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| signal.value = 'new signal value'; | ||
| await Promise.resolve(); | ||
| expect(elm.shadowRoot.textContent).toBe('new signal value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION is disabled', () => { | ||
| beforeAll(() => | ||
| setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false) | ||
| ); | ||
| it('will not warn if rendering non-signal objects', async () => { | ||
| const elm = await createElementSignalAndInsertIntoDom({ | ||
| value: 'non signal value', | ||
| }); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| expect(elm.shadowRoot.textContent).toBe('non signal value'); | ||
| }); | ||
| /** | ||
| * Signals are designed to be used where trustedSignalSet has been defined via setTrustedSignalSet | ||
| * This is because checking against the set is an efficient way to determine if an object is a Signal | ||
| * This is acceptable as Signals is an internal API and designed to work where setTrustedSignalSet has been used. | ||
| * Because of this, the signal value does not change here. | ||
| * See #5347 for context. | ||
| */ | ||
| it('will not warn if rendering signal objects but it will not react', async () => { | ||
| const signal = new Signal('signal value'); | ||
| const elm = await createElementSignalAndInsertIntoDom(signal); | ||
| expect(consoleSpy.calls.warn.length).toEqual(0); | ||
| signal.value = 'new signal value'; | ||
| await Promise.resolve(); | ||
| expect(elm.shadowRoot.textContent).toBe('signal value'); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
39 changes: 39 additions & 0 deletions
39
packages/@lwc/integration-not-karma/test/signal/untrusted/x/signal/signal.js
This file contains hidden or 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,39 @@ | ||
| // Note for testing purposes the signal implementation uses LWC module resolution to simplify things. | ||
| // In production the signal will come from a 3rd party library. | ||
|
|
||
| import { addTrustedSignal } from '../../../../../helpers/signals.js'; | ||
|
|
||
| export class Signal { | ||
| subscribers = new Set(); | ||
|
|
||
| constructor(initialValue) { | ||
| this._value = initialValue; | ||
| addTrustedSignal(this); | ||
| } | ||
|
|
||
| set value(newValue) { | ||
| this._value = newValue; | ||
| this.notify(); | ||
| } | ||
|
|
||
| get value() { | ||
| return this._value; | ||
| } | ||
|
|
||
| subscribe(onUpdate) { | ||
| this.subscribers.add(onUpdate); | ||
| return () => { | ||
| this.subscribers.delete(onUpdate); | ||
| }; | ||
| } | ||
|
|
||
| notify() { | ||
| for (const subscriber of this.subscribers) { | ||
| subscriber(); | ||
| } | ||
| } | ||
|
|
||
| getSubscriberCount() { | ||
| return this.subscribers.size; | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
packages/@lwc/integration-not-karma/test/signal/untrusted/x/test/test.html
This file contains hidden or 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> | ||
| {object.value} | ||
| </template> |
5 changes: 5 additions & 0 deletions
5
packages/@lwc/integration-not-karma/test/signal/untrusted/x/test/test.js
This file contains hidden or 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 @@ | ||
| import { LightningElement, api } from 'lwc'; | ||
|
|
||
| export default class Test extends LightningElement { | ||
| @api object; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| "files": [ | ||
| { | ||
| "path": "packages/@lwc/engine-dom/dist/index.js", | ||
| "maxSize": "25.0KB" | ||
| "maxSize": "24.73KB" | ||
|
||
| }, | ||
| { | ||
| "path": "packages/@lwc/synthetic-shadow/dist/index.js", | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO
spyConsoledoesn't add any value over just using vitest'sspyOn. I want to removespyConsoleeventually, but that's a problem for later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tech debt ftw