1717import { vi , describe , it , expect , beforeEach , type MockedFunction } from 'vitest' ;
1818import { createInstance as jsCreateInstance } from '@optimizely/optimizely-sdk' ;
1919import type { Config } from '@optimizely/optimizely-sdk' ;
20- import { createInstance , CLIENT_ENGINE , CLIENT_VERSION } from './createInstance' ;
20+ import { createInstance , CLIENT_ENGINE , CLIENT_VERSION , REACT_CLIENT_META } from './createInstance' ;
21+ import type { ReactClientMeta } from './createInstance' ;
22+
23+ type ClientWithMeta = Record < symbol , ReactClientMeta > ;
24+
25+ const mockJsClient = vi . hoisted ( ( ) => ( {
26+ onReady : vi . fn ( ) . mockResolvedValue ( undefined ) ,
27+ createUserContext : vi . fn ( ) ,
28+ close : vi . fn ( ) ,
29+ } ) ) ;
2130
2231vi . mock ( '@optimizely/optimizely-sdk' , ( ) => ( {
23- createInstance : vi . fn ( ) . mockReturnValue ( {
24- onReady : vi . fn ( ) . mockResolvedValue ( undefined ) ,
25- createUserContext : vi . fn ( ) ,
26- close : vi . fn ( ) ,
27- } ) ,
32+ createInstance : vi . fn ( ) . mockReturnValue ( mockJsClient ) ,
2833} ) ) ;
2934
3035const mockedJsCreateInstance = jsCreateInstance as MockedFunction < typeof jsCreateInstance > ;
@@ -51,4 +56,51 @@ describe('createInstance', () => {
5156 ) ;
5257 } ) ;
5358 } ) ;
59+
60+ describe ( 'prototype delegation' , ( ) => {
61+ it ( 'should return an object that delegates to the JS SDK client' , ( ) => {
62+ const client = createInstance ( createTestConfig ( ) ) ;
63+ // Methods from the JS client should be accessible via prototype chain
64+ expect ( client . onReady ) . toBe ( mockJsClient . onReady ) ;
65+ expect ( client . createUserContext ) . toBe ( mockJsClient . createUserContext ) ;
66+ expect ( client . close ) . toBe ( mockJsClient . close ) ;
67+ } ) ;
68+
69+ it ( 'should have the JS SDK client as its prototype' , ( ) => {
70+ const client = createInstance ( createTestConfig ( ) ) ;
71+ expect ( Object . getPrototypeOf ( client ) ) . toBe ( mockJsClient ) ;
72+ } ) ;
73+ } ) ;
74+
75+ describe ( 'REACT_CLIENT_META' , ( ) => {
76+ it ( 'should set hasOdpManager to false when odpManager is not provided' , ( ) => {
77+ const client = createInstance ( createTestConfig ( ) ) ;
78+ const meta = ( client as unknown as ClientWithMeta ) [ REACT_CLIENT_META ] ;
79+ expect ( meta . hasOdpManager ) . toBe ( false ) ;
80+ } ) ;
81+
82+ it ( 'should set hasOdpManager to true when odpManager is provided' , ( ) => {
83+ const client = createInstance ( createTestConfig ( { odpManager : { } as Config [ 'odpManager' ] } ) ) ;
84+ const meta = ( client as unknown as ClientWithMeta ) [ REACT_CLIENT_META ] ;
85+ expect ( meta . hasOdpManager ) . toBe ( true ) ;
86+ } ) ;
87+
88+ it ( 'should set hasVuidManager to false when vuidManager is not provided' , ( ) => {
89+ const client = createInstance ( createTestConfig ( ) ) ;
90+ const meta = ( client as unknown as ClientWithMeta ) [ REACT_CLIENT_META ] ;
91+ expect ( meta . hasVuidManager ) . toBe ( false ) ;
92+ } ) ;
93+
94+ it ( 'should set hasVuidManager to true when vuidManager is provided' , ( ) => {
95+ const client = createInstance ( createTestConfig ( { vuidManager : { } as Config [ 'vuidManager' ] } ) ) ;
96+ const meta = ( client as unknown as ClientWithMeta ) [ REACT_CLIENT_META ] ;
97+ expect ( meta . hasVuidManager ) . toBe ( true ) ;
98+ } ) ;
99+
100+ it ( 'should store meta on the react client, not on the prototype' , ( ) => {
101+ const client = createInstance ( createTestConfig ( ) ) ;
102+ expect ( Object . prototype . hasOwnProperty . call ( client , REACT_CLIENT_META ) ) . toBe ( true ) ;
103+ expect ( Object . prototype . hasOwnProperty . call ( mockJsClient , REACT_CLIENT_META ) ) . toBe ( false ) ;
104+ } ) ;
105+ } ) ;
54106} ) ;
0 commit comments