|
| 1 | +/** |
| 2 | + * Copyright 2026, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { vi } from 'vitest'; |
| 18 | +import React from 'react'; |
| 19 | +import { OptimizelyContext, ProviderStateStore, OptimizelyProvider } from '../provider/index'; |
| 20 | +import { REACT_CLIENT_META } from '../client/index'; |
| 21 | +import type { OptimizelyUserContext, OptimizelyDecision, Client } from '@optimizely/optimizely-sdk'; |
| 22 | +import type { OptimizelyContextValue } from '../provider/index'; |
| 23 | + |
| 24 | +export const MOCK_DECISION: OptimizelyDecision = { |
| 25 | + variationKey: 'variation_1', |
| 26 | + enabled: true, |
| 27 | + variables: { color: 'red' }, |
| 28 | + ruleKey: 'rule_1', |
| 29 | + flagKey: 'flag_1', |
| 30 | + userContext: {} as OptimizelyUserContext, |
| 31 | + reasons: [], |
| 32 | +}; |
| 33 | + |
| 34 | +export const MOCK_DECISIONS: Record<string, OptimizelyDecision> = { |
| 35 | + flag_1: MOCK_DECISION, |
| 36 | + flag_2: { |
| 37 | + variationKey: 'variation_2', |
| 38 | + enabled: false, |
| 39 | + variables: { size: 'large' }, |
| 40 | + ruleKey: 'rule_2', |
| 41 | + flagKey: 'flag_2', |
| 42 | + userContext: {} as OptimizelyUserContext, |
| 43 | + reasons: [], |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Creates a mock OptimizelyUserContext with all methods stubbed. |
| 49 | + * Override specific methods via the overrides parameter. |
| 50 | + */ |
| 51 | +export function createMockUserContext( |
| 52 | + overrides?: Partial<Record<string, unknown>>, |
| 53 | +): OptimizelyUserContext { |
| 54 | + return { |
| 55 | + getUserId: vi.fn().mockReturnValue('test-user'), |
| 56 | + getAttributes: vi.fn().mockReturnValue({}), |
| 57 | + fetchQualifiedSegments: vi.fn().mockResolvedValue(true), |
| 58 | + decide: vi.fn().mockReturnValue(MOCK_DECISION), |
| 59 | + decideAll: vi.fn().mockReturnValue(MOCK_DECISIONS), |
| 60 | + decideForKeys: vi.fn().mockImplementation((keys: string[]) => { |
| 61 | + const result: Record<string, OptimizelyDecision> = {}; |
| 62 | + for (const key of keys) { |
| 63 | + if (MOCK_DECISIONS[key]) { |
| 64 | + result[key] = MOCK_DECISIONS[key]; |
| 65 | + } |
| 66 | + } |
| 67 | + return result; |
| 68 | + }), |
| 69 | + setForcedDecision: vi.fn().mockReturnValue(true), |
| 70 | + getForcedDecision: vi.fn(), |
| 71 | + removeForcedDecision: vi.fn().mockReturnValue(true), |
| 72 | + removeAllForcedDecisions: vi.fn().mockReturnValue(true), |
| 73 | + trackEvent: vi.fn(), |
| 74 | + getOptimizely: vi.fn(), |
| 75 | + setQualifiedSegments: vi.fn(), |
| 76 | + getQualifiedSegments: vi.fn().mockReturnValue([]), |
| 77 | + qualifiedSegments: null, |
| 78 | + ...overrides, |
| 79 | + } as unknown as OptimizelyUserContext; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Creates a mock Optimizely Client. |
| 84 | + * @param hasConfig - If true, getOptimizelyConfig returns a config object; otherwise null. |
| 85 | + */ |
| 86 | +export function createMockClient(hasConfig = false): Client { |
| 87 | + return { |
| 88 | + getOptimizelyConfig: vi.fn().mockReturnValue(hasConfig ? { revision: '1' } : null), |
| 89 | + createUserContext: vi.fn(), |
| 90 | + onReady: vi.fn().mockResolvedValue({ success: true }), |
| 91 | + notificationCenter: {}, |
| 92 | + } as unknown as Client; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Creates a mock client with notification center support and wraps it in OptimizelyProvider. |
| 97 | + * Used for integration-style tests that need the full Provider lifecycle. |
| 98 | + */ |
| 99 | +export function createProviderWrapper(mockUserContext: OptimizelyUserContext) { |
| 100 | + let configUpdateCallback: (() => void) | undefined; |
| 101 | + |
| 102 | + const client = { |
| 103 | + getOptimizelyConfig: vi.fn().mockReturnValue({ revision: '1' }), |
| 104 | + createUserContext: vi.fn().mockReturnValue(mockUserContext), |
| 105 | + onReady: vi.fn().mockResolvedValue(undefined), |
| 106 | + isOdpIntegrated: vi.fn().mockReturnValue(false), |
| 107 | + notificationCenter: { |
| 108 | + addNotificationListener: vi.fn().mockImplementation((type: string, cb: () => void) => { |
| 109 | + if (type === 'OPTIMIZELY_CONFIG_UPDATE') { |
| 110 | + configUpdateCallback = cb; |
| 111 | + } |
| 112 | + return 1; |
| 113 | + }), |
| 114 | + removeNotificationListener: vi.fn(), |
| 115 | + }, |
| 116 | + } as unknown as Client; |
| 117 | + |
| 118 | + (client as unknown as Record<symbol, unknown>)[REACT_CLIENT_META] = { |
| 119 | + hasOdpManager: false, |
| 120 | + hasVuidManager: false, |
| 121 | + }; |
| 122 | + |
| 123 | + function Wrapper({ children }: { children: React.ReactNode }) { |
| 124 | + return ( |
| 125 | + <OptimizelyProvider client={client} user={{ id: 'user-1' }}> |
| 126 | + {children} |
| 127 | + </OptimizelyProvider> |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + wrapper: Wrapper, |
| 133 | + client, |
| 134 | + fireConfigUpdate: () => configUpdateCallback?.(), |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Creates a lightweight wrapper that provides OptimizelyContext directly |
| 140 | + * (bypassing Provider lifecycle). Used for unit tests. |
| 141 | + */ |
| 142 | +export function createWrapper(store: ProviderStateStore, client: Client) { |
| 143 | + const contextValue: OptimizelyContextValue = { store, client }; |
| 144 | + |
| 145 | + return function Wrapper({ children }: { children: React.ReactNode }) { |
| 146 | + return <OptimizelyContext.Provider value={contextValue}>{children}</OptimizelyContext.Provider>; |
| 147 | + }; |
| 148 | +} |
0 commit comments