Skip to content

Commit

Permalink
fix: provide disableTimer option for testing (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
imundra authored Sep 20, 2024
1 parent 79c5fbf commit 3937356
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@coinbase/cookie-banner": "1.0.4",
"@coinbase/cookie-manager": "1.1.7",
"@coinbase/cookie-manager": "1.1.8",
"next": "14.1.1",
"react": "^18",
"react-dom": "^18"
Expand Down
2 changes: 1 addition & 1 deletion packages/cookie-banner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"react-dom": "^18.1.0"
},
"dependencies": {
"@coinbase/cookie-manager": "^1.1.7",
"@coinbase/cookie-manager": "^1.1.8",
"react-intl": "^6.5.1",
"styled-components": "^5.3.6"
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cookie-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.8 (09/18/2024)

- Introduce an optional `disableTimer` prop in `CookieProvider` to enhance testing flexibility and prevent conflicts with frequently used functions such as `runAllTimers` and `advanceTimersByTime`.

## 1.1.7 (07/17/2024)

- Include Tracker in the list of exported types
Expand Down
2 changes: 1 addition & 1 deletion packages/cookie-manager/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coinbase/cookie-manager",
"version": "1.1.7",
"version": "1.1.8",
"description": "Coinbase Cookie Manager",
"main": "dist/index.js",
"license": "Apache-2.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/cookie-manager/src/CookieContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const CookieProvider = ({ children }: Props) => {
onPreferenceChange,
initialCookieValues,
initialGPCValue,
disableTimer,
} = useTrackingManager();

const POLL_INTERVAL = 500;
Expand Down Expand Up @@ -109,6 +110,12 @@ export const CookieProvider = ({ children }: Props) => {
}
};

// We provide a disableTimer option to avoid setting the interval and potentially introducing issues in timer related tests.
// This is meant to be used in testing only!
if (disableTimer == true) {
return () => {};
}

checkCookies();
// Call the function once before setting the interval
const intervalId = setInterval(checkCookies, POLL_INTERVAL);
Expand All @@ -118,6 +125,7 @@ export const CookieProvider = ({ children }: Props) => {
};
}
}, []);

useEffect(() => {
if (onPreferenceChange) {
onPreferenceChange(trackingPreference);
Expand Down
4 changes: 4 additions & 0 deletions packages/cookie-manager/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export type TrackingManagerDependencies = {
log: LogFunction;
initialCookieValues?: Record<string, string>;
initialGPCValue?: boolean;
// We provide a disableTimer option to avoid setting an interval for
// periodically checking cookies. This is meant to be used in testing only to
// avoid issues with timer related tests.
disableTimer?: boolean;
};

export type AdTrackingPreference = {
Expand Down

0 comments on commit 3937356

Please sign in to comment.