44 * SPDX-License-Identifier: MIT
55 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66 */
7- import { describe , it , expect , vi , beforeAll , afterEach } from 'vitest' ;
8- import { setFeatureFlagForTest } from '@lwc/features' ;
7+ import { describe , it , expect , vi , beforeAll , afterEach , afterAll } from 'vitest' ;
98import { setTrustedContextSet , setContextKeys } from '@lwc/shared' ;
109import { logWarnOnce } from '../../shared/logger' ;
1110import { connectContext , disconnectContext } from '../modules/context' ;
1211
13- // Mock the logger to avoid console output during tests
12+ // Mock the logger to inspect console output during tests
1413vi . mock ( '../../shared/logger' , ( ) => ( {
1514 logWarnOnce : vi . fn ( ) ,
1615} ) ) ;
@@ -33,6 +32,17 @@ const mockVM = {
3332 renderer : mockRenderer ,
3433} as any ;
3534
35+ if ( ! ( globalThis as any ) . lwcRuntimeFlags ) {
36+ Object . defineProperty ( globalThis , 'lwcRuntimeFlags' , { value : { } } ) ;
37+ }
38+
39+ /**
40+ * Need to be able to set and reset the flags at will (lwc/features doesn't provide this)
41+ */
42+ const setFeatureFlag = ( name : string , value : boolean ) => {
43+ ( globalThis as any ) . lwcRuntimeFlags [ name ] = value ;
44+ } ;
45+
3646/**
3747 * These tests test that properties are correctly validated within the connectContext and disconnectContext
3848 * functions regardless of whether trusted context has been defined or not.
@@ -50,31 +60,35 @@ describe('context functions', () => {
5060 vi . clearAllMocks ( ) ;
5161 } ) ;
5262
63+ afterAll ( ( ) => {
64+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
65+ } ) ;
66+
5367 describe ( 'without setting trusted context' , ( ) => {
5468 it ( 'should log a warning when trustedContext is not defined and connectContext is called with legacy signal context validation' , ( ) => {
55- setFeatureFlagForTest ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , true ) ;
69+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , true ) ;
5670 connectContext ( mockVM ) ;
5771 expect ( logWarnOnce ) . toHaveBeenCalledWith (
5872 'Attempted to connect to trusted context but received the following error: component[contextfulKeys[i]][connectContext2] is not a function'
5973 ) ;
6074 } ) ;
6175
6276 it ( 'should not log a warning when trustedContext is not defined and connectContext is called with non-legacy context validation' , ( ) => {
63- setFeatureFlagForTest ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
77+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
6478 connectContext ( mockVM ) ;
6579 expect ( logWarnOnce ) . not . toHaveBeenCalled ( ) ;
6680 } ) ;
6781
6882 it ( 'should log a warning when trustedContext is not defined and disconnectContext is called with legacy signal context validation' , ( ) => {
69- setFeatureFlagForTest ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , true ) ;
83+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , true ) ;
7084 disconnectContext ( mockVM ) ;
7185 expect ( logWarnOnce ) . toHaveBeenCalledWith (
7286 'Attempted to disconnect from trusted context but received the following error: component[contextfulKeys[i]][disconnectContext2] is not a function'
7387 ) ;
7488 } ) ;
7589
7690 it ( 'should not log a warning when trustedContext is not defined and disconnectContext is called with non-legacy context validation' , ( ) => {
77- setFeatureFlagForTest ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
91+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
7892 disconnectContext ( mockVM ) ;
7993 expect ( logWarnOnce ) . not . toHaveBeenCalled ( ) ;
8094 } ) ;
@@ -83,10 +97,10 @@ describe('context functions', () => {
8397 describe ( 'with trusted context set' , ( ) => {
8498 it ( 'should not log warnings when trustedContext is defined' , ( ) => {
8599 setTrustedContextSet ( new WeakSet ( ) ) ;
86- setFeatureFlagForTest ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , true ) ;
100+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , true ) ;
87101 connectContext ( mockVM ) ;
88102 disconnectContext ( mockVM ) ;
89- setFeatureFlagForTest ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
103+ setFeatureFlag ( 'ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION' , false ) ;
90104 expect ( logWarnOnce ) . not . toHaveBeenCalled ( ) ;
91105 } ) ;
92106 } ) ;
0 commit comments