Hi there! :wave: Can someone please point me in th...
# ask-questions
h
Hi there! 👋 Can someone please point me in the direction of docs for general advice on mocking feature states in a React app? I want to my components for when a flag is on or off, for instance.
1
r
@blue-sundown-32899 has marked the issue as closed. Reopen Issue button
Our official support hours are 9am - 5pm Pacific Time, Monday through Friday. You may occasionally hear from us outside of these hours. Your support request has been logged in our system. Our support team will get back to you very soon!
f
you can use the devtools
or you can set some specific rules for you
h
Hi Graham, this is for automated tests. What I ended up doing was create a test-specific GrowthBookProvider that uses a mocked client. Then I exposed the client to my tests so that I can call the
setFeatures()
as I need them for my specific test cases.
Basically like this:
Copy code
import {
  FeatureDefinition,
  GrowthBook,
  GrowthBookProvider,
} from '@growthbook/growthbook-react'

type FeaturesClient = GrowthBook<Record<string, unknown>> & {
  patchFeatures: (features: Record<string, FeatureDefinition<unknown>>) => void
}

export const featuresClient: FeaturesClient = Object.assign(
  createFeaturesClient({
    apiHost: '',
    clientKey: '',
    enableDevMode: false,
    subscribeToChanges: false,
    features: {},
  }),
  {
    patchFeatures(features: Record<string, FeatureDefinition<unknown>>) {
      featuresClient.setFeatures({
        ...featuresClient.getFeatures(),
        ...features,
      })
    },
  }
)

export const FeaturesProviderMock = ({
  children,
}: {
  children: React.ReactNode
}) => {
  return (
    <GrowthBookProvider growthbook={featuresClient}>
      {children}
    </GrowthBookProvider>
  )
}
The
createFeaturesClient
function is a small wrapper around
new GrowthBook()
. From my tests, I can then call
featuresClient.patchFeatures({ 'feature-name': { defaultValue: true } })
There is likely a nicer way to do it, but otherwise, that might help others as well.
r
Hi JP, I talked this over with one of our Engineers and he said that your approach is just fine. GrowthBook isn't opinionated about testing yet, nor do we have any specific suggestions in this regard. That could change in the future as our user base grows and we get more interest in the testing of our SDKs.
h
Hi August, I appreciate that you did that.
r
Yes of course, happy to help! Is there anything else related to testing that I can help with on this thread? If not, I'll go ahead and close the ticket on our side. You can always open a new support request by posting again in #ask-questions.
h
Sorry, I didn't see your reply. You can go ahead and close this ticket on your side. Thank you