1
1
// Sample test
2
2
import { getFlagsmith } from './test-constants' ;
3
- import { IFlagsmith } from '../types' ;
3
+ import { IFlagsmith , IFlagsmithFeature } from '../types' ;
4
4
5
5
describe ( 'Flagsmith Types' , ( ) => {
6
6
@@ -20,13 +20,45 @@ describe('Flagsmith Types', () => {
20
20
typedFlagsmith . getValue ( "flag2" )
21
21
} ) ;
22
22
test ( 'should allow supplying interface generics to a flagsmith instance' , async ( ) => {
23
- const { flagsmith, } = getFlagsmith ( { } ) ;
23
+ const { flagsmith } = getFlagsmith ( { } ) ;
24
24
const typedFlagsmith = flagsmith as IFlagsmith <
25
25
{
26
26
stringFlag : string
27
27
numberFlag : number
28
28
objectFlag : { first_name : string }
29
29
} >
30
+ typedFlagsmith . init ( {
31
+ environmentID : "test" ,
32
+ defaultFlags : {
33
+ stringFlag : {
34
+ id : 1 ,
35
+ enabled : true ,
36
+ value : "string_value"
37
+ } ,
38
+ numberFlag : {
39
+ id : 2 ,
40
+ enabled : true ,
41
+ value : 123
42
+ } ,
43
+ objectFlag : {
44
+ id : 3 ,
45
+ enabled : true ,
46
+ value : JSON . stringify ( { first_name : "John" } )
47
+ }
48
+ } ,
49
+ onChange : ( previousFlags ) => {
50
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
51
+ const previousStringFlag = previousFlags ?. stringFlag
52
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
53
+ const previousNumberFlag = previousFlags ?. numberFlag
54
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
55
+ const previousObjectFlag = previousFlags ?. objectFlag
56
+ //@ts -expect-error - flag does not exist
57
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
58
+ const previousNonExistingFlag = previousFlags ?. nonExistingFlag
59
+ }
60
+ } )
61
+
30
62
//@ts -expect-error - feature not defined
31
63
typedFlagsmith . hasFeature ( "fail" )
32
64
//@ts -expect-error - feature not defined
@@ -36,6 +68,18 @@ describe('Flagsmith Types', () => {
36
68
typedFlagsmith . hasFeature ( "numberFlag" )
37
69
typedFlagsmith . getValue ( "stringFlag" )
38
70
typedFlagsmith . getValue ( "numberFlag" )
71
+
72
+ const typedFlags = await typedFlagsmith . getAllFlags ( )
73
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
74
+ const asString = typedFlags . stringFlag
75
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
76
+ const asNumber = typedFlags . numberFlag
77
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
78
+ const asObject = typedFlags . objectFlag
79
+
80
+ // @ts -expect-error - invalid does not exist on type
81
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
82
+ const asNonExisting = typedFlags . nonExistingFlag
39
83
40
84
//eslint-disable-next-line @typescript-eslint/no-unused-vars
41
85
const stringFlag : string | null = typedFlagsmith . getValue ( "stringFlag" )
@@ -44,7 +88,7 @@ describe('Flagsmith Types', () => {
44
88
//eslint-disable-next-line @typescript-eslint/no-unused-vars
45
89
const firstName : string | undefined = typedFlagsmith . getValue ( "objectFlag" ) ?. first_name
46
90
47
- // @ts -expect-error - invalid does not exist on type announcement
91
+ // @ts -expect-error - invalid does not exist on type
48
92
//eslint-disable-next-line @typescript-eslint/no-unused-vars
49
93
const invalidPointer : string = typedFlagsmith . getValue ( "objectFlag" ) ?. invalid
50
94
0 commit comments