From 3ab9f0423e97af63da4bf6a13ffd786955bd5b3b Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 26 Oct 2023 21:55:55 +0200 Subject: refactor(hooks,providers): rewrite useAckee hook and AckeeProvider --- src/utils/hooks/index.ts | 2 +- src/utils/hooks/use-ackee/index.ts | 1 + src/utils/hooks/use-ackee/use-ackee.test.tsx | 44 ++++++++++++++++++++++++++++ src/utils/hooks/use-ackee/use-ackee.ts | 15 ++++++++++ src/utils/hooks/use-update-ackee-options.tsx | 17 ----------- 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 src/utils/hooks/use-ackee/index.ts create mode 100644 src/utils/hooks/use-ackee/use-ackee.test.tsx create mode 100644 src/utils/hooks/use-ackee/use-ackee.ts delete mode 100644 src/utils/hooks/use-update-ackee-options.tsx (limited to 'src/utils/hooks') diff --git a/src/utils/hooks/index.ts b/src/utils/hooks/index.ts index 47e90f1..cf8c01c 100644 --- a/src/utils/hooks/index.ts +++ b/src/utils/hooks/index.ts @@ -1,3 +1,4 @@ +export * from './use-ackee'; export * from './use-add-classname'; export * from './use-article'; export * from './use-attributes'; @@ -20,4 +21,3 @@ export * from './use-route-change'; export * from './use-scroll-position'; export * from './use-settings'; export * from './use-state-change'; -export * from './use-update-ackee-options'; diff --git a/src/utils/hooks/use-ackee/index.ts b/src/utils/hooks/use-ackee/index.ts new file mode 100644 index 0000000..81cac12 --- /dev/null +++ b/src/utils/hooks/use-ackee/index.ts @@ -0,0 +1 @@ +export * from './use-ackee'; diff --git a/src/utils/hooks/use-ackee/use-ackee.test.tsx b/src/utils/hooks/use-ackee/use-ackee.test.tsx new file mode 100644 index 0000000..230fe0b --- /dev/null +++ b/src/utils/hooks/use-ackee/use-ackee.test.tsx @@ -0,0 +1,44 @@ +import { act, renderHook } from '@testing-library/react'; +import type { FC, ReactNode } from 'react'; +import type { AckeeTrackerValue } from '../../../types'; +import { AckeeProvider, type AckeeProviderProps } from '../../providers'; +import { useAckee } from './use-ackee'; + +const createWrapper = ( + Wrapper: FC, + config: AckeeProviderProps +) => + function CreatedWrapper({ children }: { children: ReactNode }) { + return {children}; + }; + +describe('useAckee', () => { + it('should return the default value without provider and prevent update', () => { + const { result } = renderHook(() => useAckee()); + + expect(result.current[0]).toBe('full'); + + act(() => result.current[1]()); + + expect(result.current[0]).toBe('full'); + }); + + it('can update the value', () => { + const defaultValue: AckeeTrackerValue = 'full'; + + const { result } = renderHook(() => useAckee(), { + wrapper: createWrapper(AckeeProvider, { + domainId: 'some-id', + server: 'https://example.com', + storageKey: 'veniam', + tracking: defaultValue, + }), + }); + + expect(result.current[0]).toBe(defaultValue); + + act(() => result.current[1]()); + + expect(result.current[0]).toBe('partial'); + }); +}); diff --git a/src/utils/hooks/use-ackee/use-ackee.ts b/src/utils/hooks/use-ackee/use-ackee.ts new file mode 100644 index 0000000..a89701a --- /dev/null +++ b/src/utils/hooks/use-ackee/use-ackee.ts @@ -0,0 +1,15 @@ +import { useCallback, useContext } from 'react'; +import { AckeeContext } from '../../providers'; + +export const useAckee = () => { + const { tracking, setTracking } = useContext(AckeeContext); + + const toggle = useCallback(() => { + setTracking((prev) => { + if (prev === 'full') return 'partial'; + return 'full'; + }); + }, [setTracking]); + + return [tracking, toggle] as const; +}; diff --git a/src/utils/hooks/use-update-ackee-options.tsx b/src/utils/hooks/use-update-ackee-options.tsx deleted file mode 100644 index f6e5c86..0000000 --- a/src/utils/hooks/use-update-ackee-options.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import { useEffect } from 'react'; -import { useAckeeTracker } from '../providers'; - -export type AckeeOptions = 'full' | 'partial'; - -/** - * Update Ackee settings with the given choice. - * - * @param {AckeeOptions} value - Either `full` or `partial`. - */ -export const useUpdateAckeeOptions = (value: AckeeOptions) => { - const { setDetailed } = useAckeeTracker(); - - useEffect(() => { - setDetailed(value === 'full'); - }, [value, setDetailed]); -}; -- cgit v1.2.3