|
| 1 | +import React from 'react'; |
| 2 | +import {render} from '@testing-library/react'; |
| 3 | +import {FlagsmithProvider, useFlags, useFlagsmith} from '../lib/flagsmith/react'; |
| 4 | +import {getFlagsmith,} from './test-constants'; |
| 5 | + |
| 6 | + |
| 7 | +describe.only('FlagsmithProvider', () => { |
| 8 | + it('should allow supplying interface generics to useFlagsmith', () => { |
| 9 | + const FlagsmithPage = ()=> { |
| 10 | + const typedFlagsmith = useFlagsmith< |
| 11 | + { |
| 12 | + stringFlag: string |
| 13 | + numberFlag: number |
| 14 | + objectFlag: { first_name: string } |
| 15 | + } |
| 16 | + >() |
| 17 | + //@ts-expect-error - feature not defined |
| 18 | + typedFlagsmith.hasFeature("fail") |
| 19 | + //@ts-expect-error - feature not defined |
| 20 | + typedFlagsmith.getValue("fail") |
| 21 | + |
| 22 | + typedFlagsmith.hasFeature("stringFlag") |
| 23 | + typedFlagsmith.hasFeature("numberFlag") |
| 24 | + typedFlagsmith.getValue("stringFlag") |
| 25 | + typedFlagsmith.getValue("numberFlag") |
| 26 | + |
| 27 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 28 | + const stringFlag: string|null = typedFlagsmith.getValue("stringFlag") |
| 29 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 30 | + const numberFlag: number|null = typedFlagsmith.getValue("numberFlag") |
| 31 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 32 | + const firstName: string | undefined = typedFlagsmith.getValue("objectFlag")?.first_name |
| 33 | + |
| 34 | + // @ts-expect-error - invalid does not exist on type announcement |
| 35 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 36 | + const invalidPointer: string | undefined = typedFlagsmith.getValue("objectFlag")?.invalid |
| 37 | + |
| 38 | + // @ts-expect-error - feature should be a number |
| 39 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 40 | + const incorrectNumberFlag: string = typedFlagsmith.getValue("numberFlag") |
| 41 | + |
| 42 | + return <></> |
| 43 | + } |
| 44 | + const onChange = jest.fn(); |
| 45 | + const {flagsmith,initConfig, mockFetch} = getFlagsmith({onChange}) |
| 46 | + render( |
| 47 | + <FlagsmithProvider flagsmith={flagsmith} options={initConfig}> |
| 48 | + <FlagsmithPage/> |
| 49 | + </FlagsmithProvider> |
| 50 | + ); |
| 51 | + }); |
| 52 | + it('should allow supplying interface generics to useFlags', () => { |
| 53 | + const FlagsmithPage = ()=> { |
| 54 | + const typedFlagsmith = useFlags< |
| 55 | + { |
| 56 | + stringFlag: string |
| 57 | + numberFlag: number |
| 58 | + objectFlag: { first_name: string } |
| 59 | + } |
| 60 | + >([]) |
| 61 | + //@ts-expect-error - feature not defined |
| 62 | + typedFlagsmith.fail?.enabled |
| 63 | + //@ts-expect-error - feature not defined |
| 64 | + typedFlagsmith.fail?.value |
| 65 | + |
| 66 | + typedFlagsmith.numberFlag |
| 67 | + typedFlagsmith.stringFlag |
| 68 | + typedFlagsmith.objectFlag |
| 69 | + |
| 70 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 71 | + const stringFlag: string = typedFlagsmith.stringFlag?.value |
| 72 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 73 | + const numberFlag: number = typedFlagsmith.numberFlag?.value |
| 74 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 75 | + const firstName: string = typedFlagsmith.objectFlag?.value.first_name |
| 76 | + |
| 77 | + // @ts-expect-error - invalid does not exist on type announcement |
| 78 | + //eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 79 | + const invalidPointer: string = typedFlagsmith.objectFlag?.value.invalid |
| 80 | + |
| 81 | + // @ts-expect-error - feature should be a number |
| 82 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 83 | + const incorrectNumberFlag: string = typedFlagsmith.numberFlag?.value |
| 84 | + |
| 85 | + return <></> |
| 86 | + } |
| 87 | + const onChange = jest.fn(); |
| 88 | + const {flagsmith,initConfig, mockFetch} = getFlagsmith({onChange}) |
| 89 | + render( |
| 90 | + <FlagsmithProvider flagsmith={flagsmith} options={initConfig}> |
| 91 | + <FlagsmithPage/> |
| 92 | + </FlagsmithProvider> |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments