diff --git a/.changeset/renovate-e1b8baf.md b/.changeset/renovate-e1b8baf.md new file mode 100644 index 000000000..be819f4c1 --- /dev/null +++ b/.changeset/renovate-e1b8baf.md @@ -0,0 +1,5 @@ +--- +'@scaleway/use-growthbook': patch +--- + +Updated dependency `@growthbook/growthbook-react` to `0.18.0`. diff --git a/packages/use-growthbook/package.json b/packages/use-growthbook/package.json index 7a82b5948..41c8ee9d4 100644 --- a/packages/use-growthbook/package.json +++ b/packages/use-growthbook/package.json @@ -25,7 +25,7 @@ "feature flags" ], "dependencies": { - "@growthbook/growthbook-react": "0.17.0" + "@growthbook/growthbook-react": "0.18.0" }, "devDependencies": { "react": "18.2.0" diff --git a/packages/use-growthbook/src/AbTestProvider.tsx b/packages/use-growthbook/src/AbTestProvider.tsx index 229b6c135..324b6fc18 100644 --- a/packages/use-growthbook/src/AbTestProvider.tsx +++ b/packages/use-growthbook/src/AbTestProvider.tsx @@ -1,11 +1,7 @@ -/* 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 @@ -13,13 +9,7 @@ export type ToolConfig = { 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 +export type TrackingCallback = NonNullable export type AbTestProviderProps = { children: ReactNode @@ -38,7 +28,7 @@ const getGrowthBookInstance = ({ config: ToolConfig attributes: Attributes trackingCallback: TrackingCallback -}): GrowthBookType => +}) => new GrowthBook({ apiHost, clientKey, @@ -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], ) @@ -79,7 +69,3 @@ export const AbTestProvider = ({ {children} ) } -/* 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 */ diff --git a/packages/use-growthbook/src/__mocks__/@growthbook/growthbook-react.tsx b/packages/use-growthbook/src/__mocks__/@growthbook/growthbook-react.tsx new file mode 100644 index 000000000..daccaf138 --- /dev/null +++ b/packages/use-growthbook/src/__mocks__/@growthbook/growthbook-react.tsx @@ -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 } diff --git a/packages/use-growthbook/src/__tests__/AbTestProvider.tsx b/packages/use-growthbook/src/__tests__/AbTestProvider.tsx index e8fe8e4f2..8f10e5601 100644 --- a/packages/use-growthbook/src/__tests__/AbTestProvider.tsx +++ b/packages/use-growthbook/src/__tests__/AbTestProvider.tsx @@ -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 describe('AbTestProvider', () => { let trackingCallback: TrackingCallback @@ -35,8 +30,7 @@ describe('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', () => { @@ -57,7 +51,6 @@ describe('AbTestProvider', () => { , ) - expect(mockGrowthBook).toBeCalled() - expect(mockGrowthBook.mock.instances[0]?.loadFeatures).not.toBeCalled() + expect(GrowthBook).toBeCalled() }) }) diff --git a/packages/use-growthbook/src/__tests__/useAbTestAttributes.ts b/packages/use-growthbook/src/__tests__/useAbTestAttributes.ts index 7e5217799..0184141d1 100644 --- a/packages/use-growthbook/src/__tests__/useAbTestAttributes.ts +++ b/packages/use-growthbook/src/__tests__/useAbTestAttributes.ts @@ -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' @@ -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) }) it('should allow to get attributes from GrowthBook', () => { diff --git a/packages/use-growthbook/src/index.ts b/packages/use-growthbook/src/index.ts index 1feaa7ceb..5b7793558 100644 --- a/packages/use-growthbook/src/index.ts +++ b/packages/use-growthbook/src/index.ts @@ -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' diff --git a/packages/use-growthbook/src/types.ts b/packages/use-growthbook/src/types.ts index 88961ead8..44d5488d8 100644 --- a/packages/use-growthbook/src/types.ts +++ b/packages/use-growthbook/src/types.ts @@ -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 - setAttributes: (attributes: Attributes) => void -} diff --git a/packages/use-growthbook/src/useAbTestAttributes.ts b/packages/use-growthbook/src/useAbTestAttributes.ts index 3cafb4319..990eaab28 100644 --- a/packages/use-growthbook/src/useAbTestAttributes.ts +++ b/packages/use-growthbook/src/useAbTestAttributes.ts @@ -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() ?? {}, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 196809db5..a2975895c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -235,8 +235,8 @@ importers: packages/use-growthbook: dependencies: '@growthbook/growthbook-react': - specifier: 0.17.0 - version: 0.17.0(react@18.2.0) + specifier: 0.18.0 + version: 0.18.0(react@18.2.0) devDependencies: react: specifier: 18.2.0 @@ -2418,18 +2418,18 @@ packages: tslib: 2.5.3 dev: false - /@growthbook/growthbook-react@0.17.0(react@18.2.0): - resolution: {integrity: sha512-od+bSg3RD9AXfi/TWFDSzRaB6RuJLkUX5eFOZz8u8wL8yDbgd5RF9cwu4lQIF9/tyG1WLu4qipV+SM2v5s4BcQ==} + /@growthbook/growthbook-react@0.18.0(react@18.2.0): + resolution: {integrity: sha512-Nuyn6j3nmSNN7hMcc4OEl0IhMH2nQ40zVXDYuw0q4IgfNN3gWvA8UrUeqK/p1vevBKtErXNZmETIRYe4iVoNeg==} engines: {node: '>=10'} peerDependencies: react: ^16.8.0-0 || ^17.0.0-0 || ^18.0.0-0 || 18 dependencies: - '@growthbook/growthbook': 0.27.0 + '@growthbook/growthbook': 0.28.0 react: 18.2.0 dev: false - /@growthbook/growthbook@0.27.0: - resolution: {integrity: sha512-gt/DWXfgyudY3gXAUjzKm9kMjVfCzfRc8d0nQQiXCP523ow23jThrmBzkRKCN+zxYVxUKcHjP9yjU9EKJqP4Fw==} + /@growthbook/growthbook@0.28.0: + resolution: {integrity: sha512-RnrP29yHfL12nmAxtPbyHlY70BSB2f/LIOrYaBhqn2oBNdeuAxwGt9FO40dDJh0W+opaG5EbiPvZUxsy8J0aAw==} engines: {node: '>=10'} dependencies: dom-mutator: 0.5.0