@@ -2,6 +2,7 @@ import React from 'react';
22import { render } from '@testing-library/react' ;
33import { FlagsmithProvider , useFlags , useFlagsmith } from '../lib/flagsmith/react' ;
44import { getFlagsmith , } from './test-constants' ;
5+ import { IFlagsmithTrait } from '../types' ;
56
67
78describe . only ( 'FlagsmithProvider' , ( ) => {
@@ -51,13 +52,16 @@ describe.only('FlagsmithProvider', () => {
5152 } ) ;
5253 it ( 'should allow supplying interface generics to useFlags' , ( ) => {
5354 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" ] )
6165 //@ts -expect-error - feature not defined
6266 typedFlagsmith . fail ?. enabled
6367 //@ts -expect-error - feature not defined
@@ -72,11 +76,11 @@ describe.only('FlagsmithProvider', () => {
7276 //eslint-disable-next-line @typescript-eslint/no-unused-vars
7377 const numberFlag : number = typedFlagsmith . numberFlag ?. value
7478 //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
7680
7781 // @ts -expect-error - invalid does not exist on type announcement
7882 //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
8084
8185 // @ts -expect-error - feature should be a number
8286 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -92,4 +96,30 @@ describe.only('FlagsmithProvider', () => {
9296 </ FlagsmithProvider >
9397 ) ;
9498 } ) ;
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+ } ) ;
95125} ) ;
0 commit comments