From c9b2dcb2a8232b179e5cc0d726cf98cd645c476d Mon Sep 17 00:00:00 2001 From: Neek Sandhu Date: Thu, 13 Jul 2023 12:58:39 -0700 Subject: [PATCH] [LIBWEB-1353] Fix cookie write error --- .../src/core/user/__tests__/index.test.ts | 24 +++++++++++++++---- .../src/core/user/__tests__/storage.test.ts | 15 ++++++++++++ packages/browser/src/core/user/index.ts | 15 ++++++------ 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 packages/browser/src/core/user/__tests__/storage.test.ts diff --git a/packages/browser/src/core/user/__tests__/index.test.ts b/packages/browser/src/core/user/__tests__/index.test.ts index 7f78deab7..968571f99 100644 --- a/packages/browser/src/core/user/__tests__/index.test.ts +++ b/packages/browser/src/core/user/__tests__/index.test.ts @@ -19,6 +19,20 @@ function clear(): void { localStorage.clear() } +/** + * Filters out the calls made for probing cookie availability + */ +const ignoreProbeCookieWrites = ( + fn: jest.SpyInstance< + string | undefined, + [ + name: string, + value: string | object, + options?: jar.CookieAttributes | undefined + ] + > +) => fn.mock.calls.filter((c) => c[0] !== 'ajs_cookies') + let store: LocalStorage beforeEach(function () { store = new LocalStorage() @@ -58,7 +72,7 @@ describe('user', () => { assert(user.anonymousId()?.length === 36) expect(jar.get('ajs_anonymous_id')).toBeUndefined() expect(localStorage.getItem('ajs_anonymous_id')).toBeNull() - expect(setCookieSpy.mock.calls.length).toBe(0) + expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0) }) it('should not overwrite anonymous id', () => { @@ -218,7 +232,7 @@ describe('user', () => { user.id('foo') assert(user.anonymousId() === prev) - expect(setCookieSpy.mock.calls.length).toBe(0) + expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0) }) it('should reset anonymousId if the user id changed', () => { @@ -227,7 +241,7 @@ describe('user', () => { user.id('baz') assert(user.anonymousId() !== prev) assert(user.anonymousId()?.length === 36) - expect(setCookieSpy.mock.calls.length).toBe(0) + expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0) }) it('should not reset anonymousId if the user id changed to null', () => { @@ -236,7 +250,7 @@ describe('user', () => { user.id(null) assert(user.anonymousId() === prev) assert(user.anonymousId()?.length === 36) - expect(setCookieSpy.mock.calls.length).toBe(0) + expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0) }) }) @@ -795,7 +809,7 @@ describe('group', () => { expect(group.id()).toBe('gid') expect(jar.get('ajs_group_id')).toBeFalsy() expect(store.get('ajs_group_id')).toBeFalsy() - expect(setCookieSpy.mock.calls.length).toBe(0) + expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0) }) it('behaves the same as user', () => { diff --git a/packages/browser/src/core/user/__tests__/storage.test.ts b/packages/browser/src/core/user/__tests__/storage.test.ts new file mode 100644 index 000000000..30100a30c --- /dev/null +++ b/packages/browser/src/core/user/__tests__/storage.test.ts @@ -0,0 +1,15 @@ +import { Cookie } from '..' + +describe('Cookie storage', () => { + it('should report cookie storage available when cookies are accessible', () => { + expect(Cookie.available()).toBe(true) + }) + + it('should report cookie storage unavailable when cookies are not accessible', () => { + ;(document as any).__defineGetter__('cookie', function () { + return '' + }) + + expect(Cookie.available()).toBe(false) + }) +}) diff --git a/packages/browser/src/core/user/index.ts b/packages/browser/src/core/user/index.ts index a778ecd21..ff888ca60 100644 --- a/packages/browser/src/core/user/index.ts +++ b/packages/browser/src/core/user/index.ts @@ -62,15 +62,14 @@ const ONE_YEAR = 365 export class Cookie extends Store { static available(): boolean { - let cookieEnabled = window.navigator.cookieEnabled - - if (!cookieEnabled) { - jar.set('ajs:cookies', 'test') - cookieEnabled = document.cookie.includes('ajs:cookies') - jar.remove('ajs:cookies') + try { + jar.set('ajs_cookies', 'test') + const cookieEnabled = document.cookie.includes('ajs_cookies') + jar.remove('ajs_cookies') + return cookieEnabled + } catch (error) { + return false } - - return cookieEnabled } static get defaults(): CookieOptions {