From bf255139f0af45a757ac1fef101150efc5e544ab Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Wed, 22 Jul 2020 10:50:49 -0300 Subject: [PATCH] Handle cases where the domain is undefined --- src/cookie.ts | 2 +- test/cookie.test.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cookie.ts b/src/cookie.ts index d7f4f9d6..c9b32259 100644 --- a/src/cookie.ts +++ b/src/cookie.ts @@ -1,4 +1,4 @@ -export function getBaseDomain(domain = window.document.domain): string { +export function getBaseDomain(domain = window.document.domain ?? ''): string { const parts: string[] = domain.split('.'); const random = `___${Math.random()}`; diff --git a/test/cookie.test.ts b/test/cookie.test.ts index 26f63f82..17da3a94 100644 --- a/test/cookie.test.ts +++ b/test/cookie.test.ts @@ -15,6 +15,11 @@ beforeEach(() => { }); describe('A function to detect the base domain', () => { + test('should not fail if the domain is undefined', () => { + expect(document.domain).toBeUndefined(); + expect(getBaseDomain()).toBe(''); + }); + test('should detect the top-most domain that allows for setting cookies', () => { let cookie = ''; cookieSetter.mockImplementation(value => {