|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 8 | +import { setFeatureFlagForTest } from '@lwc/features'; |
| 9 | +import { setTrustedContextSet, setContextKeys } from '@lwc/shared'; |
| 10 | +import { logWarnOnce } from '../../shared/logger'; |
| 11 | +import { connectContext, disconnectContext } from '../modules/context'; |
| 12 | + |
| 13 | +// Mock the logger to avoid console output during tests |
| 14 | +vi.mock('../../shared/logger', () => ({ |
| 15 | + logWarnOnce: vi.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +// Create mock component with a regular, non-contextful property |
| 19 | +const mockComponent = {}; |
| 20 | +Object.setPrototypeOf(mockComponent, { |
| 21 | + regularProp: 'not contextful', |
| 22 | +}); |
| 23 | + |
| 24 | +// Create mock renderer |
| 25 | +const mockRenderer = { |
| 26 | + registerContextProvider: vi.fn(), |
| 27 | +}; |
| 28 | + |
| 29 | +// Create mock VM |
| 30 | +const mockVM = { |
| 31 | + component: mockComponent, |
| 32 | + elm: null, |
| 33 | + renderer: mockRenderer, |
| 34 | +} as any; |
| 35 | + |
| 36 | +/** |
| 37 | + * These tests test that properties are correctly validated within the connectContext and disconnectContext |
| 38 | + * functions regardless of whether trusted context has been defined or not. |
| 39 | + * Integration tests have been used for extensive coverage of the LWC context feature, but this particular |
| 40 | + * scenario is best isolated and unit tested as it involves manipulation of the trusted context API. |
| 41 | + */ |
| 42 | +describe('context functions', () => { |
| 43 | + beforeAll(() => { |
| 44 | + const connectContext = Symbol('connectContext'); |
| 45 | + const disconnectContext = Symbol('disconnectContext'); |
| 46 | + setContextKeys({ connectContext, disconnectContext }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('without setting trusted context', () => { |
| 54 | + 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); |
| 56 | + connectContext(mockVM); |
| 57 | + expect(logWarnOnce).toHaveBeenCalledWith( |
| 58 | + 'Attempted to connect to trusted context but received the following error: component[contextfulKeys[i]][connectContext2] is not a function' |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + 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); |
| 64 | + connectContext(mockVM); |
| 65 | + expect(logWarnOnce).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + 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); |
| 70 | + disconnectContext(mockVM); |
| 71 | + expect(logWarnOnce).toHaveBeenCalledWith( |
| 72 | + 'Attempted to disconnect from trusted context but received the following error: component[contextfulKeys[i]][disconnectContext2] is not a function' |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + 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); |
| 78 | + disconnectContext(mockVM); |
| 79 | + expect(logWarnOnce).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('with trusted context set', () => { |
| 84 | + it('should not log warnings when trustedContext is defined', () => { |
| 85 | + setTrustedContextSet(new WeakSet()); |
| 86 | + setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true); |
| 87 | + connectContext(mockVM); |
| 88 | + disconnectContext(mockVM); |
| 89 | + setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false); |
| 90 | + expect(logWarnOnce).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments