launchdarkly / jest-launchdarkly-mock

Easily unit test LaunchDarkly feature flagged components with jest
Apache License 2.0
25 stars 19 forks source link

Fix mock signature for LDProvider #31

Closed jamiehaywood closed 2 years ago

jamiehaywood commented 2 years ago

Is this a support request? This issue tracker is maintained by LaunchDarkly SDK developers and is intended for feedback on the SDK code. If you're not sure whether the problem you are having is specifically related to the SDK, or to the LaunchDarkly service overall, it may be more appropriate to contact the LaunchDarkly support team; they can help to investigate the problem and will consult the SDK team if necessary. You can submit a support request by going here or by emailing support@launchdarkly.com.

Note that issues filed on this issue tracker are publicly accessible. Do not provide any private account information on your issues. If your problem is specific to your account, you should submit a support request as described above.

Describe the bug The mock signature for the LDProvider component is not correct, and causes tests to fail with Objects are not valid as a React child (found: object with keys {clientSideID, options, children}). in some test scenarios.

To reproduce

const ldConfig: ProviderConfig = {
  clientSideID: process.env.LAUNCHDARKLY_CLIENT_ID,
  options: { bootstrap: 'localStorage', fetchGoals: false },
}

render(<LDProvider {...ldConfig}><p>test</p></LDProvider>)

Expected behavior No error to occur when using the jest-launchdarkly-mock

Logs If applicable, add any log output related to your problem.

SDK version 1.0.5

Language version, developer tools JavaScript / TypeScript 4.7.4, Jest

OS/platform MacOS 12.4

Additional context

jamiehaywood commented 2 years ago

The fix is fairly trivial, changing https://github.com/launchdarkly/jest-launchdarkly-mock/blob/1c04fc4a1f871e24059d8a395109eeee3effeceb/src/index.ts#L53 to

mockLDProvider.mockImplementation((props: any) => props.children)

which is exactly the same as the mockAsyncWithLDProvider minus the Promise.resolve. https://github.com/launchdarkly/jest-launchdarkly-mock/blob/1c04fc4a1f871e24059d8a395109eeee3effeceb/src/index.ts#L52