@@ -2,6 +2,7 @@ import React from 'react';
2
2
import { render } from '@testing-library/react' ;
3
3
import { FlagsmithProvider , useFlags , useFlagsmith } from '../lib/flagsmith/react' ;
4
4
import { getFlagsmith , } from './test-constants' ;
5
+ import { IFlagsmithTrait } from '../types' ;
5
6
6
7
7
8
describe . only ( 'FlagsmithProvider' , ( ) => {
@@ -51,13 +52,16 @@ describe.only('FlagsmithProvider', () => {
51
52
} ) ;
52
53
it ( 'should allow supplying interface generics to useFlags' , ( ) => {
53
54
const FlagsmithPage = ( ) => {
54
- const typedFlagsmith = useFlags <
55
- {
56
- stringFlag : string
57
- numberFlag : number
58
- objectFlag : { first_name : string }
59
- }
60
- > ( [ ] )
55
+ interface MyFeatureInterface {
56
+ stringFlag : string
57
+ numberFlag : number
58
+ objectFlag : { first_name : string }
59
+ }
60
+ const typedFlagsmith = useFlags < MyFeatureInterface > ( [ "stringFlag" , "numberFlag" , "objectFlag" ] )
61
+
62
+ // @ts -expect-error - feature not defined
63
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
64
+ const wrongTypedFlagsmith = useFlags < MyFeatureInterface > ( [ "non-existing-flag" ] )
61
65
//@ts -expect-error - feature not defined
62
66
typedFlagsmith . fail ?. enabled
63
67
//@ts -expect-error - feature not defined
@@ -72,11 +76,11 @@ describe.only('FlagsmithProvider', () => {
72
76
//eslint-disable-next-line @typescript-eslint/no-unused-vars
73
77
const numberFlag : number = typedFlagsmith . numberFlag ?. value
74
78
//eslint-disable-next-line @typescript-eslint/no-unused-vars
75
- const firstName : string = typedFlagsmith . objectFlag ?. value . first_name
79
+ const firstName : string = typedFlagsmith . objectFlag ?. value ? .first_name
76
80
77
81
// @ts -expect-error - invalid does not exist on type announcement
78
82
//eslint-disable-next-line @typescript-eslint/no-unused-vars
79
- const invalidPointer : string = typedFlagsmith . objectFlag ?. value . invalid
83
+ const invalidPointer : string = typedFlagsmith . objectFlag ?. value ? .invalid
80
84
81
85
// @ts -expect-error - feature should be a number
82
86
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -92,4 +96,30 @@ describe.only('FlagsmithProvider', () => {
92
96
</ FlagsmithProvider >
93
97
) ;
94
98
} ) ;
99
+ it ( 'should allow supplying string type to useFlags' , ( ) => {
100
+ const FlagsmithPage = ( ) => {
101
+ type StringTypes = "stringFlag" | "numberFlag" | "objectFlag"
102
+ const typedFlagsmith = useFlags < StringTypes > ( [ "stringFlag" , "numberFlag" , "objectFlag" ] )
103
+
104
+ // @ts -expect-error - feature not defined
105
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
106
+ const wrongTypedFlagsmith = useFlags < StringTypes > ( [ "non-existing-flag" ] )
107
+
108
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
109
+ const stringFlag : IFlagsmithTrait = typedFlagsmith . stringFlag
110
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
111
+ const numberFlag : IFlagsmithTrait = typedFlagsmith . numberFlag
112
+ //eslint-disable-next-line @typescript-eslint/no-unused-vars
113
+ const firstName : IFlagsmithTrait = typedFlagsmith . objectFlag
114
+
115
+ return < > </ >
116
+ }
117
+ const onChange = jest . fn ( ) ;
118
+ const { flagsmith, initConfig, mockFetch} = getFlagsmith ( { onChange} )
119
+ render (
120
+ < FlagsmithProvider flagsmith = { flagsmith } options = { initConfig } >
121
+ < FlagsmithPage />
122
+ </ FlagsmithProvider >
123
+ ) ;
124
+ } ) ;
95
125
} ) ;
0 commit comments