1
1
import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
2
- import { createFlagsmithAdapter } from '.' ;
2
+ import { flagsmithAdapter } from '.' ;
3
3
import flagsmith , { IFlagsmithFeature , IState } from 'flagsmith' ;
4
4
5
5
// Mock the flagsmith module
@@ -14,26 +14,24 @@ vi.mock('flagsmith', () => ({
14
14
describe ( 'Flagsmith Adapter' , ( ) => {
15
15
beforeEach ( ( ) => {
16
16
vi . resetAllMocks ( ) ;
17
+ process . env . FLAGSMITH_ENVIRONMENT_ID = 'test-key' ;
17
18
} ) ;
18
19
19
20
afterEach ( ( ) => {
20
21
vi . mocked ( flagsmith . init ) . mockClear ( ) ;
22
+ delete process . env . FLAGSMITH_ENVIRONMENT_ID ;
21
23
vi . clearAllMocks ( ) ;
22
24
} ) ;
23
25
24
26
it ( 'should initialize the adapter' , async ( ) => {
25
- const adapter = createFlagsmithAdapter ( {
26
- environmentID : 'test-key' ,
27
- } ) ;
27
+ const adapter = flagsmithAdapter . getFeature ( ) ;
28
28
29
29
expect ( adapter ) . toBeDefined ( ) ;
30
30
expect ( adapter . decide ) . toBeDefined ( ) ;
31
31
} ) ;
32
32
33
33
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 ( ) ;
37
35
38
36
// Mock getState to return a specific flag value
39
37
const mockFlag : IFlagsmithFeature = {
@@ -57,16 +55,14 @@ describe('Flagsmith Adapter', () => {
57
55
} ) ;
58
56
59
57
expect ( flagsmith . init ) . toHaveBeenCalledWith ( {
60
- environmentID : 'test-key' ,
61
58
fetch : expect . any ( Function ) ,
59
+ environmentID : process . env . FLAGSMITH_ENVIRONMENT_ID ,
62
60
} ) ;
63
61
expect ( value ) . toEqual ( mockFlag ) ;
64
62
} ) ;
65
63
66
64
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 ( ) ;
70
66
71
67
// Mock getState to return empty flags
72
68
vi . mocked ( flagsmith . getState ) . mockReturnValue ( {
@@ -91,9 +87,7 @@ describe('Flagsmith Adapter', () => {
91
87
} ) ;
92
88
93
89
it ( 'should reuse initialized Flagsmith client' , async ( ) => {
94
- const adapter = createFlagsmithAdapter < IFlagsmithFeature , any > ( {
95
- environmentID : 'test-key' ,
96
- } ) ;
90
+ const adapter = flagsmithAdapter . getFeature ( ) ;
97
91
98
92
// Set Flagsmith as already initialized
99
93
vi . mocked ( flagsmith ) . initialised = true ;
@@ -115,8 +109,7 @@ describe('Flagsmith Adapter', () => {
115
109
} ) ;
116
110
117
111
it ( 'should handle additional Flagsmith configuration options' , async ( ) => {
118
- const adapter = createFlagsmithAdapter < IFlagsmithFeature , any > ( {
119
- environmentID : 'test-key' ,
112
+ const adapter = flagsmithAdapter . getFeature ( {
120
113
api : 'https://custom-api.com' ,
121
114
enableLogs : true ,
122
115
} ) ;
@@ -131,10 +124,63 @@ describe('Flagsmith Adapter', () => {
131
124
} ) ;
132
125
133
126
expect ( flagsmith . init ) . toHaveBeenCalledWith ( {
134
- environmentID : 'test-key' ,
135
127
api : 'https://custom-api.com' ,
136
128
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 ,
137
182
fetch : expect . any ( Function ) ,
138
183
} ) ;
184
+ expect ( myFeatureFlag ) . toEqual ( mockFlag ) ;
139
185
} ) ;
140
186
} ) ;
0 commit comments