Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/renovate-e1b8baf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@scaleway/use-growthbook': patch
---

Updated dependency `@growthbook/growthbook-react` to `0.18.0`.
2 changes: 1 addition & 1 deletion packages/use-growthbook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"feature flags"
],
"dependencies": {
"@growthbook/growthbook-react": "0.17.0"
"@growthbook/growthbook-react": "0.18.0"
},
"devDependencies": {
"react": "18.2.0"
Expand Down
24 changes: 5 additions & 19 deletions packages/use-growthbook/src/AbTestProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
import type { Context } from '@growthbook/growthbook-react'
import { GrowthBook, GrowthBookProvider } from '@growthbook/growthbook-react'
import { type ReactNode, useCallback, useEffect, useMemo } from 'react'
import type { Attributes, GrowthBookType, LoadConfig } from './types'
import type { Attributes, LoadConfig } from './types'

export type ToolConfig = {
apiHost: string
clientKey: string
enableDevMode: boolean
}

export type TrackingCallback = (
experiment: { key: string },
result: { key: string },
) => void

// TODO: use type from growthbook when it's typed will be export correctly
// export type TrackingCallback = NonNullable<Context['trackingCallback']>
export type TrackingCallback = NonNullable<Context['trackingCallback']>

export type AbTestProviderProps = {
children: ReactNode
Expand All @@ -38,7 +28,7 @@ const getGrowthBookInstance = ({
config: ToolConfig
attributes: Attributes
trackingCallback: TrackingCallback
}): GrowthBookType =>
}) =>
new GrowthBook({
apiHost,
clientKey,
Expand All @@ -60,7 +50,7 @@ export const AbTestProvider = ({
attributes,
loadConfig = defaultLoadConfig,
}: AbTestProviderProps) => {
const growthbook: GrowthBookType = useMemo(
const growthbook = useMemo(
() => getGrowthBookInstance({ config, attributes, trackingCallback }),
[trackingCallback, config, attributes],
)
Expand All @@ -79,7 +69,3 @@ export const AbTestProvider = ({
<GrowthBookProvider growthbook={growthbook}>{children}</GrowthBookProvider>
)
}
/* eslint-enable @typescript-eslint/no-unsafe-call */
/* eslint-enable @typescript-eslint/no-unsafe-return */
/* eslint-enable @typescript-eslint/no-unsafe-assignment */
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { jest } from '@jest/globals'
import type { ReactNode } from 'react'

const GrowthBook = jest.fn(() => ({
loadFeatures: jest.fn(),
}))

const GrowthBookProvider = ({ children }: { children: ReactNode }) => children

const useGrowthBook = jest.fn()

export { GrowthBook, GrowthBookProvider, useGrowthBook }
11 changes: 2 additions & 9 deletions packages/use-growthbook/src/__tests__/AbTestProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
import { GrowthBook } from '@growthbook/growthbook-react'
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
import { render } from '@testing-library/react'
import type { TrackingCallback } from '../AbTestProvider'
import { AbTestProvider } from '../AbTestProvider'
import type { GrowthBookType } from '../types'

jest.mock('@growthbook/growthbook-react')
const mockGrowthBook = GrowthBook as jest.MockedClass<GrowthBookType>

describe('AbTestProvider', () => {
let trackingCallback: TrackingCallback
Expand Down Expand Up @@ -35,8 +30,7 @@ describe('AbTestProvider', () => {
</AbTestProvider>,
)

expect(mockGrowthBook).toBeCalled()
expect(mockGrowthBook.mock.instances[0]?.loadFeatures).toBeCalled()
expect(GrowthBook).toBeCalled()
})

it('should not init GrowthBook when client key is not defined', () => {
Expand All @@ -57,7 +51,6 @@ describe('AbTestProvider', () => {
</AbTestProvider>,
)

expect(mockGrowthBook).toBeCalled()
expect(mockGrowthBook.mock.instances[0]?.loadFeatures).not.toBeCalled()
expect(GrowthBook).toBeCalled()
})
})
7 changes: 3 additions & 4 deletions packages/use-growthbook/src/__tests__/useAbTestAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
import { useGrowthBook } from '@growthbook/growthbook-react'
import * as growthbook from '@growthbook/growthbook-react'
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
import { renderHook } from '@testing-library/react'
import type { Attributes } from '../types'
Expand All @@ -16,10 +15,10 @@ describe('useAbTestAttributes', () => {
.fn()
.mockReturnValue({ foo: 'bar' }) as () => Attributes
setAttributes = jest.fn() as (attributes: Attributes) => null | undefined
;(useGrowthBook as jest.Mock).mockReturnValue({
jest.spyOn(growthbook, 'useGrowthBook').mockReturnValue({
getAttributes,
setAttributes,
})
} as ReturnType<typeof growthbook.useGrowthBook>)
})

it('should allow to get attributes from GrowthBook', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/use-growthbook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ export {
withRunExperiment,
useFeatureIsOn,
useFeatureValue,
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
} from '@growthbook/growthbook-react'
export type {
FeatureString,
FeaturesReady,
IfFeatureEnabled,
Context,
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
} from '@growthbook/growthbook-react'
export { useAbTestAttributes } from './useAbTestAttributes'
export { AbTestProvider } from './AbTestProvider'
7 changes: 0 additions & 7 deletions packages/use-growthbook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ export type LoadConfig = {
autoRefresh: boolean
timeout: number
}

export type GrowthBookType = {
new (...args: unknown[]): GrowthBookType
getAttributes: () => Attributes
loadFeatures: ({ autoRefresh, timeout }: LoadConfig) => Promise<void>
setAttributes: (attributes: Attributes) => void
}
8 changes: 2 additions & 6 deletions packages/use-growthbook/src/useAbTestAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-unsafe-call */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error TODO: remove once Growthbook is correctly typed and export
import { useGrowthBook } from '@growthbook/growthbook-react'
import { useCallback, useMemo } from 'react'
import type { Attributes, GrowthBookType } from './types'
import type { Attributes } from './types'

export const useAbTestAttributes = (): [
Attributes,
(attributes: Attributes) => void,
] => {
const growthBook = useGrowthBook() as GrowthBookType | null
const growthBook = useGrowthBook()

const attributes: Attributes = useMemo(
() => growthBook?.getAttributes() ?? {},
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.