aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-ackee
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks/use-ackee')
-rw-r--r--src/utils/hooks/use-ackee/index.ts1
-rw-r--r--src/utils/hooks/use-ackee/use-ackee.test.tsx44
-rw-r--r--src/utils/hooks/use-ackee/use-ackee.ts15
3 files changed, 60 insertions, 0 deletions
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<AckeeProviderProps>,
+ config: AckeeProviderProps
+) =>
+ function CreatedWrapper({ children }: { children: ReactNode }) {
+ return <Wrapper {...config}>{children}</Wrapper>;
+ };
+
+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;
+};