This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(insights): add getInsightsAnonymousUserToken helper (#2887)
- Loading branch information
Showing
5 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/react-instantsearch-dom/src/core/__tests__/getInsightsAnonymousUserToken.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import getInsightsAnonymousUserToken, { | ||
ANONYMOUS_TOKEN_COOKIE_KEY, | ||
} from '../getInsightsAnonymousUserToken'; | ||
|
||
const DAY = 86400000; /* 1 day in ms*/ | ||
const DATE_TOMORROW = new Date(Date.now() + DAY).toUTCString(); | ||
const DATE_YESTERDAY = new Date(Date.now() - DAY).toUTCString(); | ||
|
||
const resetCookie = cookieKey => { | ||
document.cookie = `${cookieKey}=;expires=Thu, 01-Jan-1970 00:00:01 GMT;`; | ||
}; | ||
|
||
describe('getInsightsAnonymousUserToken', () => { | ||
beforeEach(() => { | ||
resetCookie(ANONYMOUS_TOKEN_COOKIE_KEY); | ||
}); | ||
|
||
it('should return undefined when no cookies', () => { | ||
expect(getInsightsAnonymousUserToken()).toBe(undefined); | ||
}); | ||
|
||
it('should return undefined when cookie present but expired', () => { | ||
document.cookie = `${ANONYMOUS_TOKEN_COOKIE_KEY}=anonymous-uuid;expires=${DATE_YESTERDAY};`; | ||
expect(getInsightsAnonymousUserToken()).toBe(undefined); | ||
}); | ||
|
||
it('should return the anonymous uuid when cookie present and valid', () => { | ||
document.cookie = `${ANONYMOUS_TOKEN_COOKIE_KEY}=anonymous-uuid;expires=${DATE_TOMORROW};`; | ||
expect(getInsightsAnonymousUserToken()).toBe('anonymous-uuid'); | ||
}); | ||
|
||
it('should return the anonymous uuid when other cookies are invalid', () => { | ||
document.cookie = `${ANONYMOUS_TOKEN_COOKIE_KEY}=anonymous-uuid;expires=${DATE_TOMORROW};`; | ||
document.cookie = `BAD_COOKIE=val%ue;expires=${DATE_TOMORROW};path=/`; | ||
expect(getInsightsAnonymousUserToken()).toBe('anonymous-uuid'); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/react-instantsearch-dom/src/core/getInsightsAnonymousUserToken.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const ANONYMOUS_TOKEN_COOKIE_KEY = '_ALGOLIA'; | ||
|
||
function getCookie(name: string): string | undefined { | ||
const prefix = `${name}=`; | ||
const cookies = document.cookie.split(';'); | ||
for (let i = 0; i < cookies.length; i++) { | ||
let cookie = cookies[i]; | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1); | ||
} | ||
if (cookie.indexOf(prefix) === 0) { | ||
return cookie.substring(prefix.length, cookie.length); | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
export default function getInsightsAnonymousUserToken(): string | undefined { | ||
return getCookie(ANONYMOUS_TOKEN_COOKIE_KEY); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters