11import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
2- import { createFlagsmithAdapter } from '.' ;
2+ import { flagsmithAdapter } from '.' ;
33import flagsmith , { IFlagsmithFeature , IState } from 'flagsmith' ;
44
55// Mock the flagsmith module
@@ -14,26 +14,24 @@ vi.mock('flagsmith', () => ({
1414describe ( 'Flagsmith Adapter' , ( ) => {
1515 beforeEach ( ( ) => {
1616 vi . resetAllMocks ( ) ;
17+ process . env . FLAGSMITH_ENVIRONMENT_ID = 'test-key' ;
1718 } ) ;
1819
1920 afterEach ( ( ) => {
2021 vi . mocked ( flagsmith . init ) . mockClear ( ) ;
22+ delete process . env . FLAGSMITH_ENVIRONMENT_ID ;
2123 vi . clearAllMocks ( ) ;
2224 } ) ;
2325
2426 it ( 'should initialize the adapter' , async ( ) => {
25- const adapter = createFlagsmithAdapter ( {
26- environmentID : 'test-key' ,
27- } ) ;
27+ const adapter = flagsmithAdapter . getFeature ( ) ;
2828
2929 expect ( adapter ) . toBeDefined ( ) ;
3030 expect ( adapter . decide ) . toBeDefined ( ) ;
3131 } ) ;
3232
3333 it ( 'should initialize Flagsmith client when deciding flag value' , async ( ) => {
34- const adapter = createFlagsmithAdapter < IFlagsmithFeature , any > ( {
35- environmentID : 'test-key' ,
36- } ) ;
34+ const adapter = flagsmithAdapter . getFeature ( ) ;
3735
3836 // Mock getState to return a specific flag value
3937 const mockFlag : IFlagsmithFeature = {
@@ -57,16 +55,14 @@ describe('Flagsmith Adapter', () => {
5755 } ) ;
5856
5957 expect ( flagsmith . init ) . toHaveBeenCalledWith ( {
60- environmentID : 'test-key' ,
6158 fetch : expect . any ( Function ) ,
59+ environmentID : process . env . FLAGSMITH_ENVIRONMENT_ID ,
6260 } ) ;
6361 expect ( value ) . toEqual ( mockFlag ) ;
6462 } ) ;
6563
6664 it ( 'should return default value when flag is not found' , async ( ) => {
67- const adapter = createFlagsmithAdapter < IFlagsmithFeature , any > ( {
68- environmentID : 'test-key' ,
69- } ) ;
65+ const adapter = flagsmithAdapter . getFeature ( ) ;
7066
7167 // Mock getState to return empty flags
7268 vi . mocked ( flagsmith . getState ) . mockReturnValue ( {
@@ -91,9 +87,7 @@ describe('Flagsmith Adapter', () => {
9187 } ) ;
9288
9389 it ( 'should reuse initialized Flagsmith client' , async ( ) => {
94- const adapter = createFlagsmithAdapter < IFlagsmithFeature , any > ( {
95- environmentID : 'test-key' ,
96- } ) ;
90+ const adapter = flagsmithAdapter . getFeature ( ) ;
9791
9892 // Set Flagsmith as already initialized
9993 vi . mocked ( flagsmith ) . initialised = true ;
@@ -115,8 +109,7 @@ describe('Flagsmith Adapter', () => {
115109 } ) ;
116110
117111 it ( 'should handle additional Flagsmith configuration options' , async ( ) => {
118- const adapter = createFlagsmithAdapter < IFlagsmithFeature , any > ( {
119- environmentID : 'test-key' ,
112+ const adapter = flagsmithAdapter . getFeature ( {
120113 api : 'https://custom-api.com' ,
121114 enableLogs : true ,
122115 } ) ;
@@ -131,10 +124,63 @@ describe('Flagsmith Adapter', () => {
131124 } ) ;
132125
133126 expect ( flagsmith . init ) . toHaveBeenCalledWith ( {
134- environmentID : 'test-key' ,
135127 api : 'https://custom-api.com' ,
136128 enableLogs : true ,
129+ environmentID : process . env . FLAGSMITH_ENVIRONMENT_ID ,
130+ fetch : expect . any ( Function ) ,
131+ } ) ;
132+ } ) ;
133+
134+ it ( 'should handle manually set environmentID' , async ( ) => {
135+ const adapter = flagsmithAdapter . getFeature ( {
136+ environmentID : 'custom-env-id' ,
137+ } ) ;
138+
139+ vi . mocked ( flagsmith ) . initialised = false ;
140+
141+ await adapter . decide ( {
142+ key : 'test-flag' ,
143+ entities : undefined ,
144+ headers : { } as any ,
145+ cookies : { } as any ,
146+ } ) ;
147+
148+ expect ( flagsmith . init ) . toHaveBeenCalledWith ( {
149+ environmentID : 'custom-env-id' ,
150+ fetch : expect . any ( Function ) ,
151+ } ) ;
152+ } ) ;
153+
154+ it ( 'should retrieve the feature flag value using the decide method' , async ( ) => {
155+ const mockFlag : IFlagsmithFeature = {
156+ enabled : true ,
157+ value : 'mocked-value' ,
158+ } ;
159+
160+ vi . mocked ( flagsmith . getState ) . mockReturnValue ( {
161+ flags : {
162+ 'my-feature' : mockFlag ,
163+ } ,
164+ api : 'https://api.flagsmith.com/api/v1/' ,
165+ } as IState < string > ) ;
166+
167+ // Mock Flagsmith as not initialized
168+ vi . mocked ( flagsmith ) . initialised = false ;
169+
170+ const adapter = flagsmithAdapter . getFeature ( ) ;
171+
172+ const myFeatureFlag = await adapter . decide ( {
173+ key : 'my-feature' ,
174+ defaultValue : { enabled : false , value : 'default-value' } ,
175+ entities : undefined ,
176+ headers : { } as any ,
177+ cookies : { } as any ,
178+ } ) ;
179+
180+ expect ( flagsmith . init ) . toHaveBeenCalledWith ( {
181+ environmentID : process . env . FLAGSMITH_ENVIRONMENT_ID ,
137182 fetch : expect . any ( Function ) ,
138183 } ) ;
184+ expect ( myFeatureFlag ) . toEqual ( mockFlag ) ;
139185 } ) ;
140186} ) ;
0 commit comments